LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-27-2009, 10:46 PM   #1
cherrington
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Rep: Reputation: 0
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.
 
Old 04-28-2009, 06:06 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
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
 
Old 04-28-2009, 07:16 AM   #3
cherrington
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Sorry about the long description, and the missing curly bracket was only made in my post.
 
Old 04-28-2009, 07:45 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
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
 
Old 04-28-2009, 08:39 AM   #5
cherrington
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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'>&gt; 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.
 
Old 04-28-2009, 09:32 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
I suggest that you insert some
PHP Code:
print_r() 
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
 
Old 04-28-2009, 09:47 AM   #7
cherrington
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
That is because they are in the text of the fields of ocsuc1, ocsuc2, ocsuc3, ocfail1, and ocfail2.
 
Old 04-28-2009, 10:03 AM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
No they are not. You didn't put them there, so they ain't there.

jlinkels
 
Old 04-28-2009, 10:23 AM   #9
cherrington
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 04-28-2009, 10:58 AM   #10
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
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
 
Old 04-28-2009, 11:06 AM   #11
cherrington
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks, I was afraid of what I was trying to do was not possible or worse yet, taking me to quote and escape hell.
 
Old 04-29-2009, 01:27 AM   #12
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Default value in a php variable if a form-field is empty? MheAd Programming 2 12-19-2008 06:21 AM
How to hyperlink one field from the results of a php query texmansru47 Programming 1 06-27-2008 03:56 PM
[PHP] [mySQL] Finding all records with with a certain field. s0l1dsnak3123 Programming 2 06-15-2008 08:26 PM
php mass update database field value frieza Programming 3 04-02-2008 05:50 PM
qmail spoofing return-path (from field) j0hnd0e Linux - Server 1 03-29-2007 10:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration