DoctorJ03
DoctorJ03

Reputation: 11

Why are my Values from a database not displaying in my drop down menu ?

I've coded this page in a combination of html5 and PHP per its' specifications. My problem is that when I connect the page to a specific database (which I do twice on this page) the second connection displays the values in the table I laid out flawlessly, but the first connection gives me a drop down menu with empty spaces. The menu is suppose to display all 50 states in alphabetical order as they do in my table below it. The empty spaces in the menu seem to equal up to 50 states I just don't have any text displayed in those fields. What am I missing ? I've tried several different ways per other questions on this site, but end up with the same result.

Below is part of the HTML and PHP:

<html>
<body>
<form>

<!-- Drop Down set up for Database -->
<select name = "state">
<option value ="default">Select a State</option>

<!-- Retrieve Data from Database -->
<?php
 $db=pg_connect("dbname=etc..");
 $query = "select state_name";
 $query .= " from state_t";
 $query .= " order by state_name";
 $dbResult = pg_query($query);

 if (!$dbResult)
 {
  die("Database error...");
 }

 $num = pg_num_rows($dbResult);
 if ($num == 0)
 {
  echo '<tr><td colspan="2">';
  echo 'Database Query Retrieved Nothing!</td></tr>';
 }
  while ($num = pg_fetch_array($dbResult))
 {
  $state_name = pg_Result($dbResult,'state_name');
  echo '<option value="$num[$state_name]"</option>';
 }
?>
</select>

</form>
</body>
</html>

Upvotes: 1

Views: 1408

Answers (3)

S Vinesh
S Vinesh

Reputation: 539

The bascic Syntax for select box itself is wrong

The syntax is

 <select>
    <option value="a"> A </option>
 <select>

Where a = The value you want to get from this option if it is selected, A = The value to be displayed to the user

So your coding is supposed to be

 while ($fet = pg_fetch_array($dbResult))
 {
      echo "<option value='$fet[state_name]'>".$fet['state_name']."</option>";
 }

where state_name is the field name of states in your table

Upvotes: 0

Jackson LG
Jackson LG

Reputation: 1

You can try:

echo '<option value="$num["$state_name"]" > $num["$state_name"] </option>';

instead of:

echo '<option value="$num[$state_name]"</option>';

Upvotes: 0

Tyralcori
Tyralcori

Reputation: 1047

Because syntax error?

Try:

while ($num = pg_fetch_array($dbResult))
{
   $state_name = pg_Result($dbResult,'state_name');
   echo '<option value="$num[$state_name]">' . $num[$state_name] . '</option>';
}

Upvotes: 1

Related Questions