I have a select option form that is self referencing, How do I assign the SQL query to each select option? so when the user selects, that selection queries the database for that specific table and outputs to page. I have a select option for each table in the database and have created the related queries. Do I need to put the queries into an array? I am stuck on how to advance further. I have included some of the code below. Do I need to create a unique SQL query var for each user option? Any direction would be helpful. Thanks
//this is the first query it gets the count for pagination
$sql = "SELECT COUNT(productCode) FROM products"
or die ('Cannot Select database');
// This query is just to get the total count of rows
//$sql = "SELECT COUNT(productCode) FROM products";
$query = $dbc->query($sql);
$row = $query->fetch_row();
//this is were I am stuck
if($_POST['productCode'] == ('productCode')) {
// query to get code records
$sql2 = "SELECT productCode FROM products";
}
elseif($_POST['productName'] == 'productName') {
// query to get product name records
$sql3 = "SELECT productName FROM products";
} else {
// query to get all records
$sql4 = "SELECT * FROM products";
}
$list = '';
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$id = $row['productCode'];
$name = $row['productName'];
$line = $row['productLine'];
$scale = $row['productScale'];
$vendor = $row['productVendor'];
$description = $row['productDescription'];
$buy_price = $row['buyPrice'];
$quanity = $row['quantityInStock'];
$msrp = $row['MSRP'];
$list .= "<table>
<tr>
<td class='center'><strong>Product Id</strong></td>
</tr>
<tr>
<td class='center'>$id</td>
<td>$name</td>
<td>$product_line</td>
<td>$scale</td>
<td>$vendor</td>
<td>$description</td>
<td>$buy_price</td>
<td>$quanity</td>
<td>$msrp</td>
</tr>
</table>";
}
<select name="value">
<option value="<?=$id; ?>">Product ID</option>
<option value="<?=$name; ?>">Product Name</option>
<option value="<?=$line; ?>">Product Line</option>
<option value="<?=$scale; ?>">Product Scale</option>
<option value="<?=$vendor; ?>">Product Vendor</option>
<option value="<?=$description; ?>">Product Description</option>
<option value="<?=$quanity; ?>">Quanity In Stock</option>
<option value="<?=$buy_price; ?>">Buy Price</option>
<option value="<?=$msrp; ?>">MSRP</option>
</select>
<?php
//SQL table rows system names => 'human names for select'
$rows4Search = array(
'productCode' => 'Product Code',
'productName' => 'Product Name',
'productLine' => 'Product Line',
'productScale' => 'Product Scale',
);
//build html select options
foreach ( $rows4Search as $key=>&$val ){
$opinonsString .= '<option value="'.$key.'" '.( $_POST['wattafind']==$key ? 'selected' : '' ).' >'.$val.'</option>';
}
//form
echo '
<form method=POST>
<select name="wattafind">
<option>All fields</option>
'.$opinonsString.'
</select>
<input type="submit">
</form>
';
//catch selected option & put it to your query
if ( $_POST['wattafind'] && array_key_exists( $_POST['wattafind'], $rows4Search) )
$sql = 'SELECT `'.$_POST['wattafind'].'` FROM products';
else
$sql = 'SELECT * FROM products';
echo "<pre>".print_r($sql,true)."</pre>";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.