garethhall Posted May 24, 2009 Share Posted May 24, 2009 Hi I have a table with 3 fields in it optionID, optionName, optionPrice. I neew for loop though the rows a couple of time but mysql does not allow me to do that. So I am trying the read the values in to an array. I got the first part working but can't get the second par to work or shall I say the second value. I need to output the price as well. How can I add the extra value into the array? To be honest I really just don't know how. Please help <? $SQL_selectOptions = "SELECT * FROM options"; $rs_selectOptions = mysql_query($SQL_selectOptions, $admin); $row_selectOptions = mysql_fetch_assoc($rs_selectOptions); $optionsPrice = array(); do{$i++; $optionsName[$i] = $row_selectOptions['optionName']; }while($row_selectOptions = mysql_fetch_assoc($rs_selectOptions)); foreach($optionsName as $name){ echo $Name; } ?> Link to comment https://forums.phpfreaks.com/topic/159453-solved-a-little-help-with-an-array/ Share on other sites More sharing options...
wildteen88 Posted May 24, 2009 Share Posted May 24, 2009 This is what you'll want to do $SQL_selectOptions = "SELECT optionID, optionName, optionPrice FROM options"; $rs_selectOptions = mysql_query($SQL_selectOptions, $admin); $selectOptions = array(); while($row_selectOptions = mysql_fetch_assoc($rs_selectOptions)) { $selectOptions[] = $row_selectOptions; } All your results will be stored into the $selectOptions array. Now to display all your results foreach($selectOptions as $options) { echo $options['optionID'] .'<br />'; echo $options['optionName'] .'<br />'; echo $options['optionPrice'] . '<br /><br />'; } Link to comment https://forums.phpfreaks.com/topic/159453-solved-a-little-help-with-an-array/#findComment-841120 Share on other sites More sharing options...
jackpf Posted May 24, 2009 Share Posted May 24, 2009 Surely you don't need the while() loop. $SQL_selectOptions = "SELECT optionID, optionName, optionPrice FROM options"; $rs_selectOptions = mysql_query($SQL_selectOptions, $admin); $row_selectOptions = mysql_fetch_assoc($rs_selectOptions) Then $row_selectOptions will have all of the results in an array. Doesn't your while loop just kind of...put it into another array? Thus isn't needed. Forgive me if I am wrong Oh, and also, in your original code, $Name is not $name. You need to be careful with cases. Link to comment https://forums.phpfreaks.com/topic/159453-solved-a-little-help-with-an-array/#findComment-841130 Share on other sites More sharing options...
trq Posted May 24, 2009 Share Posted May 24, 2009 Surely you don't need the while() loop. $SQL_selectOptions = "SELECT optionID, optionName, optionPrice FROM options"; $rs_selectOptions = mysql_query($SQL_selectOptions, $admin); $row_selectOptions = mysql_fetch_assoc($rs_selectOptions) Then $row_selectOptions will have all of the results in an array. Doesn't your while loop just kind of...put it into another array? Thus isn't needed. Forgive me if I am wrong Oh, and also, in your original code, $Name is not $name. You need to be careful with cases. mysql_fetch_assoc returns one row each time it is called. So if you want all your results, you need a loop. Link to comment https://forums.phpfreaks.com/topic/159453-solved-a-little-help-with-an-array/#findComment-841132 Share on other sites More sharing options...
jackpf Posted May 24, 2009 Share Posted May 24, 2009 Ugh...just woke up. Ignore me. Link to comment https://forums.phpfreaks.com/topic/159453-solved-a-little-help-with-an-array/#findComment-841135 Share on other sites More sharing options...
garethhall Posted May 25, 2009 Author Share Posted May 25, 2009 Thanks it is working great now :) Link to comment https://forums.phpfreaks.com/topic/159453-solved-a-little-help-with-an-array/#findComment-841476 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.