Jump to content

Retrieve multiple values from dropdown list


biggieuk

Recommended Posts

Hi all,

 

Ill try explain my problem as much as i can, let me know if you need anything clearing up.

 

I have a dropdown box which is populated from 'users' table in mysql db:

 

     <select name="users" id="users" onchange="window.location.href='<?=$HTTP_SERVER_VARS['PHP_SELF']; ?>?userid='+this.options[this.selectedIndex].value+'&id=<?=$row_selectSessions['selections'];?> ';"> 
<?   
     if(mysql_num_rows($userlist)) 
     { 
     while($row_userlist = mysql_fetch_assoc($userlist)) 
     { ?>
      <option value="<?=$row_userlist['userid'];?>" <? if ($_GET['userid'] == $row_userlist['userid'] ){ echo "Selected"; } ?> ><?=$row_userlist['username'];?> - <?=$row_userlist['email'];?></option> 
  <? } 
  
     } 
     else {
     echo "<option>Nothing Found</option>";  
    } 
?>
</select>

 

When a user is selected the onChange property refreshes the current page and appends a '?userid=[value from dropdown item]?id=[users Selections, stored in the users database e.g 12,34,56,78 ]

 

These selections are also ids of items in the 'selections' table.

 

I have a php procedure in the head of my page which GETs the 'userid' from the querystring and sets the dropdown to the same value before the refresh. The procedure also GETs the 'id' value and uses 'explode' to split these up and individually retreive the Titles from the 'selections' table.

 

Ive managed to get this working ok, however as the page is being refreshed the 'id' in the URL is the previously selected and so the previous Selection Titles are returned.

 

Anybody know how i can get around this, hope ive not made it sound too complicated.

 

Thanks

 

 

Link to comment
Share on other sites

Hi,

 

Not sure why you would want to use the id as a GET variable. Why not use the UserId to look up the selections from the list within the script. GET vars should only be used when the user wants to communicate in a simple way with the webpage.

 

If I have misunderstood the problem I apologise.

 

Link to comment
Share on other sites

If it helps, my tables are laid out similar to this:

 

user table

userid    username    selections

1            Dan            2,5,6

2            Pete          1,3,5

 

 

selections table

id          title               

1        Option one

2        Option two

3        Option three

 

 

the dropdown value is the 'userid'.

 

I want to be able to select a user and have the 'title' of each session display.

 

thanks again

Link to comment
Share on other sites

Hi,

 

Not sure why you would want to use the id as a GET variable. Why not use the UserId to look up the selections from the list within the script. GET vars should only be used when the user wants to communicate in a simple way with the webpage.

 

If I have misunderstood the problem I apologise.

 

 

thanks for the reply,

 

The userid is in a different table to where the selections are stored. Is there a more complicated query that can read the userid, select the selections and read them from the 'selections' table?

Link to comment
Share on other sites

Ah, let me save you some time here as this is a trap that most early database adopters get into.

 

Your method of storing selections should instead utilise a "linkage table". This is a table normally with 2 columns which links one piece of data to another.

 

Therefore the best route is to create a table called:

 

user_selection

user_id              selection_id

1                            2

1                            5

1                            6

2                            1

2                            3

2                            5

 

That way you can then do a JOIN such as

 

SELECT a.*,c.* FROM user AS a INNER JOIN user_selection AS b ON a.user_id = b.user_id INNER JOIN selections AS c ON b.selection_id = c.id

 

and use

 

INSERT INTO user_selection (user_id,selection_id) VALUES (1,2)

 

when you are adding selections.

 

This is not designed to complete your script for you but if you use this method you'll save a lot of time on a lot of projects in the future.

 

Good luck

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.