Jump to content

[SOLVED] PHP/MySql Menu Populating


Trium918

Recommended Posts

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

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.