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
Share on other sites

Use a temporary array:

<?php
$p = array();
$res = mysql_query($sql) or die (mysql_error());

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

for($i=0;$i<count($p);$i++)
   echo $p[$i] . '<br>';
?>

 

Ken

Link to comment
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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';


?>

Link to comment
Share on other sites

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'];
}
?>

Link to comment
Share on other sites

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];

?>

Link to comment
Share on other sites

it will restrict you to only 6 rows...

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

 

use this

$i = 1;
foreach ($z as $plName)
{
     $z.$i = $plName;
     $i++;
}

from here you will get all the rows...

like $z1,$z2.... etc.....

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.