Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with populating a listbox???

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
US
I am a newbie with PHP/PostgreSQL, but what I am trying to do is populate a listbox on a form with the results from a query against the database. My code is below, any help is much appreciated. My results show up as:
'Array' 'No data available'.

The 'No data available' is from my Else condition if nothing is found, but I know it should return one value.


//Code

$query = "SELECT DISTINCT type FROM tblmain";
$result = pg_query($connection, $query) or die("Error in query: $query." . pg_last_error($connection));

$rows = pg_num_rows($result);

if ($rows > 0)
for ($i=0; $i<$rows; $i++)
{
list($results[]) = pg_fetch_row($result);
{
echo $results;
}
}
else
?>
<font size=&quot;-1&quot;>No data available.</font>
<?
{
pg_close($connection);
}
 
You need to access the values of the array $results, it won't print out the whole array like that.

You can try echo $results[0], or $results[key] if you know the key to ge tthe first element... you may need some kind of foreach or each if you want all the elements.

If you're uncertain the key, and/or 0 doesn't work for you you can echo serialize($results) to see the entire contents of the array, though not in a very readable format, it is decipherable and the keys can be ascertained.

-Rob
 
OK...I have modified it below...It returns 3 distict types in a dropdown but it is building three dropdowns and the two extra dropdowns are empty??? And if I added a 4th distinct type, it will create a 4th dropdown???

//Code

$query = &quot;SELECT DISTINCT type FROM tblmain&quot;;
$result = pg_query($connection, $query) or die(&quot;Error in query: $query. &quot; . pg_last_error($connection));
$rows = pg_num_rows($result);
if ($rows > 0)
for ($i=0; $i<$rows; $i++)
{
{
echo &quot;<select name=type>\n&quot;;
while ($row = pg_fetch_array($result)) {
echo &quot;<option value=$row[0]>$row[0]</option>\n&quot;;
}
echo &quot;</select>\n&quot;;
}
}
pg_close($connection);
 
Fixed it...

Took out:

for ($i=0; $i<$rows; $i++)

Thanks for the help...
 
that's because you have some malformed loops.

$rows = 3 I'm guessing.

So what you want is
Code:
$rows = pg_num_rows($result);
if ($rows >0) {
  echo &quot;<select name=type>\n&quot;;
  for($i=0;$i<$rows;$i++) 
  {
    $row = pg_fetch_array($result);
    echo &quot;<option value=$row[0]>$row[0]</option\n&quot;;
  }
  echo &quot;</select>&quot;;

OR JUST... this will give you an empty listbox if no info, which may be desirable... enclose it in the same if as above if it's not.

Code:
echo &quot;<select name=type>\n&quot;;
while ($row = pg_fetch_array($result)) {
  echo &quot;<option value=$row[0]>$row[0]</option>\n&quot;;
}
echo &quot;</select>&quot;;

What's happening if you're curious is that pg_fetch_array moves the pointer through your records... so you loop through the first time, and properly execute that loop filling your first list box, but now you have 2 more loops left on your for loop, so it tries that while loop again, but the pointer is already at the last row so it has no information for you.
 
Once you have populated the listbox with values from the database (I'm using MySql), how do you assign the chosen selection option to a session variable to use in another page?


My code is:

$connect = db_connect();
$query = &quot;select divisionname from division&quot;;
$results = mysql_query($query);

?>select name = &quot;item&quot;><?
while ($line = mysql_fetch_array($results)
{
foreach ($line as $value)
{
print &quot;<OPTION value = '$value');
}
print &quot;>$value</OPTION>&quot;;
}
mysql_free_result($result);
?></select><?
}

cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top