Mutley Posted March 18, 2007 Share Posted March 18, 2007 I have a looping database query and want to create a variable for each one, there will only be 6 results from the query. I tried: $number = array(1,2,3,4,5,6); ...{ echo "$variable$number = $database_entry"; } But the array won't work, it's probably obviously wrong to you guys. Link to comment https://forums.phpfreaks.com/topic/43294-number-array/ Share on other sites More sharing options...
fert Posted March 18, 2007 Share Posted March 18, 2007 $count=1; while($row=mysql_fetch_array($result)) { $var="result".$count; $$var=$row['column']; $count++; } I'm pretty sure that's what you want to do Link to comment https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210210 Share on other sites More sharing options...
Mutley Posted March 18, 2007 Author Share Posted March 18, 2007 Doesn't appear to be working, I'm using a looping query list, like this: while (list($points) = mysql_fetch_row($res)) { echo "$points<br>"; } Link to comment https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210219 Share on other sites More sharing options...
Barand Posted March 19, 2007 Share Posted March 19, 2007 Why don't you put them into an array <?php $pointsArray = array(); while (list($points) = mysql_fetch_row($res)) { $pointsArray[] = $points; } // view array data foreach ($pointsArray as $p) echo "$p<br>"; ?> Link to comment https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210234 Share on other sites More sharing options...
Mutley Posted March 19, 2007 Author Share Posted March 19, 2007 I receive lots of: 1 1 1 I need it to display the $points with a new variable for it, so it would show as: $newvar1 = $points; $newvar2 = $points; $newvar3 = $points; $newvar4 = $points; But with a loop it would obvioulsy just be 1 of the above but I can't assign variables to it (as it would just loop the same number/variable). Link to comment https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210237 Share on other sites More sharing options...
fert Posted March 19, 2007 Share Posted March 19, 2007 http://us2.php.net/manual/en/language.variables.variable.php Link to comment https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210244 Share on other sites More sharing options...
Barand Posted March 19, 2007 Share Posted March 19, 2007 Then fert's earlier suggestion should work - read the link he provided <?php $count=1; while (list($points) = mysql_fetch_row($res)) { $var = 'newvar'.$count; $$var = $points; ++$count; } echo $newvar1. '<br>'; echo $newvar2. '<br>'; echo $newvar3. '<br>'; echo $newvar4. '<br>'; echo $newvar5. '<br>'; echo $newvar6. '<br>'; // if you had a 1000, would you still insist on this method? ?> Link to comment https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210253 Share on other sites More sharing options...
Mutley Posted March 19, 2007 Author Share Posted March 19, 2007 I got this to work: <?php $count=0; $p1_slot = 'p1_slot'; $sql = "SELECT u.user_id, u.slot, p.name, p.points FROM loadout u INNER JOIN products p ON u.prod_id = p.prod_id WHERE u.user_id = '1' ORDER BY u.user_id, u.slot"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); while (list($user, $slot, $prod, $points) = mysql_fetch_row($res)) { echo "$$p1_slot$count = $points<br>"; $count++; } ?> $p1_slot0 = 3 $p1_slot1 = 5 $p1_slot2 = 4 $p1_slot3 = 4 $p1_slot4 = 10 $p1_slot5 = 3 But if I wanted to use these variables in a part of the script further on, how would I do that? As at the moment they don't show in the code, only when echoed. Link to comment https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210401 Share on other sites More sharing options...
Mutley Posted March 19, 2007 Author Share Posted March 19, 2007 If I take away the echo to make them variables (not displayed as text) I get a unexpected variable message. Link to comment https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210504 Share on other sites More sharing options...
per1os Posted March 19, 2007 Share Posted March 19, 2007 <?php $count=0; $p1_slot = 'p1_slot'; $sql = "SELECT u.user_id, u.slot, p.name, p.points FROM loadout u INNER JOIN products p ON u.prod_id = p.prod_id WHERE u.user_id = '1' ORDER BY u.user_id, u.slot"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); while (list($user, $slot, $prod, $points) = mysql_fetch_row($res)) { $newVar = "p1_slot" . $count; $$newVar = $points<br>"; $count++; } ?> I thought barand pointed this out already, but here it is for a second time. Link to comment https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210506 Share on other sites More sharing options...
Mutley Posted March 19, 2007 Author Share Posted March 19, 2007 I got that to work. Can you link to any resources or reply how I would do this without listing the query results, just using something like $row['field'] instead? I've never used the "INNER JOIN" SELECT method before, so not sure how it exactly works. Link to comment https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.