Jump to content

problem with variable variables


joebudden

Recommended Posts

hi guys..

 

right this is my query..

$sql = "SELECT playersurname
	FROM player,playsfor
	WHERE player.playerid = playsfor.playerid and teamid = '$teamid'";

 

.. it returns 6 rows..

 

what i want to do i have each row as a separate variable... ($p1, $p2, $p3 etc etc) ... so i can them pass them to FLASH

 

this is my code so far...

 

<?php

$res = mysql_query($sql) or die (mysql_error());

while(list($playername) = mysql_fetch_array($res))
{
  for ($i=0;$i<6;$i++)
   {
$p{$i} = $playername;
   }
}

echo $p1;
echo $p2;
echo $p3;

?>

 

I've tried several different variations of $p{$i} but keep getting errors...

 

can anyone fix it please ??

 

thanx

Link to comment
https://forums.phpfreaks.com/topic/40780-problem-with-variable-variables/
Share on other sites

If you really want variable variables then

 

<?php
$p = array ('Smith', 'Jones', 'Doe', 'Bloggs', 'Brown', 'Black');

foreach ($p as $k=>$n) {
    $var = "var$k";
    $$var = $n ;
}

echo "$var0<br>";
echo "$var1<br>";
echo "$var2<br>";
echo "$var3<br>";
echo "$var4<br>";
echo "$var5<br>";

?>

 

But why can't you just pass $p[0], $p[1] etc to Flash?

ok heres what iv got is this correct barand??

 

<?php
// get my players

$sql = "SELECT playersurname
	FROM player,playsfor
	WHERE player.playerid = playsfor.playerid and teamid = '$teamid'";

$res = mysql_query($sql) or die (mysql_error());

while($row = mysql_fetch_assoc($res))
{
  foreach ($row as $k=>$n) {
    $var = "p$k";
    $$var = $n ;
    }
}

echo $var1;
echo $var2;
echo $var3;
?>

 

because it says undefined variable when i run it

 

so how could i just pass $p[0], $p[1] etc ?? coz when i implemented that it was the same player name whatever number i put in the []'s ??

 

please help

try

<?php
$sql = "SELECT playersurname
	FROM player,playsfor
	WHERE player.playerid = playsfor.playerid and teamid = '$teamid'";

$res = mysql_query($sql) or die (mysql_error());

while($row = mysql_fetch_assoc($res))
{
     $p[] = $row['playersurname'];
}


foreach ($p as $k=>$n) {
    $var = "var$k";
    $$var = $n ;
}

echo $var0, '<br>';
echo $var1, '<br>';
echo $var2, '<br>';
echo $var3, '<br>';

echo '<br>';
echo $p[0], '<br>';
echo $p[1], '<br>';
echo $p[2], '<br>';
echo $p[3], '<br>';


?>

I'd go with $p[0] etc, then all you need is

 

<?php
$sql = "SELECT playersurname
	FROM player,playsfor
	WHERE player.playerid = playsfor.playerid and teamid = '$teamid'";

$res = mysql_query($sql) or die (mysql_error());

while($row = mysql_fetch_assoc($res))
{
     $p[] = $row['playersurname'];
}
?>

thanks again barand... before you go tho, can you see anything wrong with this ?

 

<?php

// get opponent players

$opsql = "SELECT playersurname
	  FROM player,playsfor
	  WHERE player.playerid = playsfor.playerid and teamid = '$oppositionid'";

$opres = mysql_query($opsql) or die (mysql_error());

while($r = mysql_fetch_assoc($opres))
{
     $z[] = $r['playersurname'];

 echo $r['playersurname'];

}

$z1 = $z[0];
$z2 = $z[1];
$z3 = $z[2];
$z4 = $z[3];
$z5 = $z[4];
$z6 = $z[5];

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.