Jump to content

select box from database / foreach help.


herghost

Recommended Posts

Hi all,

 

I have the below:

 <?php
                    
                    $query0 = "SELECT * FROM product_package";
						$result0 = mysql_query($query0);
						echo '<dl>';
						echo '<dt>';
						echo '<label for="package_id">Product Package:</label>';
						echo '</dt>';


                   	 		echo '<dd>';
						echo '<select size="1"name="package_id" id="package_id">';
						while ($list0 = mysql_fetch_assoc($result0)) 
							{	
								$p_id = $list0['package_id'];
								$p_name = $list0['package_name'];
									echo '<option value=';
									echo $p_id;
									echo '>';
									echo $p_name;
									echo '</option>';
									echo '</select>';
									echo '</dd>';
									echo '</dl>';
							}
				?> 

 

Which works fine if their is only one result in the database, however if there is more then they just get listed under dropdown box. I am guessing that I need a foreach statement, but not sure how I implement one.

 

Link to comment
https://forums.phpfreaks.com/topic/196329-select-box-from-database-foreach-help/
Share on other sites

Got It!

<?php

                    					


                    $query0 = "SELECT * FROM product_package";
						$result0 = mysql_query($query0);
						echo '<dl>';
						echo '<dt>';
						echo '<label for="package_id">Product Package:</label>';
						echo '</dt>';
						echo '<dd>';
						echo "<select name=package_id value=''>Package Name</option>";
						while ($list0 = mysql_fetch_array($result0)) 
							{	
									echo "<option value=$list0[package_id]>$list0[package_name]</option>";

							}
							echo '</select>';
									echo '</dd>';
									echo '</dl>';
				?> 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.