Jump to content

Php Dropdown - Selected?


mikey3521

Recommended Posts

Hello, Right now i'm using this to pull information from a database and display it in a dropdown list. The problem is I am using the name code when I goto the "Update" page, but i'm not sure how to add the "selected" field, that way what ever I saved in the database will load.

 

Example: I goto add and it add's the "id" of 7 to the field, when I load the page again it auto's back to 1 instead of clicking in-to 7 as thats whats already there... = ) If anyones got a quick fix I would greatly appreciate that. THanks.

 

 

 

<?php
include("connect.php");
$query="SELECT catagory_name,id FROM catalog_categories";

$result = mysql_query ($query);
echo "<select name='catagory_parent' value=''>Student Name</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[catagory_name]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>

Link to comment
https://forums.phpfreaks.com/topic/71971-php-dropdown-selected/
Share on other sites

Add a SELECTED value if there is a match:

 

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]";
if ($nt[id] == $selected_id) {
     echo " SELECTED";
}
echo ">$nt[catagory_name]</option>";
/* Option values are added by looping through the array */
}

Link to comment
https://forums.phpfreaks.com/topic/71971-php-dropdown-selected/#findComment-362512
Share on other sites

Try this

 

<?
			  $sql = "SELECT * FROM table ORDER BY `ID` DESC";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        echo "<option value=\"{$row['Field from Table']}\">{$row['Field from Table']}</option>";
      }
    }
  }
  ?>

 

Brett

Link to comment
https://forums.phpfreaks.com/topic/71971-php-dropdown-selected/#findComment-362516
Share on other sites

hmm thank you both for your solution, neither of them worked though perhaps I didn't make myself clear with what I want to happen.

 

Right now the Dropdown box looks like this when I load it

 

-1 -(default selected)

-2

-3

-4

-5

 

Always in that order. If I select 5 and save it to the database.... when I load the script again, I want the default to be 5 so now it will show 5 in the dropdown as the default instead of 1...

Link to comment
https://forums.phpfreaks.com/topic/71971-php-dropdown-selected/#findComment-362530
Share on other sites

You're going to have to store the last value selected in some manner.  If you want this to persiste between visits to the website, you're going to have to store it in the database somewhere.  If you only want it to work per visit to the website, do what BlueSkyIS suggested, but base it on the POST or GET data.

Link to comment
https://forums.phpfreaks.com/topic/71971-php-dropdown-selected/#findComment-362542
Share on other sites

  • 1 year later...

hmm thank you both for your solution, neither of them worked though perhaps I didn't make myself clear with what I want to happen.

 

Right now the Dropdown box looks like this when I load it

 

-1 -(default selected)

-2

-3

-4

-5

 

Always in that order. If I select 5 and save it to the database.... when I load the script again, I want the default to be 5 so now it will show 5 in the dropdown as the default instead of 1...

:P
Link to comment
https://forums.phpfreaks.com/topic/71971-php-dropdown-selected/#findComment-840669
Share on other sites

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.