I have code to propagate a drop down list that displays people's names. The problem is that the first name and last name are separate fields in the database, and I can only get the drop down list to display one of them. Here's my code:

<?php 
$result = mysql_query("SELECT id, `first`, `last` FROM agents ORDER BY agents.id");
?>

<form name="Events" method="post" action="<?php echo $php_SELF;?>">

<table width="500" border="1" align="center" cellpadding="4" cellspacing="0">
<tr>
<td width="200" align="left">
<select name="Person" id="Person">
<option selected>Select</option>

<?php while ($row = mysql_fetch_array($result)){?>
<option><?php echo $row['last'];?></option>
<?php }?>
</select>
</td>
</tr>
</table>
</form>

If I run this in my browser, I get a list of last names. How can I get both the first names and the last names?

Thanks

Recommended Answers

All 2 Replies

Member Avatar for Rhyan

Depends on what you expect to happen next. You can act both in PHP, manipulating the returned array result, or you can change your SQL so that sql returns the first and last name as one column, respectively as one value.

I would go with mysql change of the select statement like this.

$result = mysql_query("SELECT id, concat(first, ' ',last) as fullname FROM agents ORDER BY agents.id");

This code will return the first and last names with space between them.
Then you can access the fullname as associative array, using e.g.
$result_from_query;

Try and advise if it works for you.
Also advise if you shall need to separate the names again e.g. during submit or something.

Thanks. That worked fine. Now, I just have to continue learning.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.