newbreed65 Posted October 5, 2008 Share Posted October 5, 2008 I currently struggling to get 2 fields (firstname, surname) from a database to populate in a list box I can do it easy with just one field like with my code example ive got it working with people_firstname but I just cant get it working with both of them to make a full name , any suggestions?? <?php foreach ($people as $people_id => $people_firstname ) { if ($people_id == $movie_director) { $selected = " selected"; } else { $selected = ""; } ?> <option value="<?php echo $people_id; ?>"<?php echo $selected; ?>> <?php echo $people_firstname; ?></option> <?php } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/ Share on other sites More sharing options...
budimir Posted October 5, 2008 Share Posted October 5, 2008 Try this: <?php foreach ($people as $people_id => "$people_firstname $people_lastname" ) { if ($people_id == $movie_director) { $selected = " selected"; } else { $selected = ""; } ?> <option value="<?php echo $people_id; ?>"<?php echo $selected; ?>> <?php echo $people_firstname; ?></option> <?php } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/#findComment-657476 Share on other sites More sharing options...
JasonLewis Posted October 5, 2008 Share Posted October 5, 2008 What does the $people array look like? How is it being populated? I'm guessing its like this: Array ( 0 => Bill, 1 => Bob, 2 => Jack ) When populating it you could make it like this: Array ( 0 => array(Bill, Anderson), 1 => array(Bob, Builder), 2 => array(Jack, Beanstalk) ) Then your code: <?php foreach ($people as $people_id => $people_array) { if ($people_id == $movie_director) { $selected = " selected"; } else { $selected = ""; } ?> <option value="<?php echo $people_id; ?>"<?php echo $selected; ?>> <?php echo $people_array[0]." ".$people_array[1]; ?></option> <?php } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/#findComment-657479 Share on other sites More sharing options...
newbreed65 Posted October 5, 2008 Author Share Posted October 5, 2008 thanx ProjectFear that sorta works its showing first letter and second letter of people_firstname i just need to declaire the fields better im getting J i B o S a when i want Jim Carrey Bob Random Sam Smith Quote Link to comment https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/#findComment-657487 Share on other sites More sharing options...
JasonLewis Posted October 5, 2008 Share Posted October 5, 2008 Yeah, you would. You need to post the code that builds your $people array. When you populate it you need to make the value for the keys an array containing the first and last name. Hope you can understand what I mean. If you don't, post the part of the code that makes the $people array. Quote Link to comment https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/#findComment-657488 Share on other sites More sharing options...
newbreed65 Posted October 5, 2008 Author Share Posted October 5, 2008 yes sorry in away i did get what you mean but because im just a beginner with PHP my head struggles to get round somethings $peoplesql = "SELECT * FROM people"; $result = mysql_query($peoplesql) or die("Invalid query: " . mysql_error()); while ($row = mysql_fetch_array($result)) { $people[$row['people_id']] = $row['people_firstname']; } <select name="movie_director"> <option value="" selected>Select a director...</option> <?php foreach ($people as $people_id => $people_array) { if ($people_id == $movie_director) { $selected = " selected"; } else { $selected = ""; } ?> <option value="<?php echo $people_id; ?>"<?php echo $selected; ?>> <?php echo $people_array[$people_firstname]." ".$people_array[1]; ?></option> <?php } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/#findComment-657493 Share on other sites More sharing options...
JasonLewis Posted October 5, 2008 Share Posted October 5, 2008 Okay try this: $peoplesql = "SELECT * FROM people"; $result = mysql_query($peoplesql) or die("Invalid query: " . mysql_error()); while ($row = mysql_fetch_array($result)) { $people[$row['people_id']] = array("firstname" => $row['people_firstname'], "lastname" => $row['people_lastname']); } <select name="movie_director"> <option value="" selected>Select a director...</option> <?php foreach ($people as $people_id => $people_array) { if ($people_id == $movie_director) { $selected = " selected"; } else { $selected = ""; } ?> <option value="<?php echo $people_id; ?>"<?php echo $selected; ?>> <?php echo $people_array['firstname']." ".$people_array['lastname']; ?></option> <?php } ?> EDIT, actually you don't need two loops. Try this, if it doesn't work I mucked up somewhere. In that case, use the above. $peoplesql = "SELECT * FROM people"; $result = mysql_query($peoplesql) or die("Invalid query: " . mysql_error()); ?> <select name="movie_director"> <option value="" selected>Select a director...</option> <?php while ($row = mysql_fetch_array($result)) { if($row['people_id'] == $movie_director){ $selected = " selected='selected'"; }else{ $selected = ""; } ?> <option value="<?php echo $row['people_id']; ?>"<?php echo $selected; ?>> <?php echo $row['people_firstname']." ".$row['people_lastname']; ?> </option> <?php } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/#findComment-657496 Share on other sites More sharing options...
newbreed65 Posted October 5, 2008 Author Share Posted October 5, 2008 yup thanx you very much Quote Link to comment https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/#findComment-657497 Share on other sites More sharing options...
JasonLewis Posted October 5, 2008 Share Posted October 5, 2008 Read my post again, you didn't need to use two loops. Quote Link to comment https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/#findComment-657499 Share on other sites More sharing options...
newbreed65 Posted October 5, 2008 Author Share Posted October 5, 2008 no sorry the second bit doesnt work thou it might be better to leave it with the first one since i have 2 list boxes that uses the first loop (actor listbox and director listbox) Quote Link to comment https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/#findComment-657502 Share on other sites More sharing options...
JasonLewis Posted October 5, 2008 Share Posted October 5, 2008 Okay, yeah no worries then. Stick with the first one. Quote Link to comment https://forums.phpfreaks.com/topic/127107-combining-2-fields-in-a-list-box/#findComment-657504 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.