Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-27-2009, 10:46 PM
|
#1
|
LQ Newbie
Registered: Feb 2009
Posts: 8
Rep:
|
php question, how do I get a return from a field within a field?
I am creating a game with random variables. In the game I have created a dialogue exchange to players. I have set up a table with various returns and I inserted {$fields} to represent various random variables. When I call on the requested fields, I only see the field text and my field names. Am I supposed to parse something and call it back another way?
ie:
myfield is: "You have won {$random1} silver! <br />{$wi['gender'] majesty rewards you well."
the code I am using to call that field is:
if($wi['wsex']=="Male")
{$gender=his;}
else if($wi['wsex']!="Male")
{$gender=her;}
$random1=rand(6,34);
print $wi['myfield'];
Last edited by cherrington; 04-27-2009 at 10:47 PM.
|
|
|
04-28-2009, 06:06 AM
|
#2
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
It would have been better to copy the code used for assigning myfield a value verbatim instead of describing it. Also please use PHP tags around PHP code.
I assume this is what you did:
PHP Code:
$myfield="You have won {$random1} silver! <br />{$wi['gender'] majesty rewards you well."
You have to enclose {$wi['gender'] } in curly braces. The right one is missing in your code.
jlinkels
|
|
|
04-28-2009, 07:16 AM
|
#3
|
LQ Newbie
Registered: Feb 2009
Posts: 8
Original Poster
Rep:
|
Sorry about the long description, and the missing curly bracket was only made in my post.
|
|
|
04-28-2009, 07:45 AM
|
#4
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
PHP Code:
<?
$wi['wsex']="Female";
if($wi['wsex']=="Male"){
$gender=his;
}
else if($wi['wsex']!="Male") {
{$gender=her;}
}
$random1=rand(6,34);
$wi['gender']=$gender;
$wi['myfield']="You have won $random1 silver! <br />{$wi['gender']} majesty rewards you well." ;
print $wi['myfield'];
?>
You might want to reconsider what you put into the $wi[] array referencing with $wi['name'], and what you put directly into the variable $name. You have been mixing up those two.
jlinkels
|
|
|
04-28-2009, 08:39 AM
|
#5
|
LQ Newbie
Registered: Feb 2009
Posts: 8
Original Poster
Rep:
|
My apologies again, the actual text "You have won $random1 silver! <br />{$wi['gender']} majesty rewards you well." is stored in a field from my database. Depending on a random result, the text will be different each time in the fields ocsuc1, ocsuc2, ocsuc3, ocfail1, and ocfail2.
This is the actual code I am working on:
$sq=$db->query("SELECT wk.*,un.*,oc.* FROM worker wk LEFT JOIN occupation oc ON wk.wkoc=oc.ocid LEFT JOIN unemp un ON wk.wkitm=un.wid WHERE wk.wkid={$_GET['ID']} AND wk.wkuid=$userid");
$wi=$db->fetch_row($sq);
if($wq['wkaction']>$wi['ocaction'])
{
die("Sorry, {$wi['wname']} is just too tired to work again this day, try again tomorrow.<br />
<a href='worker.php'>> Back</a>");
}
$db->query("UPDATE worker SET wkaction=wkaction+1 WHERE wkid={$_GET['ID']} AND wkuid=$userid");
if($wi['wsex']=="Male"){$gen=his;}else if($wi['wsex']!="Male"){$gen=her;}
if($wi['wsex']=="Male"){$gend=him;}else if($wi['wsex']!="Male"){$gend=her;}
if($wq['wkdom']<=rand(1,100))
{
$numb=rand(1,4);
$upkeep=($wi['ocup']*$numb)*$wq['wkrk'];
$gain=($wi['ocpay']*$numb)*$wq['wkrk'];
$num=rand(1, 5);
switch($num)
{
case 1:
print $wi['ocsuc1'];
break;
case 2:
print $wi['ocsuc2'];
break;
case 3:
print $wi['ocsuc3'];
break;
case 4:
print $wi['ocfail1'];
break;
case 5:
print $wi['ocfail2'];
break;
}
}
In each 'case' I have a different script requiring the different $gen, $gend, & $numb arrays (and their calculated results in $gain and $upkeep). I was tying to describe what I was actually trying to do without throwing a bunch of code that, to me, would have been confusing 
Last edited by cherrington; 04-28-2009 at 08:42 AM.
|
|
|
04-28-2009, 09:32 AM
|
#6
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
I suggest that you insert some statements to print out the values of $wi and $wq.
It should become clear soon enough what is in those arrays as compared to what you expect.
From your code I see you are giving $gen and $gend a value, but you don't use it at any further point in the code.
jlinkels
|
|
|
04-28-2009, 09:47 AM
|
#7
|
LQ Newbie
Registered: Feb 2009
Posts: 8
Original Poster
Rep:
|
That is because they are in the text of the fields of ocsuc1, ocsuc2, ocsuc3, ocfail1, and ocfail2.
|
|
|
04-28-2009, 10:03 AM
|
#8
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
No they are not. You didn't put them there, so they ain't there.
jlinkels
|
|
|
04-28-2009, 10:23 AM
|
#9
|
LQ Newbie
Registered: Feb 2009
Posts: 8
Original Poster
Rep:
|
For example, as I have many rows in my data table occupation, the content I have in the field ocsuc1 is:"{$wi['wname']} picks {$numb} pockets and gains {$gain} silver for {$gen} coffer." Where on a different row, ocsuc1 may say: "Your worker {$wi['wname']}, has found {$gain} silver."
The expected result should look like this "Boris picks 2 pockets and gains 12 silver for his coffer." Instead I get "{$wi['wname']} picks {$numb} pockets and gains {$gain} silver for {$gen} coffer."
Last edited by cherrington; 04-28-2009 at 10:27 AM.
|
|
|
04-28-2009, 10:58 AM
|
#10
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
That doesn't work.
Quote:
Originally Posted by cherrington
{$wi['wname']} picks {$numb} pockets and gains {$gain} silver for {$gen} coffer.
|
This is simply a string which you retrieve from the database. It is a string with {}, [], $ and '' characters, which are part of the string and don't mean anything for PHP.
If you want to substitute the correct values in the string, you have to build the string inside your php code.
PHP Code:
$mystring="you won $numb"
will correctly build the string for you, whereas retrieving this string:
PHP Code:
"you won $numb"
from the database will print out "you won $numb"
But you said that.
You could try to use the eval() function to evaluate the string you get back from MySQL which does what your intend to do. However, you are entering quote and escape hell, and you'll completely loose track about what is string and what is a variable, let alone if you insert array fields with literal names as well. I advice against it, you'd better try to compose the string from the fields you get returned from the database as I explained above.
jlinkels
|
|
|
04-28-2009, 11:06 AM
|
#11
|
LQ Newbie
Registered: Feb 2009
Posts: 8
Original Poster
Rep:
|
Thanks, I was afraid of what I was trying to do was not possible  or worse yet, taking me to quote and escape hell.
|
|
|
04-29-2009, 01:27 AM
|
#12
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
Of course it is possible. As you were told, the eval() function is what you want to use. It does exactly what you are trying to do: takes a string and evaluates it as PHP code.
http://us2.php.net/manual/en/function.eval.php
|
|
|
All times are GMT -5. The time now is 08:28 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|