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.

 

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

 

 

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

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.

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 

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

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

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>

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.