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
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.

Link to comment
Share on other sites

Still nothing, the attatched file is what the dropdown box looks like with this code mate  :  Just cant understand why the word 'housenumber' is appearing instead of the data !

 

 

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

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 ""; } ?>

 

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 !

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

<!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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Still the same mate, im wondering if ORDERBY HouseNumber is the problem here, I know it shouldent be but something is telling me that i should maybe try ordering by ID or something (E.g taking data from HouseNumber and then order by ID number)

 

That make any sence  ?

Link to comment
Share on other sites

Well, then I guess you're going to need to get to more difficult debugging.  Try adding things such as echoing mysql_num_rows($result) check that it is grabbing the right data.  Tell me if that returns the correct number of rows.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.