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
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
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
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
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
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
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.