Jump to content

How get query of all table columns with drop down list?


shams

Recommended Posts

Hi,

This is  a code dd.php with two drop down list options, when select the value from the first drop down  it retrieves the related  values from the column subcategory from the second table subcategory and prents only these two columns:

Value of $cat = 2
Value of $subcat = user@gmx.ca

My question is how to pass the value of $subcat to the query below in the same dd.php page and prints the full row of  the table  subcategory which has more 4 columns:

SELECT * FROM subcategory  WHERE subcategory=?;

this is dd.php:

<?php
require 'config.php';  // Database connection
//////// End of connecting to database ////////
?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Multiple drop down list box from plus2net</title>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;
}

</script>
</head>

<body>
<?Php

@$cat=$_GET['cat']; // Use this line or below line if register_global is off
if(strlen($cat) > 0 and !is_numeric($cat)){ // to check if $cat is numeric data or not. 
echo "Data Error";
exit;
}



///////// Getting the data from Mysql table for first list box//////////
$quer2="SELECT DISTINCT category,cat_id FROM category order by category"; 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
if(isset($cat) and strlen($cat) > 0){
$quer="SELECT DISTINCT * FROM subcategory where cat_id=$cat order by subcategory"; 
}else{$quer="SELECT DISTINCT * FROM subcategory order by subcategory"; } 
////////// end of query for second subcategory drop down list box ///////////////////////////

echo "<form method=post name=f1 action='dd-check.php'>";
/// Add your form processing page address to action in above line. Example  action=dd-check.php////
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
foreach ($dbo->query($quer2) as $noticia2) {
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
echo "<select name='subcat'><option value=''>Select one</option>";
foreach ($dbo->query($quer) as $noticia) {
echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";


//////////////////  This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";
while ($row = $quer->fetch(PDO::FETCH_ASSOC))
{

    echo  ('cat_id =' . $row['cat_id'] . "\r\n" . 'subcategory=' . $row['subcategory']. "\r\n". 'user=' . $row['user'] . "\r\n". 'passw=' . $row['passw']. "\r\n". 'server=' . $row['server']  . '<br />');

}

?>
<br><br>
<a href=dd.php>Reset and start again</a>
<br><br>
<center><a href='http://www.plus2net.com' rel="nofollow">PHP SQL HTML free tutorials and scripts</a></center> 
</body>

</html>

 

Link to comment
Share on other sites

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.