Jump to content

retrieving an HTML dropdown select


phppup

Recommended Posts

  • Replies 73
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

That was the first thinig I did.  I mean, I'm not totally ignorant.  I switched a few numbers and this is what I have now:

echo "<tr><td>";

echo "<select name='roastturkey'>";

echo        "<option value='0.00'  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">0</option>";

echo        "<option value='1.00'  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">1</option>";

echo        "<option value='2.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">2</option>";

echo        "<option value='3.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">3</option>";

echo        "<option value='4.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">4</option>";

echo        "<option value='5.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">5</option>";

echo        "<option value='6.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">6</option>";

echo        "<option value='7.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">7</option>";

echo        "<option value='8.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">8</option>";

echo        "<option value='9.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">9</option>";

echo        "<option value='10.00'>  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">10</option>";

echo        "      </select></td><td rowspan='2'>ROAST TURKEY</span></td><td rowspan='2'>7.00 LB</span></td> ";

 

The drop down shows as follows:

0

1

selected="selected">2

selected="selected">3

selected="selected">4... etc.

 

And the database quierie after connecting is:

 

$result = mysql_query("SELECT * FROM pass WHERE id = '4' ")

 

Record 4 has 10 as the value for the 'roastturkey'.

I've changed it to record 5 which has a value of 5, but the dropdown does NOT change.

 

Link to comment
Share on other sites

You need to check that the value matches the value of the option tags.

echo        "<option value='0.00'  " . ($array['roastturkey'] == '0.00' ? 'selected="selected"' : '') . ">0</option>";
echo        "<option value='1.00'  " . ($array['roastturkey'] == '1.00' ? 'selected="selected"' : '') . ">1</option>";
echo        "<option value='2.00'> " . ($array['roastturkey'] == '2.00' ? 'selected="selected"' : '') . ">2</option>";
echo        "<option value='3.00'> " . ($array['roastturkey'] == '3.00' ? 'selected="selected"' : '') . ">3</option>";

Link to comment
Share on other sites

OKAY, the dropdown is now populated with number from 0 thru 10.

 

The database quierie after connecting is:

 

$result = mysql_query("SELECT * FROM pass WHERE id = '4' ")

 

Record 4 has 10 as the value for the 'roastturkey'.

I've changed it to record 5 which has a value of 5,

 

Yet the dropdown STILL does NOT change to reflect the stored value in the database  :-(

 

 

Link to comment
Share on other sites

I've been refreshing ALL day.

 

 

 

I have a dropdwon menu coded as follows"

echo "<tr><td>";

echo "<select name='roastturkey'>";

echo        "<option value='0.00'  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">0</option>";

echo        "<option value='1.00'  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">1</option>";

echo        "<option value='2.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">2</option>";

echo        "<option value='3.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">3</option>";

echo        "<option value='4.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">4</option>";

echo        "<option value='5.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">5</option>";

echo        "<option value='6.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">6</option>";

echo        "<option value='7.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">7</option>";

echo        "<option value='8.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">8</option>";

echo        "<option value='9.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">9</option>";

echo        "<option value='10.00'>  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">10</option>";

echo        "      </select></td><td rowspan='2'>ROAST TURKEY</span></td><td rowspan='2'>7.00 LB</span></td> ";

 

 

And a database quierie after connecting is:

 

$result = mysql_query("SELECT * FROM pass WHERE id = '4' ")

 

I want it to get the info from the database so that the option that is saved in the database will be new default when the form page is loaded, and will SHOW in the form.

 

If I don't change it to the previous info it will update the database with the default option rather then the actual option when re-submitted.

 

 

Record 4 has 10 as the value for the 'roastturkey'.

I've changed it to record 5 which has a value of 5, but the dropdown does NOT change.

 

Link to comment
Share on other sites

You still haven't changed the comparison value.

echo        "<option value='0.00'  " . ($array['roastturkey'] == '0.00' ? 'selected="selected"' : '') . ">0</option>";
echo        "<option value='1.00'  " . ($array['roastturkey'] == '1.00' ? 'selected="selected"' : '') . ">1</option>";
echo        "<option value='2.00'> " . ($array['roastturkey'] == '2.00' ? 'selected="selected"' : '') . ">2</option>";
echo        "<option value='3.00'> " . ($array['roastturkey'] == '3.00' ? 'selected="selected"' : '') . ">3</option>";
echo        "<option value='4.00'> " . ($array['roastturkey'] == '4.00' ? 'selected="selected"' : '') . ">4</option>";
echo        "<option value='5.00'> " . ($array['roastturkey'] == '5.00' ? 'selected="selected"' : '') . ">5</option>";
echo        "<option value='6.00'> " . ($array['roastturkey'] == '6.00' ? 'selected="selected"' : '') . ">6</option>";
echo        "<option value='7.00'> " . ($array['roastturkey'] == '7.00' ? 'selected="selected"' : '') . ">7</option>";
echo        "<option value='8.00'> " . ($array['roastturkey'] == '8.00' ? 'selected="selected"' : '') . ">8</option>";
echo        "<option value='9.00'> " . ($array['roastturkey'] == '9.00' ? 'selected="selected"' : '') . ">9</option>";
echo        "<option value='10.00'>  " . ($array['roastturkey'] == '10.00' ? 'selected="selected"' : '') . ">10</option>";

Link to comment
Share on other sites

You've been given MANY examples.

<?php
//It is assumed that you have the table field called `roastturkey` and holds a numeric value and you are querying table `pass` with test id 4.
$query = "SELECT roastturkey FROM pass WHERE id = '4'";
$result = mysql_query($query);
$array = mysql_fetch_assoc($result);

$values = range(0, 10);
echo "<select name=\"my_field\">\n";
foreach($values as $v) {
echo  "<option value=\"$v\"  " . ($array['roastturkey'] == $v ? 'selected="selected"' : '') . ">$v</option>";
}
echo "</select>\n";
?>

Link to comment
Share on other sites

Correct!  Although the dropdown is hardcoded as:

echo        "<option value='0.00'  " . ($array['roastturkey'] == '0.00' ? 'selected="selected"' : '') . ">0</option>";echo        "<option value='1.00'  " . ($array['roastturkey'] == '1.00' ? 'selected="selected"' : '') . ">1</option>";echo        "<option value='2.00'> " . ($array['roastturkey'] == '2.00' ? 'selected="selected"' : '') . ">2</option>";echo        "<option value='3.00'> " . ($array['roastturkey'] == '3.00' ? 'selected="selected"' : '') . ">3</option>";echo        "<option value='4.00'> " . ($array['roastturkey'] == '4.00' ? 'selected="selected"' : '') . ">4</option>";echo        "<option value='5.00'> " . ($array['roastturkey'] == '5.00' ? 'selected="selected"' : '') . ">5</option>";echo        "<option value='6.00'> " . ($array['roastturkey'] == '6.00' ? 'selected="selected"' : '') . ">6</option>";echo        "<option value='7.00'> " . ($array['roastturkey'] == '7.00' ? 'selected="selected"' : '') . ">7</option>";echo        "<option value='8.00'> " . ($array['roastturkey'] == '8.00' ? 'selected="selected"' : '') . ">8</option>";echo        "<option value='9.00'> " . ($array['roastturkey'] == '9.00' ? 'selected="selected"' : '') . ">9</option>";echo        "<option value='10.00'>  " . ($array['roastturkey'] == '10.00' ? 'selected="selected"' : '') . ">10</option>";

 

So when I test the script it appears to work and shows 10 as the quntity for roastturkey.

 

Yet when I change the quiery to a differnt ID=x, the quantity of 10 remains (as if frozen).

The DB has a variety of qunatities in it with varying IDs just for testing.

So the script FAILS because the quantity never changes from 10.

 

Ideas???

Link to comment
Share on other sites

DON'T HARD CODE OPTIONS.  In my example, the number of selection options is defined here.

$values = range(0, 10);

If you need to COUNT the number of options for a particular product then run a separate query to do so then change the number of options.  Not knowing where or how you are storing this information makes it impossible to give working answer but it would be something like this.

 

$countturkey = mysql_query("SELECT COUNT(*) as ct FROM products WHERE item='turkeybreast'");
WHILE($cntturkey = mysql_fetch_array($countturkey)) {
$ct = $cntturkey['COUNT(ct)'];
}

$values = range(0, $ct);

 

The "Selected" option should be showing based on query with the user `id`, which I believe we all thought was the problem you were having.

Link to comment
Share on other sites

You need to READ the 3 pages prior.  NOTHING is being COUNTED.

 

There is data in a DB. I can easily view it in a browser.  Now I want to have the same data populated into the numerous dropdown selections so the information resembles that which was ORIGINALLY submitted AND become the NEW DEFAULT VALUES so that THIS form can be submitted to UPDATE or CHANGE any of the data.

 

That's the entire project.

Link to comment
Share on other sites

You need to READ the 3 pages prior.  NOTHING is being COUNTED.

 

No, YOU need to take a step back and understand what is being said. You have a select list of 10 options where the only difference is that the value/display is going from 0.00/0 to 10.00/10. Hard coding those options is just plain stupid. Drumming provided some code that would dynamically create those options AND auto-select the appropriate option based upon the previously selected value. Which you were told to do back on page 1 of this thread.

 

You OBVIOUSLY didn't even try it. However, there was one difference in what his code would produce and what you currently have. The values would be 0, 1, 2 ... instead of 0.00, 1.00, 2.00, if that is really necessary that can be easily fixed. Here is my suggestion

 

//Get the ID of the record being displayed
//Variable name is just an example, replace with whatever you are using
$recID = (int) $_GET['id'];

//Get the currently selected value from the DB based upon the record
//-- This assumes you need all the data for the record to display on the page
//-- otherwise you would change the query for only the fields you need
//-- The value of 'roastturkey' for the record will be used to auto-select the correct option
$query = "SELECT * FROM pass WHERE id = '4'";
$result = mysql_query($query);
$record = mysql_fetch_assoc($result);

//Create a loop to generate ALL the select options using the value of
//$recod['roastturkey'] to auto-select the record's current value
$optionValues = range(0, 10); //Create an array of values from 0 to 10
echo "<select name=\"my_field\">\n";
foreach($optionValues as $val)
{
    //Create a selected variable to set for each option
    $selected = ($val==$record['roastturkey']) ? ' selected="selected"' : '';
    $decVal = number_format($val, 2); //Format value with decimals if required
    echo  "<option value='{$decVal}'{$selected}>{$val}</option>\n";
}
echo "</select>\n";

Link to comment
Share on other sites

FULL STORY REPEATED:

 

A form inputs data into several fields named turkey, roastbeef, spinach, potatoes, etc (i'll stop bc it's lunchtime now... LOL).  Input process is FINE.

 

Now DB has several entries with varying quantiities in each.

Record A has 5 (orders) of turkey and 2 spinach

Record B has 3 (orders) of roast beef and 1 spinach, and 2 potaoes, etc.

Any item NOT ordered has a DEFAULT of ZERO.

 

I can successfully view all orders in a browser... NO PROBLEM.

 

Now I have a SECOND form (identical to the first) and I want to ECHO the values into it.

 

I want those values to appear INSIDE the dropdown with the info from the database so that the option that is saved in the database will be new default when the form page is loaded, and will SHOW in the form so that if customer A decides that 5 orders of turkey is too much, then the order can be UPDATED to the DB with the NEW VALUE.

 

If I don't change it to the previous info it will update the database with the default option rather then the actual option when re-submitted.

 

My SECOND form has a dropdwon menu coded as follows"

echo "<tr><td>";

echo "<select name='roastturkey'>";

echo        "<option value='0.00'  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">0</option>";

echo        "<option value='1.00'  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">1</option>";

echo        "<option value='2.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">2</option>";

echo        "<option value='3.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">3</option>";

echo        "<option value='4.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">4</option>";

echo        "<option value='5.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">5</option>";

echo        "<option value='6.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">6</option>";

echo        "<option value='7.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">7</option>";

echo        "<option value='8.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">8</option>";

echo        "<option value='9.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">9</option>";

echo        "<option value='10.00'>  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">10</option>";

echo        "      </select></td><td rowspan='2'>ROAST TURKEY</span></td><td rowspan='2'>7.00 LB</span></td> ";

 

and I am currently using the statement:

 

$result = mysql_query("SELECT * FROM mytablename WHERE id = '5' ") just to get a value for testing purposes.

 

Then I change the ID within the statement to check the functionality.

 

At first, there was NO SUCCESS.  Yesterday, I got the dropdown's value to change to 10 (which was valid).

 

But NOW the dropdown has 'adopted' 10 as the default, as it does NOT CHANGE when I alter the ID value to orders that contain a value that is NOT 10 in them.

 

Any solutions?

 

 

 

 

 

Link to comment
Share on other sites

echo        "<option value='0.00'  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">0</option>";

echo        "<option value='1.00'  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">1</option>";

echo        "<option value='2.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">2</option>";

echo        "<option value='3.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">3</option>";

echo        "<option value='4.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">4</option>";

echo        "<option value='5.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">5</option>";

echo        "<option value='6.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">6</option>";

echo        "<option value='7.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">7</option>";

echo        "<option value='8.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">8</option>";

echo        "<option value='9.00'> " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">9</option>";

echo        "<option value='10.00'>  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">10</option>";

 

And were you not told that you need to change comparison values for each of these options?

If you are storing values in DB as 7.00 for example, the HARDCODED line would be.

echo        "<option value='7.00'> " . ($array['roastturkey'] == '7.00' ? 'selected="selected"' : '') . ">7</option>";

Link to comment
Share on other sites

I've changed it from: echo        "<option value='0.00'  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">0</option>";

echo        "<option value='1.00'  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">1</option>";

echo        "<option value='2.00' " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">2</option>";

echo        "<option value='3.00' " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">3</option>";

echo        "<option value='4.00' " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">4</option>";

echo        "<option value='5.00' " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">5</option>";

echo        "<option value='6.00' " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">6</option>";

echo        "<option value='7.00' " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">7</option>";

echo        "<option value='8.00' " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">8</option>";

echo        "<option value='9.00' " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">9</option>";

echo        "<option value='10.00'  " . ($array['roastturkey'] == 0 ? 'selected="selected"' : '') . ">10</option>";

 

to

 

echo        "<option value='0.00'  " . ($array['roastturkey'] == '0' ? 'selected="selected"' : '') . ">0</option>";

echo        "<option value='1.00'  " . ($array['roastturkey'] == '1' ? 'selected="selected"' : '') . ">1</option>";

echo        "<option value='2.00' " . ($array['roastturkey'] == '2' ? 'selected="selected"' : '') . ">2</option>";

echo        "<option value='3.00' " . ($array['roastturkey'] == '3' ? 'selected="selected"' : '') . ">3</option>";

echo        "<option value='4.00' " . ($array['roastturkey'] == '4' ? 'selected="selected"' : '') . ">4</option>";

echo        "<option value='5.00' " . ($array['roastturkey'] == '5' ? 'selected="selected"' : '') . ">5</option>";

echo        "<option value='6.00' " . ($array['roastturkey'] == '6' ? 'selected="selected"' : '') . ">6</option>";

echo        "<option value='7.00' " . ($array['roastturkey'] == '7' ? 'selected="selected"' : '') . ">7</option>";

echo        "<option value='8.00' " . ($array['roastturkey'] == '8' ? 'selected="selected"' : '') . ">8</option>";

echo        "<option value='9.00' " . ($array['roastturkey'] == '9' ? 'selected="selected"' : '') . ">9</option>";

echo        "<option value='10.00'  " . ($array['roastturkey'] == '10' ? 'selected="selected"' : '') . ">10</option>";

 

and now I get ZERO for all the ID's that I query.

 

The INITIAL default value is ZERO.  Then the value changes when the data is submitted though the form initially and posted into the DB.

 

Does the value that is input need to be coompared.  Does the hardcoding need to be altered for every instance AFFTER the data is recorded?

Link to comment
Share on other sites

See how you have the option "value" listed as 1.00, 2.00 etc?  Is that how values are stored in DB?

Then the comparison needs to match.

 

Does 5.00 == '5' ? 

It could in some cases but you want to look for exact match as in 5.00 == '5.00'.

echo        "<option value='5.00' " . ($array['roastturkey'] == '5.00' ? 'selected="selected"' : '') . ">5</option>";

Link to comment
Share on other sites

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.