943,913 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 9741
  • PHP RSS
Feb 3rd, 2008
0

dynamic drop down menu in php

Expand Post »
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 -----------------------------------------------------------------
html Syntax (Toggle Plain Text)
  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 -----------------------------------------------------------
php Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aliyesami is offline Offline
3 posts
since Feb 2008
Feb 3rd, 2008
1

Re: dynamic drop down menu in php

Click to Expand / Collapse  Quote originally posted by aliyesami ...
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...

PHP Syntax (Toggle Plain Text)
  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.

php Syntax (Toggle Plain Text)
  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
Reputation Points: 29
Solved Threads: 5
Junior Poster in Training
Walkere is offline Offline
57 posts
since Jan 2008
Dec 31st, 2009
0
Re: dynamic drop down menu in php
@Walkere

Dude you rock ... i was like googling!! since a long time..! to make my dynamic menu for a small project to get.. values from mysql.. but this helped me a lot

PHP Syntax (Toggle Plain Text)
  1. echo '<option value='" . $row['value'] . '">' . $row['text'] . '</option>';
  2.  

thanx again
Last edited by daneuchar; Dec 31st, 2009 at 11:38 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
daneuchar is offline Offline
30 posts
since Sep 2009
Nov 10th, 2011
0
Re: dynamic drop down menu in php
well this code rocks,
how do i use the selected value in another query? for example i have
Value text
1 A
2 b
3 c

after user select "b" how do i use "2" in anther query
how do i get the value?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sarper is offline Offline
1 posts
since Nov 2011

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: How we open a ms word document file with php?
Next Thread in PHP Forum Timeline: Unable to upload files using Code Igniter





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


Follow us on Twitter


© 2011 DaniWeb® LLC