How do I populate a dropdown box from a xoops table?

Requested and Answered by Kaotik on 2005/9/19 17:53:56

How do I populate a dropdown box from a xoops table?

Create a standard html form then add the following code:

<SELECT name="dropdown[]" size="10" multiple id="select">
<?php
$query 
$xoopsDB->query(' SELECT * FROM ' $xoopsDB->prefix('xoops_table'));
while (
$row $xoopsDB->fetchArray($query))
{
$sel $row['field1'];
$id =$row['field2'];
echo 
"<OPTION VALUE='$id'>$sel</OPTION>n";
}
?>
</SELECT>


This will create a var called $dropdown which is an array. To read through the results you can use this:
$count=count($dropdown);
for (
$i=0$i<$count$i++) {
echo 
"result: ".$dropdown[$i]."<br>";
//do something else with each selected option.
}


IN case you don't want the select box to be multiselect, replace this:
<SELECT name="dropdown[]" size="10" multiple id="select">

with this:
<SELECT name="dropdown" size="10" id="select">

This Q&A was found on XOOPS Web Application System : https://xoops.org/modules/smartfaq/faq.php?faqid=514