biggieuk Posted November 20, 2008 Share Posted November 20, 2008 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 https://forums.phpfreaks.com/topic/133502-retrieve-multiple-values-from-dropdown-list/ Share on other sites More sharing options...
mtoynbee Posted November 20, 2008 Share Posted November 20, 2008 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 https://forums.phpfreaks.com/topic/133502-retrieve-multiple-values-from-dropdown-list/#findComment-694426 Share on other sites More sharing options...
biggieuk Posted November 20, 2008 Author Share Posted November 20, 2008 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 https://forums.phpfreaks.com/topic/133502-retrieve-multiple-values-from-dropdown-list/#findComment-694459 Share on other sites More sharing options...
biggieuk Posted November 21, 2008 Author Share Posted November 21, 2008 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 https://forums.phpfreaks.com/topic/133502-retrieve-multiple-values-from-dropdown-list/#findComment-695393 Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 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 https://forums.phpfreaks.com/topic/133502-retrieve-multiple-values-from-dropdown-list/#findComment-697522 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.