Phpfr3ak Posted December 14, 2011 Share Posted December 14, 2011 In short i want to use the following code below, when someone selects there option and submits it, it would bring up details from the database on this user from the selected table, can you explain what it would be called doing this so i can look it up, Sorry to be a pain, Cheers. <select name="target2" id="target2"> <option value=""></option> <?php $sql = "SELECT player_id, friend_id, name, is_active FROM contacts as c JOIN players as p ON c.friend_id = p.id WHERE c.player_id = $playerID AND is_active = 1 ORDER BY name ASC"; $que = mysql_query($sql) or die(mysql_error()); while($list = mysql_fetch_array($que)) { ?> <option value="<?php echo $list['friend_id'] ?>"><?php echo $list['name'] ?></option> <?php } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/253157-advice-please/ Share on other sites More sharing options...
Psycho Posted December 14, 2011 Share Posted December 14, 2011 Um, a database-driven web page? What you are asking is not a single function or process. It is several functions/processes to achieve the desired goal. If you have a form and can post the form to another (or the same page). You just need to check for the value passed in the form. In this case yor script would check for the value of $_POST['target2'] (or $_GET['target2'] depending on the method you set for the form). From there, you would want to validate/sanitize the value and then run a select query for the information you want to display. Then create the logic to output the data in the manner you want it. Also, some advice on the code above: If you are only using 'friend_id' and 'name', then you only need to include those in the SELECT query. Plus, I always suggest separating the logic of your code from the presentation. So, I would put the logic to generate the options at the top of the script and then output the options in the display portion of the code at the bottom of the script (or even in a separate file). Lastly, if you need to generate multiple select lists using database queries it would be advantageous to create a function that you can pass a query to. My take <?php $sql = "SELECT friend_id, name FROM contacts AS c JOIN players AS p ON c.friend_id = p.id WHERE c.player_id = $playerID AND is_active = 1 ORDER BY name ASC"; $que = mysql_query($sql) or die(mysql_error()); $target2Options = ''; while($list = mysql_fetch_array($que)) { $target2Options .= "<option value='{$list['friend_id']}'>{$list['name']}</option>\n"; } ?> <select name="target2" id="target2"> <option value=""></option> <?php echo $target2Options; ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/253157-advice-please/#findComment-1297854 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.