ady01 Posted May 3, 2008 Share Posted May 3, 2008 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 ? Quote Link to comment Share on other sites More sharing options...
DeFActo Posted May 3, 2008 Share Posted May 3, 2008 print '<p><option value="'$HouseNumber'">'$HouseNumber'</option></p>'; try that. Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 3, 2008 Author Share Posted May 3, 2008 Better, but the dropdown box now just has $'HouseNumber' and under it it says "; endif; ?> Quote Link to comment Share on other sites More sharing options...
DeFActo Posted May 3, 2008 Share Posted May 3, 2008 check this out http://www.phpfreaks.com/forums/index.php/topic,195463.0.html Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 3, 2008 Author Share Posted May 3, 2008 Weird, still not getting a dropdown box now just getting the below in the space where the dropdown should be !! '.$field.''; } ?> Quote Link to comment Share on other sites More sharing options...
DeFActo Posted May 3, 2008 Share Posted May 3, 2008 <?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. Quote Link to comment Share on other sites More sharing options...
Xurion Posted May 3, 2008 Share Posted May 3, 2008 Why are you putting P tags around your other elements? Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 3, 2008 Author Share Posted May 3, 2008 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] Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 3, 2008 Author Share Posted May 3, 2008 Do you think I should try taking them all off ? If im honest I put them there due to 'thinking' i needed them ? Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 3, 2008 Author Share Posted May 3, 2008 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 ""; } ?> Quote Link to comment Share on other sites More sharing options...
blueman378 Posted May 4, 2008 Share Posted May 4, 2008 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> Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 4, 2008 Author Share Posted May 4, 2008 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 ! Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 4, 2008 Author Share Posted May 4, 2008 *Bump* Quote Link to comment Share on other sites More sharing options...
rarebit Posted May 4, 2008 Share Posted May 4, 2008 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! Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 4, 2008 Author Share Posted May 4, 2008 Sure im getting the below when run Value='$HouseNumber'>$HouseNumber I think I will stick with my original code as what blueman is suggesting just makes things worse. Quote Link to comment Share on other sites More sharing options...
rarebit Posted May 4, 2008 Share Posted May 4, 2008 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! Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 4, 2008 Author Share Posted May 4, 2008 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] Quote Link to comment Share on other sites More sharing options...
rarebit Posted May 4, 2008 Share Posted May 4, 2008 <!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> Quote Link to comment Share on other sites More sharing options...
MattDunbar Posted May 4, 2008 Share Posted May 4, 2008 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. Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 4, 2008 Author Share Posted May 4, 2008 Yes everything else working fine just not this Dam drop down menu !!! Sorry Rarebit, still same Problem ! Quote Link to comment Share on other sites More sharing options...
MattDunbar Posted May 4, 2008 Share Posted May 4, 2008 print "<option value='$HouseNumber'>$HouseNumber</option>"; Replace that with: print '<option value="'.$HouseNumber.'">'.$HouseNumber.'</option>'; Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 4, 2008 Author Share Posted May 4, 2008 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 ? Quote Link to comment Share on other sites More sharing options...
MattDunbar Posted May 4, 2008 Share Posted May 4, 2008 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. Quote Link to comment Share on other sites More sharing options...
ady01 Posted May 5, 2008 Author Share Posted May 5, 2008 *bump* Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.