Dynamic form from database

Thread Solved

Join Date: Sep 2007
Posts: 7
Reputation: dewhickey is an unknown quantity at this point 
Solved Threads: 0
dewhickey dewhickey is offline Offline
Newbie Poster

Dynamic form from database

 
0
  #1
Nov 28th, 2008
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:
  1. <?php
  2. $result = mysql_query("SELECT id, `first`, `last` FROM agents ORDER BY agents.id");
  3. ?>
  4.  
  5. <form name="Events" method="post" action="<?php echo $php_SELF;?>">
  6.  
  7. <table width="500" border="1" align="center" cellpadding="4" cellspacing="0">
  8. <tr>
  9. <td width="200" align="left">
  10. <select name="Person" id="Person">
  11. <option selected>Select</option>
  12.  
  13. <?php while ($row = mysql_fetch_array($result)){?>
  14. <option><?php echo $row['last'];?></option>
  15. <?php }?>
  16. </select>
  17. </td>
  18. </tr>
  19. </table>
  20. </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
Last edited by peter_budo; Nov 28th, 2008 at 5:11 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

Re: Dynamic form from database

 
0
  #2
Nov 28th, 2008
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.
  1. $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['fullname'];

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.
Last edited by Rhyan; Nov 28th, 2008 at 5:02 pm.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 7
Reputation: dewhickey is an unknown quantity at this point 
Solved Threads: 0
dewhickey dewhickey is offline Offline
Newbie Poster

Re: Dynamic form from database

 
0
  #3
Nov 28th, 2008
Thanks. That worked fine. Now, I just have to continue learning.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC