PHP array to HTML combo box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dcalladi
Forum Newbie
Posts: 2
Joined: Sat Nov 19, 2011 4:56 pm

PHP array to HTML combo box

Post by dcalladi »

Hi, im resonably new to HTML and even more new to PHP i am taking some infomration from a database and putting it in an array in PHP
i then want to take that array and use it to fill in options for a combo box in HTML
i have this code os far

Code: Select all

<?php
            $sql="SELECT * FROM hotels order by city,name ;";
         
            $result_set = pg_Exec ($conn, $sql);
            $rows = pg_NumRows($result_set);

            if ((!$result_set) || ($rows < 1)) {
                  echo "<H1>ERROR - no rows returned</H1><P>";
                  exit;  //exit the script
                  }

            for ($j=0; $j < $rows; $j++)
               {
               $name = pg_result($result_set, $j, "name");
               $hotels[$j] = $name;
			   }
			   for ($i = 0; $i < $results = count($hotels); $i++){
			   }
?> 
and then this HTML
<div id="form">
<form>
Choose Your Journey <br />
From: <br />
<select name ="From">
<option value=></option>
<option value=></option>
<option value=></option>
<option value=></option>
</select>
<br />
To: <br />
<select name ="To">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</form>
</div>

i need both of those combox boxes to have the items from the array as options

Thank you very much in advance for your help.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP array to HTML combo box

Post by social_experiment »

viewtopic.php?f=1&t=132938
This is a similar question (and answer) to what you want to do; only difference might be the type of database you are using but the principle stays the same.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply