Jump to content

Drop down menu not working


ady01

Recommended Posts

Im creating a drop down menu in PHP which pulls data from a table in MySqL : This is my code  >

 

<?php
require_once("config.php");

$db=mysql_connect($AddressBook_HOST,$AddressBook_Username,$AddressBook_Password);
mysql_select_db($AddressBook_DatabaseName,$db);

$query = "SELECT HouseNumber
          FROM Addresses
          ORDER BY HouseNumber";
$result = @mysql_query($query);
print "<p><select></p>";    
if (mysql_num_rows($result) > 0) :
while ($row = mysql_fetch_array($result)) :
	$HouseNumber = $row[HouseNumber];
	print "<p><option>value='$HouseNumber'>$HouseNumber</option></p>";
endwhile;
print "<p></select></p>";
endif;

?>

 

 

Its Kind of working but on the web page the option in the dropdown box is showing Value='$HouseNumber'>$HouseNumber  instead of the real data  ? Any Idears what i've done wrong here  ?

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/103990-drop-down-menu-not-working/
Share on other sites

<?php
require_once("config.php");

$db=mysql_connect($AddressBook_HOST,$AddressBook_Username,$AddressBook_Password);
mysql_select_db($AddressBook_DatabaseName,$db);

$query = "SELECT HouseNumber
          FROM Addresses
          ORDER BY HouseNumber";
$result = @mysql_query($query);
print "<p><select></p>";    
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
	$HouseNumber = $row[HouseNumber];
	print '<p><option value="'.$HouseNumber.'">'.$HouseNumber.'</option></p>';
}
print "<p></select></p>";
}

?>

what about now?

found some mistake, sorry, try now.

Think they need to be there taking them off just results in the below being shown on the page  >>>

 

"; if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $HouseNumber = $row[HouseNumber]; print ''.$HouseNumber.''; endwhile} print ""; } ?>

 

 

try something like this,

 

<form action="<?=$PHP_SELF?>" method="post">  <?php
global $database;
    $q = "SELECT HouseNumber
          FROM Addresses
          ORDER BY 'HouseNumber'
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error()); 
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'NONE FOUND!';
    }
?>

<select name="name">
<?php
   while( $row = mysql_fetch_assoc($result) ) {
    echo '<option value="' . $row[HouseNumber] . '">';
    echo $row[HouseNumber] . '</option>';
    }

?>
</select>
</form>

Well Thanks For the 'new way' not sure why all this is not working though still getting the below !!!

 

" method="post"> query($q) or die("Error: " . mysql_error()); $num_rows = mysql_numrows($result); if( $num_rows == 0 ){ return 'NONE FOUND!'; } ?>

 

I have done a whole search database here and cant get a simple drop down working !

Back to your original version:

print "<select>";
while ($row = mysql_fetch_array($result)) :
	$HouseNumber = $row[HouseNumber];
print "<option>value='".$row['HouseNumber']."'>".$row['HouseNumber']."</option>";
}
print "</select>";

should work... however if this doesn't, could you show the html code which is outputted!

You should be getting something like:

<select>
<option>value='42'>42</option>
<option>value='69'>69</option>
</select>

oooh, I see it... it actually needs to look like this:

<select>
<option value='42'>42</option>
<option value='69'>69</option>
</select>

we're closing the tags too soon!

Good one ! Think im still missing a trick though as this is a screen shot of what i get (I have also reposted the code as it now stands!!)

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<br />
<?php
require_once("config.php");

$db=mysql_connect($AddressBook_HOST,$AddressBook_Username,$AddressBook_Password);
mysql_select_db($AddressBook_DatabaseName,$db);

$query = "SELECT HouseNumber
          FROM Addresses
          ORDER BY HouseNumber";
$result = @mysql_query($query);
print "<p><select></p>";    
if (mysql_num_rows($result) > 0) :
while ($row = mysql_fetch_array($result)) :
	$HouseNumber = $row[HouseNumber];
	print "<p><option value='$HouseNumber'>$HouseNumber</option></p>";
endwhile;
print "<p></select></p>";
endif;

?>
</body>
</html>

 

[attachment deleted by admin]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<br />
<?php
require_once("config.php");

$db=mysql_connect($AddressBook_HOST,$AddressBook_Username,$AddressBook_Password);
mysql_select_db($AddressBook_DatabaseName,$db);

$query = "SELECT HouseNumber
          FROM Addresses
          ORDER BY HouseNumber";
$result = @mysql_query($query);
print "<p><select>";
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_array($result))
{
	$HouseNumber = $row[HouseNumber];
	print "<option value='$HouseNumber'>$HouseNumber</option>";
}
print "</select></p>";
}

?>
</body>
</html>

Have you tested your PHP installation on this server yet?

 

Make a page phpinfo.php with the contents:

<?php
phpinfo();
?>

If this code contains a large table of values its the code, if not you don't have PHP setup correctly which is what this looks like to me.

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.