Im using Mysql for my database but how do i include more than 1 chained dropdown boxes to this coding because my sql tables also includes things such as statement and location other than commodity.By the way this coding is actually taken from this forum and edited but i cant seem to find any solution in including more than one chained dropdown boxes. Im sorry because i am still new to php and mysql.
<?php
/*
* Testing configuration
*/
define('ENVIR',0);
define('ENVIR_DEV',0);
define('ENVIR_LIVE',1);
/*
* Data base credentials (replace this with your db credentials)
*/
define('DB_USER','root');
define('DB_PWD','Password');
define('DB_HOST','localhost');
define('DB_NAME','database');
/*
* Connect to database
*/
try {
$db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USER,DB_PWD);
} catch(PDOExeption $e) {
if(ENVIR == ENVIR_DEV) {
echo '<p>',$e->getMessage(),'</p>';
}
echo '<p>Unable to connect to database<p>';
exit;
}
/*
* Template variables
*/
$tpl = array(
'filter'=>array(
'#action' => $_SERVER['SCRIPT_NAME']
,'#method' => 'get'
,'commodity' => array(
'#values'=>array(
array('value'=>'','label'=>'All')
)
)
)
,'grid'=>array(
'commodity'=>array()
)
);
/*
* Populate form filter last name options
*/
$stmt = $db->query('SELECT Commodity FROM my30_rsl GROUP BY Commodity ORDER BY Commodity ASC');
if($stmt === false) {
echo '<p>Unable to populate required data to build page.</p>';
exit;
}
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
$tpl['filter']['commodity']['#values'][] = array(
'label' => $row['Commodity']
,'value' => $row['Commodity']
,'selected' => isset($_GET['filter'],$_GET['filter']['commodity']) && $_GET['filter']['commodity'] == $row['Commodity']
);
}
/*
* Populate user grid
*/
$stmt = $db->prepare(sprintf(
'SELECT Commodity,SupplierName FROM my30_rsl %s'
, isset($_GET['filter'],$_GET['filter']['commodity']) && !empty($_GET['filter']['commodity'])?'WHERE Commodity = :commodity1':''
));
if($stmt === false) {
echo '<p>Unable to populate required data to build page.</p>';
exit;
}
$stmt->execute(isset($_GET['filter'],$_GET['filter']['commodity']) && !empty($_GET['filter']['commodity'])?array(':commodity1'=>$_GET['filter']['commodity']):array());
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
$tpl['grid']['commodity'][] = $row;
}
/*
* Start template output
*/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Supplier Dictionary</title>
</head>
<body>
<!-- user filter template -->
<form action="<?php echo $tpl['filter']['#action']; ?>" method="<?php echo $tpl['filter']['#method']; ?>">
<fieldset>
<legend>Filters</legend>
<ul>
<li>
<label for="filter-commodity">Commodity</label>
<select name="filter[commodity]" id="filter-commodity">
<?php
foreach($tpl['filter']['commodity']['#values'] as &$option) {
printf(
'<option value="%s"%s>%s</option>'
,htmlentities($option['value'])
,$option['selected']?' selected':''
,htmlentities($option['label'])
);
}
?>
</select>
</li>
<li>
<input type="submit" name="filter[submit]" value="Filter Commodity">
</li>
</ul>
</fieldset>
</form>
<!-- data grid template -->
<table>
<caption>Supplier Dictionary</caption>
<thead>
<tr>
<th>Supplier</th>
<th>Commodity</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($tpl['grid']['commodity'])) {
foreach($tpl['grid']['commodity'] as &$name) {
printf(
'<tr>
<td>%s</td>
<td>%s</td>
</tr>'
,htmlentities($name['SupplierName'])
,htmlentities($name['Commodity'])
);
}
} else {
echo '<tr><td colspan="2">No commodity available</td></tr>';
}
?>
</tbody>
</table>
</body>
</html>