Jump to content

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.

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.