| | |
Dynamic form from database
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 7
Reputation:
Solved Threads: 0
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:
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
php Syntax (Toggle Plain Text)
<?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
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.
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.
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.
I would go with mysql change of the select statement like this.
PHP Syntax (Toggle Plain Text)
$result = mysql_query("SELECT id, concat(first, ' ',last) as fullname FROM agents ORDER BY agents.id");
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
I miss my mind the most...."
Mark Twain
![]() |
Similar Threads
- updating multiple rows with one form (PHP)
- PHP Dynamic Form HELP! (PHP)
- dynamic row height (PHP)
- Storing dynamic form values in Arrays for display & insert (PHP)
- dynamic tables (ASP.NET)
- Need help in design and javascript code for Dynamic form (JavaScript / DHTML / AJAX)
Other Threads in the PHP Forum
- Previous Thread: Problem with where clause or ....??
- Next Thread: How to set up a search that uses checkboxes?
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email encode error fcc file files folder form forms function functions google howtowriteathesis href htaccess html image images include insert integration ip java javascript joomla ldap limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop open parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table template tutorial update upload url validation validator variable video web xml youtube





