943,910 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1716
  • PHP RSS
Nov 28th, 2008
0

Dynamic form from database

Expand Post »
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 Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dewhickey is offline Offline
7 posts
since Sep 2007
Nov 28th, 2008
0

Re: Dynamic form from database

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.
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Nov 28th, 2008
0

Re: Dynamic form from database

Thanks. That worked fine. Now, I just have to continue learning.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dewhickey is offline Offline
7 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Problem with where clause or ....??
Next Thread in PHP Forum Timeline: How to set up a search that uses checkboxes?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC