Forum Moderators: coopster

Message Too Old, No Replies

can i have variable variables from sql in html?

         

Salvador_nl

12:50 am on May 7, 2006 (gmt 0)

10+ Year Member



Hello,

i work on a site where the contents can grow, so i wanted to have the possibility to have the admin add 'groups'.

The client must have the option to fill in a code in any of these groups.

i want something like below, but i can not get it to work?!

-----------a include file------------

if ($actie=="formlist") {
while($record = mysql_fetch_object($sql)){

echo "<tr><td align=\"right\"><b>$record->game&nbsp;#id</b></td><td>&nbsp;<input type=\"txt\" name=\"$record->game\" value=\"0\" size=\"12\" $readonly ></td></tr>" ;

} }

if ($actie=="postlist") {
while($record = mysql_fetch_object($sql)){

$$record->game=$_POST['$record->game'] ; echo $_POST['$record->game'] ;

} }

if ($actie=="showpost") {
while($record = mysql_fetch_object($sql)){

echo "<tr><td>" . $record->game . "</td><td>" . $_POST['$$record->game'] . "</td></tr>" ;

} }

--------------end---------

the double $$ is not a typo, i wanted to assign the variable the sql-field-content as a name

i feel like i dont see the obvious sollution, and it has taken me many many hours now.

help :(

Salvador_nl

10:42 am on May 7, 2006 (gmt 0)

10+ Year Member



another few hrs spended, but i still dont see my problem, however i did notice a typo in the end of the example script i wrote above

$_POST['$$record->game']

--> the $$record is $record, in my original script.

eelixduppy

12:16 am on May 8, 2006 (gmt 0)



Welcome to WebmasterWorld!

I'm not exactly sure what is wrong with your code, but MAYBE this will help. Change

$$record->game=$_POST['$record->game'] ;

to something like this:

${$record->game}=$_POST['$record->game'] ;

where ever you see it, or something like it.

See if this makes a difference. Good luck!

eelix

Salvador_nl

7:43 am on May 8, 2006 (gmt 0)

10+ Year Member



(i love webmasterworld, and it has been a rescue & valuable resource on a few occasions.)

tanx for your reply, i have tried it, but no succes.

it's part of the base-structure of the site i try to create, so i ame pretty stuck :(

-all input is very welcome

Salvador_nl

7:45 am on May 8, 2006 (gmt 0)

10+ Year Member



another cut+paste typo ..ignore the value=\"0\", it is not part of my original script

Salvador_nl

8:11 am on May 15, 2006 (gmt 0)

10+ Year Member



so i guess, something like;

$var="user12" ;

$ . $var = "1234567890" ;

echo $$var // output : 1234567890

can not be done?!

would have saved me db-space & scripting time

whoisgregg

1:39 pm on May 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$var="user12" ;
${$var} = "1234567890" ;
echo ${$var}; // output: 1234567890

The easiest way to always do variable variables correctly is to use the curly brackets both when setting and echoing.

whoisgregg

1:44 pm on May 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would go another step with eelixduppy's suggestion and also use double quotes in the POST bit:

${$record->game}=$_POST["$record->game"];

The single quotes ensure that what's inside them is used as a literal string instead of being parsed. I am pretty sure it would also work if you don't use quotes (since you are calling a variable anyways):

${$record->game}=$_POST[$record->game];

Salvador_nl

8:28 pm on May 21, 2006 (gmt 0)

10+ Year Member



thanks for the replies, i gave up on this already

:D

siMKin

6:58 am on May 23, 2006 (gmt 0)

10+ Year Member



meaning, it still doesn't work?

You say

i can not get it to work

But what exactly doesn't work? What did you try to debug it? What does work?

I also notice you use the deprecated

if ($actie=="formlist") {

At least, i assume $actie comes from a (posted) form? In that case you should write $_POST['actie'] - it may very well be, that your php is configured in such a way it won't recognise the $actie (read something about register_globals). In that case, of course, it will never work.
Also, the fact you're not checking with an isset() first means the error_reporting is also not set to E_ALL. I would strongly recommend doing so when you're in a situation like this where you can't find the problem. (in fact i would recommend to set it to E_ALL always, while developing).

If after this, you still can't find the problem, try to be a bit more specific about what does and what doesn't work. Cuz i'm sure there's a solution to whatever your problem is