Page 1 of 1

dynamic selectbox in for loop

Posted: Tue Mar 09, 2010 1:30 am
by developer.sstpl
<?php
require ("conn.php");
?>

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

<html>

<head>
<title>product list</title>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='exmp.php?pid=' + val ;
}

function reload2(form)
{

var val=form.cat.options[form.cat.options.selectedIndex].value;
var val2=form.subcat.options[form.subcat.options.selectedIndex].value;

self.location='exmp.php?pid=' + val + '&sid=' + val2 ;
}

function reload3(form)
{

var val=form.cat.options[form.cat.options.selectedIndex].value;
var val2=form.subcat.options[form.subcat.options.selectedIndex].value;
var val3=form.type.options[form.type.options.selectedIndex].value;

self.location='exmp.php?pid=' + val + '&sid=' + val2 + '&tid=' + val3;
}

</script>
</head>

<body>
<?php

/**************** Getting the data from Mysql table for first list box************/
$qry1=mysql_query("SELECT * FROM products");
/******************* 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************/

$cat=$_REQUEST['pid'];
if(isset($cat) and strlen($cat) > 0){

$qry2=mysql_query("SELECT * FROM model where pid=$cat");
}
/******** end of query for second subcategory drop down list box ***************/


/***** for Third drop down list we will check if sub category is selected else we will display all the subcategory3****/
$cat3=$_REQUEST['sid'];
if(isset($cat3) and strlen($cat3) > 0){
$qry3=mysql_query("SELECT * FROM type where sid=$cat3");
}
/******** end of query for third subcategory drop down list box ****************/

$cat4=$_REQUEST['tid'];
if(isset($cat4) and strlen($cat4) > 0){
$qry4=mysql_query("SELECT * FROM price where typeid=$cat4");
}

?>
<form method=post name=f1 action=''>
<!--////////// Starting of first drop downlist /////////-->
<table width="60%" border="0">
<tr><td width="16%">
<select name='cat' onChange="reload(this.form)">
<option value=''>Select one</option>
<? while($row1 = mysql_fetch_array($qry1)) {
if($row1['pid']==@$cat){?>
<option selected value='<?=$row1[pid]?>'><?=$row1[p_name]?></option>
<? }
else{?><option value='<?=$row1[pid]?>'><?=$row1[p_name]?></option>
<? }
}
?></select></td>
<!--******************* This will end the first drop down list *****************

**************** Starting of second drop downlist ***********************-->
<td width="16%"><select name='subcat' onChange="reload2(this.form)">
<option value=''>Select one</option>
<? while($row2 = mysql_fetch_array($qry2)) {
if($row2['sid']==@$cat3){?>
<option selected value='<?=$row2[sid]?>'><?=$row2[model]?></option>
<? }
}
?></select></td>
<!--***************** This will end the second drop down list *****************


*******************Starting of third drop downlist **********************-->
<td width="16%"><select name='type' onChange="reload3(this.form)">
<option value=''>Select one</option>
<?php while($row3 = mysql_fetch_array($qry3))
{ if($row3['typeid']==@$cat4){?>
<option selected value='<?php echo $row2[typeid];?>'> <?php echo $row3[type];?> </option>
<?php }
else{?>
<option value='<?php echo $row3[typeid];?>'> <?php echo $row3[type];?> </option>
<?php }
}?>
</select></td>

<!--***************** This will end the third drop down list ***********-->
<td width="52%"><select name='price' >
<?php
while($row4 = mysql_fetch_array($qry4)) { ?>
<option value='<?php echo $row4[amnt];?>'> <?php echo $row4[amnt].' (+ '.$row4[tax].'% tax)';?> </option>
</select>
<input type="text" value="<?php echo $row2[total_amnt]?>" size="8"/>
<? }?></td>
</tr>
</table>
</form>
<!--***************** This will end the third drop down list ****************-->

</body>
</html>


i want to put the selectboxes in a for loop,like ::


for($i=0;$i<=5;$i++){
<tr><td width="16%">
<select name='cat' onChange="reload(this.form)">
<option value=''>Select one</option>
<? while($row1 = mysql_fetch_array($qry1)) {
if($row1['pid']==@$cat){?>
<option selected value='<?=$row1[pid]?>'><?=$row1[p_name]?></option>
<? }
else{?><option value='<?=$row1[pid]?>'><?=$row1[p_name]?></option>
<? }
}
?></select></td>
<!--******************* This will end the first drop down list *****************

**************** Starting of second drop downlist ***********************-->
<td width="16%"><select name='subcat' onChange="reload2(this.form)">
<option value=''>Select one</option>
<? while($row2 = mysql_fetch_array($qry2)) {
if($row2['sid']==@$cat3){?>
<option selected value='<?=$row2[sid]?>'><?=$row2[model]?></option>
<? }
}
?></select></td>
<!--***************** This will end the second drop down list *****************


*******************Starting of third drop downlist **********************-->
<td width="16%"><select name='type' onChange="reload3(this.form)">
<option value=''>Select one</option>
<?php while($row3 = mysql_fetch_array($qry3))
{ if($row3['typeid']==@$cat4){?>
<option selected value='<?php echo $row2[typeid];?>'> <?php echo $row3[type];?> </option>
<?php }
else{?>
<option value='<?php echo $row3[typeid];?>'> <?php echo $row3[type];?> </option>
<?php }
}?>
</select></td>

<!--***************** This will end the third drop down list ***********-->
<td width="52%"><select name='price' >
<?php
while($row4 = mysql_fetch_array($qry4)) { ?>
<option value='<?php echo $row4[amnt];?>'> <?php echo $row4[amnt].' (+ '.$row4[tax].'% tax)';?> </option>
</select>
<input type="text" value="<?php echo $row2[total_amnt]?>" size="8"/>
<? }?></td>
</tr>

}


but the reload function is not working..................

please help...............