dynamic drop down menu in php

Reply

Join Date: Feb 2008
Posts: 3
Reputation: aliyesami is an unknown quantity at this point 
Solved Threads: 0
aliyesami aliyesami is offline Offline
Newbie Poster

dynamic drop down menu in php

 
0
  #1
Feb 3rd, 2008
hi !

below is a simple html code which lets a user input a value and then passes it to a php program which runs a sql querry againt an oracle database displaying the result set.
what I would like to do is to be able to offer a drop down list of values to select from ( which will come from a table in oracle) instead of the user remembering n entering a value, how can i achieve that ?

thanks
Sami

--------------------------------------------- form2.html -----------------------------------------------------------------
  1. <form method="POST" action="select2.php">
  2. <div align="left"><p><font face="BankGothic Md BT">host name?</font>
  3. <input type="text" name="host" size="14"><input type="submit" value="Submit"></p>
  4. </div></form>


---------------------------------------------- select2.php -----------------------------------------------------------
  1. <?php
  2.  
  3. $host = $_POST["host"];
  4. $hostname=strtoupper($host);
  5.  
  6. $conn = oci_connect('SAMI', 'SAMI', 'emrep');
  7. if (!$conn) {
  8. $e = oci_error();
  9. print htmlentities($e['message']);
  10. exit;
  11. }
  12.  
  13. $query = "SELECT * FROM INSTANCE where HOST_ID in (select HOST_ID from HOST_LOOKUP where HOST_NAME='$hostname')" ;
  14.  
  15. $stid = oci_parse($conn, $query);
  16. if (!$stid) {
  17. $e = oci_error($conn);
  18. print htmlentities($e['message']);
  19. exit;
  20. }
  21.  
  22. $r = oci_execute($stid, OCI_DEFAULT);
  23. if (!$r) {
  24. $e = oci_error($stid);
  25. echo htmlentities($e['message']);
  26. exit;
  27. }
  28.  
  29. print '<table border="1">';
  30. while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
  31. print '<tr>';
  32. foreach ($row as $item) {
  33. print '<td>'.($item?htmlentities($item):'&nbsp;').'</td>';
  34. }
  35. print '</tr>';
  36. }
  37. print '</table>';
  38.  
  39. oci_close($conn);
  40. ?>
Last edited by peter_budo; Feb 4th, 2008 at 9:22 am. Reason: Inserting code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 57
Reputation: Walkere is an unknown quantity at this point 
Solved Threads: 5
Walkere Walkere is offline Offline
Junior Poster in Training

Re: dynamic drop down menu in php

 
1
  #2
Feb 3rd, 2008
Originally Posted by aliyesami View Post
what I would like to do is to be able to offer a drop down list of values to select from ( which will come from a table in oracle) instead of the user remembering n entering a value, how can i achieve that ?
In HTML, what you're looking for is the <select> tag. This creates a dropdown list, like...

  1. <select>
  2. <option value="value">Text</option>
  3. <option value="value">Text</option>
  4. </select>

So what you need to do is get the result set from your database, loop through the result set, and create an <option> tag for each item in the result set.

I'm not familiar with the Oracle functions, so you'll have to create the query and what not yourself, but here's the idea.

  1. $query = "SELECT value, text FROM table WHERE condition = TRUE";
  2. $result = oci_execute($query, OCI_DEFAULT);
  3.  
  4. echo '<select>';
  5.  
  6. while ($row = oci_fetch_array($result)) {
  7. echo '<option value='" . $row['value'] . '">' . $row['text'] . '</option>';
  8. }
  9. echo '</select>';
  10.  

Hopefully you get the concept out of that, and you can fill in the right query/oracle functions to get it to work.

The steps are basically...
  • Write query
  • Execute query
  • Open the <select>
  • Loop through query results
  • Print an option for each value in the query result set
  • Close the <select>

Good luck,
- Walkere
Reply With Quote Quick reply to this message  
Reply

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




Views: 4224 | Replies: 1
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC