joebudden Posted March 1, 2007 Share Posted March 1, 2007 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 More sharing options...
kenrbnsn Posted March 1, 2007 Share Posted March 1, 2007 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 https://forums.phpfreaks.com/topic/40780-problem-with-variable-variables/#findComment-197425 Share on other sites More sharing options...
artacus Posted March 1, 2007 Share Posted March 1, 2007 It's easier (and usually always better) to just use an array $p[$i] = $playername; echo $p[1]; Link to comment https://forums.phpfreaks.com/topic/40780-problem-with-variable-variables/#findComment-197426 Share on other sites More sharing options...
joebudden Posted March 1, 2007 Author Share Posted March 1, 2007 well that allows me to display the 6 rows but that wasn't what i asked... i need to have each row in its own variable so i can send each variable separately to FLASH Link to comment https://forums.phpfreaks.com/topic/40780-problem-with-variable-variables/#findComment-197441 Share on other sites More sharing options...
Barand Posted March 1, 2007 Share Posted March 1, 2007 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 https://forums.phpfreaks.com/topic/40780-problem-with-variable-variables/#findComment-197452 Share on other sites More sharing options...
joebudden Posted March 2, 2007 Author Share Posted March 2, 2007 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 https://forums.phpfreaks.com/topic/40780-problem-with-variable-variables/#findComment-197469 Share on other sites More sharing options...
Barand Posted March 2, 2007 Share Posted March 2, 2007 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 https://forums.phpfreaks.com/topic/40780-problem-with-variable-variables/#findComment-197470 Share on other sites More sharing options...
joebudden Posted March 2, 2007 Author Share Posted March 2, 2007 hehe they both work, thanks a million dude, u dont know how much that means to me!!! just for my information though, which is better to use ?? or which would u use ?? thanx again !! Link to comment https://forums.phpfreaks.com/topic/40780-problem-with-variable-variables/#findComment-197471 Share on other sites More sharing options...
Barand Posted March 2, 2007 Share Posted March 2, 2007 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 https://forums.phpfreaks.com/topic/40780-problem-with-variable-variables/#findComment-197473 Share on other sites More sharing options...
joebudden Posted March 2, 2007 Author Share Posted March 2, 2007 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 https://forums.phpfreaks.com/topic/40780-problem-with-variable-variables/#findComment-197513 Share on other sites More sharing options...
itsmeArry Posted March 2, 2007 Share Posted March 2, 2007 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 https://forums.phpfreaks.com/topic/40780-problem-with-variable-variables/#findComment-197607 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.