Jump to content

[SOLVED] Scratching my head over this one... Select menu


suttercain

Recommended Posts

Hi guys,

 

The below code is used to edit a record. The select menu should automatically select the record that is in the MySQL table... so if 'Modern' is in the table... 'Modern' will already be selected.

 

Here is the code for two select menu... one HTML version which just shows 'Select One' on page load, and then a PHP version THAT WORKS...

 

<select name="ud_age">
	<option value='0' selected>Select One</option>
	<option value='Golden'>Golden</option>
	<option value='Silver'>Silver</option>
	<option value='Bronze'>Bronze</option>
	<option value='Modern'>Modern</option>
        </select>


        <?php
$TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern");

echo "<select name='ud_age'>";
foreach($TheList as $K => $V)
{
  if ($V == $age) {
  $sel = "selected";
  } else {
  $sel = "";
  }
  echo "<option value='$V' $sel>$K</option>";
}
echo "</select>";
?>

 

So when I remove the HTML select menu, because I am trying to replace it with the dynamic one, it no longer works... it no longer selects the item in the MySQL cell, it now defaults to the 'Select Twe'

 

What happening when I take out the HTML version? I can't get it..

 

Thanks

Link to comment
Share on other sites

This works fine for me

 

<?php

$TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern");
$age = 'Silver';

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

foreach($TheList as $K => $V){


  if ($V == $age) {
     $sel = "selected";
  } else {
     $sel = "";
  }

  echo "<option value='$V' $sel>$K - $V</option>";
  
}
echo "</select>";


?>

 

Check your value from the database ($age).

Link to comment
Share on other sites

Hi guys,

 

Thanks for the replies. I echoed $age and it printed Modern. I checked the value in the specific column 'age' and it also states Modern.

 

Like, I said works fine when I leave the HTML select menu in... but when I remove it, it stops working. Very weird

Link to comment
Share on other sites

can we rewrite your array a bit, you should really use a Integer or ENum in mysql as its less storage, I used in this case an integer in mysql with the array acting like enum

$TheList = array("Select Twe", "Golden", "Silver", "Bronze", "Modern");
echo "<select name='ud_age'>";
foreach($TheList as $key => $value){
echo "<option value=\"".$key."\" ";
if ($value == $age) {
echo  "selected=\"selected\"";
}
echo ">".$value."</option>";
}
echo "</select>";
?>

cuts down some storage space

 

Link to comment
Share on other sites

Hi cooldude, thanks for taking sometime with to post that code.

 

I tried this

 

$TheList = array("Select Twe", "Golden", "Silver", "Bronze", "Modern");
$age = 2;
echo "<select name='ud_age'>";
foreach($TheList as $key => $value){
echo "<option value=\"".$key."\" ";
if ($value == $age) {
echo  "selected=\"selected\"";
}
echo ">".$value."</option>";
}
echo "</select>";

 

and the select menu still went to Select Twe

Link to comment
Share on other sites

Sure,

 

Also I have the doctype at HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html lang="en">

 

Here is the source code in FireFox

 

<select name="ud_age"><option value="0">Select Twe</option><option value="1">Golden</option><option value="2">Silver</option><option value="3">Bronze</option><option value="4">Modern</option></select>

Link to comment
Share on other sites

yeah its not reverting back to the first because its selected its cause its first that is all.  my method isn't going to work unless you change it all to integers so use this, and don't convert age keep it text based for now to prove it works, then you should switch em all to integer or enum

<?php
$age = "Golden";
$TheList = array("Select Twe", "Golden", "Silver", "Bronze", "Modern");
echo "<select name='ud_age'>";
foreach($TheList as $key => $value){
echo "<option value=\"".$key."\" ";
if ($value == $age) {
echo  "selected=\"selected\"";
}
echo ">".$value."</option>";
}
echo "</select>";
?>

Link to comment
Share on other sites

Thinking maybe the mornign group may have an answer as to why this works okay when I leave the HTML version in the php file, but when I remove it, it no longer works. Here is the full code and the URL:

 

<?php
require ('includes/get_connected.php');

$query = "SELECT * FROM comics WHERE comic_id= '45'";
$result = mysql_query($query);
$num = mysql_num_rows($result);

$i = 0;
while ($i<$num) {
$age = mysql_result($result, $i, 'age');
?>

<select name="ud_age">
	<option value='0' selected>Select One</option>
	<option value='Golden'>Golden</option>
	<option value='Silver'>Silver</option>
	<option value='Bronze'>Bronze</option>
	<option value='Modern'>Modern</option>
        </select>


        <?php
$TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern");

echo "<select name='ud_age'>";
foreach($TheList as $K => $V)
{
  if ($V == $age) {
  $sel = "selected";
  } else {
  $sel = "";
  }
  echo "<option value='$V' $sel>$K</option>";
}
echo "</select>";
++$i;
}

?>

 

Here is the results. Notice how the HTML precedes the Dynamic list which work, I remove the HTML it stops working.

 

http://www.supermandatabase.com/select.php

 

I wanted to add this in case it helps discover the problem...

 

Source Code with HTML Select Box left in:

<select name="ud_age">
	<option value='0' selected>Select One</option>
	<option value='Golden'>Golden</option>
	<option value='Silver'>Silver</option>
	<option value='Bronze'>Bronze</option>
	<option value='Modern'>Modern</option>
        </select>


        <select name='ud_age'>
	<option value='0' selected>Select Twe</option>
	<option value='Golden' >Golden</option>
	<option value='Silver' >Silver</option>
	<option value='Bronze' >Bronze</option>
	<option value='Modern' selected>Modern</option>
</select>

 

Source code with only the dynamic select box left in:


<select name='ud_age'>
<option value='0'>Select Twe</option>
<option value='Golden' >Golden</option>
<option value='Silver' >Silver</option>
<option value='Bronze' >Bronze</option>
<option value='Modern' selected>Modern</option>
</select>

 

What is weird is how when I leave the html code in, the selected appears twice in the dynamic menu (see first source code).

Link to comment
Share on other sites

Okay, try this code and post us the link to where we can view it as well.

 

<?php

$TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern");

echo "<select name='ud_age'>";
foreach($TheList as $K => $V)
{
  if ($V == $age) {
  $sel = "selected";
  } else {
  $sel = "";
  }
  echo "<option value='$V' $sel>$K - $V - $age</option>";
}
echo "</select>";
++$i;
}

?>

Link to comment
Share on other sites

Hi poco,

 

This is the entire code

<?php
require ('includes/get_connected.php');

$query = "SELECT * FROM comics WHERE comic_id= '45'";
$result = mysql_query($query);
$num = mysql_num_rows($result);

$i = 0;
while ($i<$num) {
$age = mysql_result($result, $i, 'age');
?>

<select name="ud_age">
	<option value='0' selected>Select One</option>
	<option value='Golden'>Golden</option>
	<option value='Silver'>Silver</option>
	<option value='Bronze'>Bronze</option>
	<option value='Modern'>Modern</option>
        </select>


        <?php
$TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern");

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

foreach($TheList as $K => $V)
{
  if ($V == $age) {
  $sel = "selected";
  } else {
  $sel = "";
  }
  echo "<option value='$V' $sel>$K</option>";
}
echo "</select>";



++$i;
}

?>

 

Notice $age = mysql_result($result, $i, 'age'); in the code. When I echo $age it echoes modern. In your code no variable to age was set.

Link to comment
Share on other sites

Here is your code, but I added the $age variable to "Golden"

 

<?php

$TheList = array( "Select Twe" => 0, "Golden" => "Golden", "Silver" => "Silver", "Bronze" => "Bronze", "Modern" => "Modern");
$age = "Golden";
echo "<select name='ud_age'>";
foreach($TheList as $K => $V)
{
  if ($V == $age) {
  $sel = "selected";
  } else {
  $sel = "";
  }
  echo "<option value='$V' $sel>$K - $V - $age</option>";
}
echo "</select>";


?>

 

The output is here:

http://www.supermandatabase.com/selectpoco.php

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.