Jump to content

PHP Loop in dropdown menu


UQ13A

Recommended Posts

Another Question again  ;D

 

How would i print my result(s) into drop down menus with a button

 

if (isset($_GET['edit'])) {

$s = $_SESSION['hello'];
$gen = rand(1,1000);
$gen1 = rand(1,1000);
$gen2 = rand($gen,$gen1);

$query = ("SELECT * FROM `car` WHERE owner = '$s'");
$result = mysql_query($query) or die(mysql_error());
$numresults = mysql_num_rows($result);
if ($numresults == 0) {
echo "You dont have any cars to edit";
exit; 
} else {
$i=1;

while($row = mysql_fetch_array($result)) {

$make = $row['Make'];
$model = $row['Model'];
$id = $row['id'];

print "<form action=\"edit.php\" method=\"GET\"><select name=\"edit\" class=\"textbox\" id=\"edit\">
   <option value=\"Please Select\" selected=\"selected\">Please Select...</option>
           <option value=\"$model$make$id$gen2\">$models $makes</option>
            </select><br />
            <input type=\"submit\" name=\"edit\" id=\"edit\" value=\"Edit Car\"/>
            </form>";
           
$i++;
}
}
}

 

When this script is ran it shows all the results but in differant menus, I need them in one

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/162644-php-loop-in-dropdown-menu/
Share on other sites

Simply take all the HTML code in the Print-Statement (except for the second <option></option> line) and put it outside the loop.

 

<?php
if (isset($_GET['edit'])) {
$s = $_SESSION['hello'];
$gen = rand(1,1000);
$gen1 = rand(1,1000);
$gen2 = rand($gen,$gen1);
$query = ("SELECT * FROM `car` WHERE owner = '$s'");
$result = mysql_query($query) or die(mysql_error());
$numresults = mysql_num_rows($result);
if ($numresults == 0) {
	echo "You dont have any cars to edit";
	exit;
} else {
	$i=1;
	print "<form action=\"edit.php\" method=\"GET\"><select name=\"edit\" class=\"textbox\" id=\"edit\">
			<option value=\"Please Select\" selected=\"selected\">Please Select...</option>";
	while($row = mysql_fetch_array($result)) {
		$make = $row['Make'];
		$model = $row['Model'];
		$id = $row['id'];
		print "<option value=\"$model$make$id$gen2\">$models $makes</option>";
		$i++;
	}
	print "</select><br />
			<input type=\"submit\" name=\"edit\" id=\"edit\" value=\"Edit Car\"/>
			</form>";
}
}
?>

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.