943,678 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 696
  • PHP RSS
Jun 18th, 2009
0

How to Auto populate Origin filed

Expand Post »
I have a database with four tables: name, meaning, gender, origin, and i want to have the origin field automatically populated from the database rather than having to type the code for all the selections in the html.

Below is what i've been playing with but I can't make it work no matter where i put the code. I would appreciate any help - is the code right? and where do i place it? Thanks in advance.

PHP Syntax (Toggle Plain Text)
  1. $sql = 'select origin, originID from origin';
  2. $rsOrigins= mysql_query($sql, $connection) or die(mysql_error());
  3. $row_rsOrigins= mysql_fetch_assoc($rsOrigins);
  4. ?>
  5. <select name="origin" id="origin">
  6. <option value="any">Any</option>
  7. <?php
  8. do { ?>
  9.  
  10. <option value="<?php echo $row_rsOrigins['originID'] ?>"><?php echo $row_rsOrigins['origin'] ?></option>
  11.  
  12. <?php } while ($row_rsOrigins = mysql_fetch_assoc($rsOrigins)); ?>
  13. </select>

This is my html:

PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Baby Names Database</title>
  4. </head>
  5. <body>
  6. <h1>Baby Names</h1>
  7. <h3>Please fill in the blanks below, and We'll return baby names for you to browse.</h3>
  8.  
  9.  
  10. <table border = "1" align = "center" style="font-family: verdana; font-size: small">
  11. <form method = "post" action = "practiceBaby5.php">
  12. <tr bgcolor="#cae1ff">
  13. <td align = "left">
  14. Gender:
  15. </td>
  16. <td>
  17. <input type = "radio" name = "gender" value = "male"/>Boys Names
  18. <input type = "radio" name = "gender" value = "female"/>Girls Names
  19. <input type = "radio" name = "gender" value = ""/>Both
  20. </td>
  21. </tr>
  22. <tr bgcolor="#ffe1ff">
  23. <td align = "left">
  24. Meaning:
  25. </td>
  26. <td>
  27. <input type = "text" name = "meaning" value = ""/>
  28. </td>
  29. </tr>
  30. <tr bgcolor="#ffe1ff">
  31. <td align = "left">
  32. Name Begins With:
  33. </td>
  34. <td>
  35. <input type = "text" name = "name" value = ""/>
  36. </td>
  37. </tr>
  38. <tr bgcolor="#cae1ff">
  39. <td align = "left">
  40. Origin
  41. </td>
  42. <td>
  43.  
  44. <select name = "origin" id = "origin">
  45. <option selected value = "">Any</option>
  46. <option value = "african">African</option>
  47. <option value = "anglo">Anglo</option>
  48. <option value = "arabian">Arabian</option>
  49. <option value = "arabic">Arabic</option>
  50. <option value = "aramaic">Aramaic</option>
  51. <option value = "armenian">Armenian</option>
  52. <option value = "arthurian">Arthurian</option>
  53. <option value = "basque">Basque</option>
  54. <option value = "celtic">Celtic</option>
  55. <option value = "chamoru">Chamoru</option>
  56. </select>
  57.  
  58. </td>
  59. </tr>
  60.  
  61. </table><p /><center>
  62. <input type = "submit" value = "Show me Names"/>
  63. </center>
  64. </form>
  65. </body>
  66. </html>
  67.  

This is my php:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. // MySQL Connection Information
  3. $server = "localhost";
  4. $username = "";
  5. $password = "";
  6. $dbname = "baby_names";
  7.  
  8. // MySQL Connect String
  9. $connection = mysql_connect($server,$username,$password) or die("Can't Connect to Mysql Server:
  10.  
  11. ".mysql_error());
  12.  
  13. // Select the database
  14. mysql_select_db($dbname) or die("Can't connect to database: ".mysql_error());
  15.  
  16.  
  17.  
  18. $gender = mysql_real_escape_string($_POST['gender']);
  19. $meaning = mysql_real_escape_string($_POST['meaning']);
  20. $name = mysql_real_escape_string($_POST['name']);
  21. $origin = mysql_real_escape_string($_POST['origin']);
  22. if($gender) {
  23. $whereArr[] = "gender = '" . $gender . "' ";
  24. }
  25. if($name) {
  26. $whereArr[] = "name LIKE '" . $name . "%'";
  27. }
  28. if($meaning) {
  29. $whereArr[] = "meaning LIKE '%" . $meaning . "%'";
  30. }
  31. if($origin) {
  32. $whereArr[] = "origin = '" . $origin . "' ";
  33. }
  34. if(count($whereArr)) {
  35. $where = @implode(" AND ", $whereArr);
  36. $where = ' WHERE '.$where;
  37. }
  38.  
  39. $sql = "SELECT * FROM names $where";
  40.  
  41. /*
  42. if ($gender == 'both') { // no specific gender
  43. if ($origin == 'any') { // no specific origin
  44. $sql = 'select * from names';
  45. else { // an origin was specified
  46. $sql = "select * from names where origin = '".$origin."'";
  47. }
  48.  
  49. else { // a gender was specified
  50.  
  51. if ($origin == 'any') { // no specific origin
  52. $sql = "select * from names where gender ='".$gender."'";
  53. else { // an origin was also specified
  54. $sql = "select * from names where origin = '".$origin."' and gender ='".$gender."'";
  55. }
  56. */
  57.  
  58.  
  59.  
  60.  
  61. //execute SQL query and get result
  62. $sql_result = mysql_query($sql, $connection) or die(mysql_error());
  63.  
  64. ?>
  65. <table border="0" align="center" style="font-family: verdana; font-size: small">
  66. <tr>
  67. <th style="background: #ffd">Name</th>
  68. <th style="background: #efe">Gender</th>
  69. <th style="background: #fef">Origin</th>
  70. <th style="background: #e3e4fa">Meaning</th>
  71. </tr>
  72.  
  73. <?php
  74. // Loop through the data set and extract each row into it's own variable set
  75. while ($row = mysql_fetch_array($sql_result))
  76. {
  77. //extract($row);
  78. ?>
  79. <tr>
  80. <td style="background: #ffd"><?php echo $row['name'];?></td>
  81. <td style="background: #efe"><?php echo $row['gender'];?></td>
  82. <td style="background: #fef"><?php echo $row['origin'];?></td>
  83. <td style="background: #e3e4fa"><?php echo $row['meaning'];?></td>
  84. </tr>
  85. <?php
  86. }
  87.  
  88. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phpNewbie is offline Offline
13 posts
since Jan 2009
Jun 18th, 2009
0

Re: How to Auto populate Origin filed

I didn't test it, but tis should work for you.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $sql = 'select origin, originID from origin';
  3. $rsOrigins= mysql_query($sql, $connection) or die(mysql_error());
  4.  
  5. echo '<select name="origin" id="origin">\n'.
  6. '<option value="any">Any</option>\n';
  7. while($row_rsOrigins = mysql_fetch_array($result))
  8. {
  9. echo '<option value="'.$row_rsOrigins['originID'].'">'.$row_rsOrigins['origin'].'"</option>\n';
  10. }
  11. echo '</select>\n';
  12. ?>
Reputation Points: 34
Solved Threads: 51
Posting Whiz
kireol is offline Offline
305 posts
since Mar 2008
Jun 19th, 2009
0

Re: How to Auto populate Origin filed

Thank you for your reply - I am trying this everywhere but does your code replace the
PHP Syntax (Toggle Plain Text)
  1. <select name="origin" id="origin">
  2. <option selected value="">Any</option>
  3. <option value="african">African</option>
  4. </selection>

part of the html file or does it get placed in the .php file. If in the php file, does it matter what order the code is placed?

I am expecting to have all the Origins in my database reflected in the <select> dropdown of the html - i don't mind coding all the <option>'s into the html but i understand php will do it for me i just wish i could understand php lol

thanks for any further help as it will save a good deal of work if i add more origins to the database and they can appear in the html dropdown automatically.
Last edited by phpNewbie; Jun 19th, 2009 at 3:32 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phpNewbie is offline Offline
13 posts
since Jan 2009
Aug 14th, 2009
0

Re: How to Auto populate Origin filed

hi phpnewbie! did kireol's code work? it should..
you just have to fetch the data from the table then loop it inside the select tag.


-enim
Reputation Points: 11
Solved Threads: 2
Light Poster
enim213 is offline Offline
40 posts
since Jun 2008

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: Turning off email verification in Vbulletin
Next Thread in PHP Forum Timeline: PHP loop through database and limit number of entries





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


Follow us on Twitter


© 2011 DaniWeb® LLC