How to Auto populate Origin filed

Reply

Join Date: Jan 2009
Posts: 13
Reputation: phpNewbie is an unknown quantity at this point 
Solved Threads: 0
phpNewbie phpNewbie is offline Offline
Newbie Poster

How to Auto populate Origin filed

 
0
  #1
Jun 18th, 2009
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.

  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:

  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:

  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. ?>
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 187
Reputation: kireol is an unknown quantity at this point 
Solved Threads: 24
kireol kireol is offline Offline
Junior Poster

Re: How to Auto populate Origin filed

 
0
  #2
Jun 18th, 2009
I didn't test it, but tis should work for you.

  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. ?>
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 13
Reputation: phpNewbie is an unknown quantity at this point 
Solved Threads: 0
phpNewbie phpNewbie is offline Offline
Newbie Poster

Re: How to Auto populate Origin filed

 
0
  #3
Jun 19th, 2009
Thank you for your reply - I am trying this everywhere but does your code replace the
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 40
Reputation: enim213 is an unknown quantity at this point 
Solved Threads: 2
enim213 enim213 is offline Offline
Light Poster

Re: How to Auto populate Origin filed

 
0
  #4
Aug 14th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC