954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

dynamic drop down menu in php

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 -----------------------------------------------------------------

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

---------------------------------------------- select2.php -----------------------------------------------------------

<?php

$host = $_POST["host"];
$hostname=strtoupper($host);

  $conn = oci_connect('SAMI', 'SAMI', 'emrep');
  if (!$conn) {
    $e = oci_error();
    print htmlentities($e['message']);
    exit;
  }

  $query = "SELECT * FROM INSTANCE where HOST_ID in (select HOST_ID from HOST_LOOKUP where HOST_NAME='$hostname')" ;

  $stid = oci_parse($conn, $query);
  if (!$stid) {
    $e = oci_error($conn);
    print htmlentities($e['message']);
    exit;
  }

  $r = oci_execute($stid, OCI_DEFAULT);
  if (!$r) {
    $e = oci_error($stid);
    echo htmlentities($e['message']);
    exit;
  }

  print '<table border="1">';
  while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
    print '<tr>';
       foreach ($row as $item) {
         print '<td>'.($item?htmlentities($item):'&nbsp;').'</td>';
       }
       print '</tr>';
  }
  print '</table>';

  oci_close($conn);
?>
aliyesami
Newbie Poster
3 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 
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 tag. This creates a dropdown list, like...

<select>
    <option value="value">Text</option>
    <option value="value">Text</option>
</select>


So what you need to do is get the result set from your database, loop through the result set, and create an 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.

$query = "SELECT value, text FROM table WHERE condition = TRUE";
$result = oci_execute($query, OCI_DEFAULT);

echo '<select>';

while ($row = oci_fetch_array($result)) {
   echo '<option value='" . $row['value'] . '">' . $row['text'] . '</option>';
}
echo '</select>';


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
Loop through query results
Print an option for each value in the query result set
Close the

Good luck,
- Walkere

Walkere
Junior Poster in Training
57 posts since Jan 2008
Reputation Points: 29
Solved Threads: 5
 

@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 :)

echo '<option value='" . $row['value'] . '">' . $row['text'] . '</option>';


thanx again

daneuchar
Light Poster
30 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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?

sarper
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You