Jump to content

HowTo Dynamic Select Menus With PHP, and remembered last option set.


itsrien

Recommended Posts

I have a question about Dynamic Select Menus With PHP.

De following I like to getting work:

I have two tables: one is categories the other is named links. Now I have a edit page so I can edit the links. On that edit page I have a php pull down menu where I can change the category. This already working, the only problem is that it doesn't remember the last option I set. When I reload the page it always shows the first (default) option, from the selection list. How do I change the script, so the pull down menu remembers the last set option or load the right value from the database.

Bellow I attached the code:

Thanks in advance: Rien

 

//SELECT AND EDIT CATEGORY

$sql = "SELECT * FROM tb_links";

$link_query = mysql_query($sql);

$link = mysql_fetch_array($link_query);



$sql = "SELECT * FROM tb_categories ORDER BY catname";

$category_query = mysql_query($sql);



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

while( $category = mysql_fetch_array($category_query) ){

if( $link['catparent'] == $category['ID'] ){ $selected = 'selected="selected"'; }

echo '<option value="'.$category['ID'].'"'.$selected.'>'.$category['catname'].'</option>';

}

echo '</select><br>';

//SELECT AND EDIT CATEGORY

Link to comment
Share on other sites

Looks like what you were trying to achieve here:

 

if( $link['catparent'] == $category['ID'] ){ $selected = 'selected="selected"'; }

 

Your problem may lie in the fact that there's no space after the value and before the word selected.  Try:

 

if( $link['catparent'] == $category['ID'] ){ $selected = ' selected="selected"'; }

Link to comment
Share on other sites

Thank you so much for the answers already, but unfortunately the change didn't bring me the result I had hoped for. The result is still posted correctly to the database, the only thing still not working is that the edit page pull down menu not showing the right value. Every time the page reload, it shows the default value.

So for example: I have a link in the database called

url: http://sports.com

title: sports

catname: sport

 

When I submit the page, the data is stored correctly in de database. But when I reload the edit page again, it doesn't load the right value from the database. And only the default value (in this case it is cars) is showed. I can choose the right value, but I would like the correct value is showed.

 

Bellow an example of the tables in the database:

 

Table: links

ID url title catparent

1 http:// buddy 3

2 http:// sports 2

3 http:// cars 1

 

Table: categories

ID catname

1 cars

2 sport

3 friends

 

The field catname from de table categories, contained the desired values for the pulldown menu.

Bellow I attached the changed code, what do I need to change more to get it work?

 

//SELECT AND EDIT CATEGORY 
$sql = "SELECT * FROM links"; 
$link_query = mysql_query($sql); 
$link = mysql_fetch_array($link_query); 

$sql = "SELECT * FROM categories ORDER BY catname"; 
$category_query = mysql_query($sql); 

echo '<select name="category">'; 
while( $category = mysql_fetch_array($category_query) ){ 
if( $link['catparent'] == $category['ID'] ){ $selected = ' selected="selected"'; } 
echo '<option value="'.$category['ID'].'"'.$selected.'>'.$category['catname'].'</option>'; 
} 
echo '</select><br>'; 
//SELECT AND EDIT CATEGORY

 

 

Thanks in advance: Rien

Link to comment
Share on other sites

Well your code makes no sense to me.  You're pulling the first entry from the links table (rather than linking through), and you don't have a where clause or anything.  But from the second table, you're pulling everything, and you're checking each one of them against the first link.  That's what your code does.  Additionally, let's say that they do match on the first iteration (i.e. the first result returned from the second table matches the result from the first table) all of the other 3 selections will be marked "selected" as well, because you don't clear the $selected variable at the end of each loop.  If the selected variable is set on the first iteration, it'll remain set on the second, third, fourth, and so on.  Add an unset($selected) or $selected = '' to the end of the loop, and explain the first part of the code to me, and maybe we can shed some light on this.

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.