Trium918 Posted April 1, 2007 Share Posted April 1, 2007 My goal is to use a Drop Down Menu that is populated by the database. The user is then able to select a name from the Menu that pulls all of the information for a particular person from the database. Finally, a table is populated with the information of the selected person. Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/ Share on other sites More sharing options...
Trium918 Posted April 1, 2007 Author Share Posted April 1, 2007 My goal is to use a Drop Down Menu that is populated by the database. The user is then able to select a name from the Menu that pulls all of the information for a particular person from the database. Finally, a table is populated with the information of the selected person. Example: Click Here!!! //Query the database $query="Select * FROM people"; $result = mysql_query($query); $num_results = mysql_num_rows($result); $searchtype = mysql_real_escape_string($_POST['searchtype']); //This is the loop that will populate the table for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); $name = htmlspecialchars( stripslashes($row["name"])); $age = htmlspecialchars( stripslashes($row["age"])); $state = htmlspecialchars( stripslashes($row["state"])); $sponsor = htmlspecialchars( stripslashes($row["sponsor"])); // Output echo " <tr> <td>$name</td> <td>$age</td> <td>$state</td> <td>$sponsor</td>"; }// End of For loop /* The next function is use to populate the Drop Down Menu but I need away to make both work together.*/ // Calling Function For Drop Down Menu Populating_DDM(); function Populating_DDM(){ global $num_results,$result; echo "<select name=searchtype value=''>Sponsor</option>"; // printing the list box select command for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo "<option value=$row[$i]>$row[sponsor]</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box }// End of function Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/#findComment-219281 Share on other sites More sharing options...
marcus Posted April 1, 2007 Share Posted April 1, 2007 For populating a drop down, you can do: <?php $sql = "SELECT * FROM `table`"; $res = mysql_query($sql) or die(mysql_error()); echo "<select name=\"name\">\n"; while($row = mysql_fetch_assoc($res)){ echo "<option value=\"$row[username]\">$row[username]</option>\n"; } echo "</select>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/#findComment-219288 Share on other sites More sharing options...
Trium918 Posted April 1, 2007 Author Share Posted April 1, 2007 Would I be able to select an item from the Menu that will populate a table. Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/#findComment-219293 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/#findComment-219296 Share on other sites More sharing options...
Trium918 Posted April 1, 2007 Author Share Posted April 1, 2007 For populating a drop down, you can do: <?php $sql = "SELECT * FROM `table`"; $res = mysql_query($sql) or die(mysql_error()); echo "<select name=\"name\">\n"; while($row = mysql_fetch_assoc($res)){ echo "<option value=\"$row[username]\">$row[username]</option>\n"; } echo "</select>\n"; ?> It is populating the Drop Down Menu, but I am unable to pull data from it. Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/#findComment-219305 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 If you want someone to write all the code for you then see the freelance section, Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/#findComment-219322 Share on other sites More sharing options...
Trium918 Posted April 1, 2007 Author Share Posted April 1, 2007 If you want someone to write all the code for you then see the freelance section, Why would if(sset($searchtype) > 0) elevate to true? $searchtype = mysql_real_escape_string($_POST['searchtype']); if(isset($searchtype) > 0){ $query="Select sponsor FROM people"; } //$query="Select * FROM people where sponsor='($searchtype)'"; echo "<table border=1 width=50% align=center> <tr> <td>Name</td> <td>Age</td> <td>State</td> <td>Sponsor</td> </tr>"; $result = mysql_query($query); $num_results = mysql_num_rows($result); for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); $name = htmlspecialchars( stripslashes($row["name"])); $age = htmlspecialchars( stripslashes($row["age"])); $state = htmlspecialchars( stripslashes($row["state"])); $sponsor = htmlspecialchars( stripslashes($row["sponsor"])); // Output echo " <tr> <td>$name</td> <td>$age</td> <td>$state</td> <td>$sponsor</td>"; }// End of For loop echo "</tr> </table>"; $query2 = "SELECT * FROM people"; $result2 = mysql_query($query2) or die(mysql_error()); $num_results2 = mysql_num_rows($res); echo "<select name=searchtype >Sponsor</option>"; // printing the list box select command for ($i = 0; $i < $num_results2; $i++) { $row = mysql_fetch_array($result2); echo "<option value=$row[$i]>$row[sponsor]</option>"; //Option values are added by looping through the array } echo "</select>";// Closing of list box Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/#findComment-219334 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 because your setting it.. $searchtype = mysql_real_escape_string($_POST['searchtype']); use if(isset($_POST['searchtype']) > 0){ Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/#findComment-219336 Share on other sites More sharing options...
Trium918 Posted April 1, 2007 Author Share Posted April 1, 2007 If you want someone to write all the code for you then see the freelance section, I cannot learn how to be a good programmer if I was to let someone else do my work. I just want to know if I am on the right path. Can anyone understand what I am trying to do? P.S. This is for School Project Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/#findComment-219338 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 Tip #1 Break the project down in to small parts/projects Tip #2 Break those parts into smaller parts Tip #3 test each part with known data until you get expected results so now you have a drop down list create one which just displays a table with all data then modify it to find defined data, then merge them together Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/#findComment-219344 Share on other sites More sharing options...
Trium918 Posted April 1, 2007 Author Share Posted April 1, 2007 Ok, I got it doing something. My problem is that it will only populate tables with the form inside of the code /code and not the one inside of the php /php. What is the differents when it comes to pulling data from the form inside of the php tags. $searchtype = mysql_real_escape_string($_POST['searchtype']); $query="Select * FROM people where sponsor='$searchtype'"; echo "<table border=1 width=50% align=center> <tr> <td>Name</td> <td>Age</td> <td>State</td> <td>Sponsor</td> </tr>"; $result = mysql_query($query); $num_results = mysql_num_rows($result); for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); $name = htmlspecialchars( stripslashes($row["name"])); $age = htmlspecialchars( stripslashes($row["age"])); $state = htmlspecialchars( stripslashes($row["state"])); $sponsor = htmlspecialchars( stripslashes($row["sponsor"])); // Output echo " <tr> <td>$name</td> <td>$age</td> <td>$state</td> <td>$sponsor</td>"; }// End of For loop echo "</tr> </table>"; $query2 = "SELECT sponsor FROM people"; $result2 = mysql_query($query2) or die(mysql_error()); $num_results2 = mysql_num_rows($result2); // I am still trying to get this working echo "<form method=post>"; echo "<select name=\"searchtype\" > <option>Sponsor</option>"; for ($i = 0; $i < $num_results2; $i++) { $row = mysql_fetch_array($result2); echo "<option value='$row[$sponsor]'>$row[sponsor]</option>"; } echo "</select>"; echo "<input type=\"submit\" value=\"Submit\" />"; echo "</form>"; echo $searchterm; I got it populating tables with this form <form method="post"> <select name="searchtype"> <option value=' '>Sponsor <option value="">Name <option value="33">Age <option value="state">State <option value="Kobe Bryant">Sponsor </select> <input type="submit" value="Submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/45170-solved-phpmysql-menu-populating/#findComment-219430 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.