Jump to content

[SOLVED] A little help with an array


garethhall

Recommended Posts

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

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

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.

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.

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.