I am trying to create a filter search script.
My plan is to select the first filter, after the filtering to select the second filter. But, the $_SESSION doesn’t store the information sent by form.
http://techtest.great-site.net/check_product_filtered1.php?number=0
<?php
session_start();
include "connection.php";
echo "<link href='/css/table.css' rel='stylesheet' type='text/css'>";
function droplist($filter) { //function
include "connection.php";
// communication with SQL
$sql = "SELECT DISTINCT {$filter} FROM warehouse";
$result = $conn -> query($sql);
echo "<form action='{$_SERVER['PHP_SELF']}' method='GET'>";
echo "<select name='{$filter}' id='{$filter}' onchange='this.form.submit()'>
<option>{$filter}</option>";
foreach ($conn -> query($sql) as $row) {
echo "<option value=$row[$filter]>$row[$filter]</option>";
}
echo "</select>";
echo "<noscript><input type='submit' value='submit'></noscript>";
echo "</form";
mysqli_close($conn);
} //function
$_SESSION["location"] = $_GET["location"];
$_SESSION["number"] = $_GET["number"];
$sql = "SELECT * FROM warehouse WHERE 1=1";
if($_SESSION["location"] !="") {
$sql .= " AND location='{$_SESSION['location']}'";
echo $_SESSION["location"];
}
if($_SESSION["number"] !="") {
$sql .= " AND number='{$_SESSION['number']}'";
echo $_SESSION["number"];
}
$result = $conn -> query($sql);
//Display data from MySql
if ($result->num_rows > 0) { //the function num_rows() checks if there are more than zero rows returned.
// output data of each row
?>
<table class='table'>
<tr id='header'>
<th> <?php droplist("location"); ?> </th>
<th> <?php droplist("number"); ?> </th>
<th> <?php droplist("product"); ?> </th>
<th> <?php droplist("packing"); ?> </th>
<th>quantity</th>
<th>net</th>
<th>unit</th>
<th>gross</th>
<th>batch</th>
<th>pallet</th>
<th>height</th>
<th>date</th>
</tr>
<?php
while($row = $result->fetch_assoc()) { //the function fetch_assoc() puts all the results into an associative array that we can loop through
//The while() loop loops through the result set and outputs the data from the id, firstname and lastname columns.
echo "<tr id='tr'>";
echo "<td>" .$row["location"]. "</td>";
echo "<td>" .$row["number"]. "</td>";
echo "<td>" .$row['product']. "</td>";
echo "<td>" .$row['packing']. "</td>";
echo "<td>" .$row['quantity']. "</td>";
echo "<td>" .$row['net']. "</td>";
echo "<td>" .$row['unit']. "</td>";
echo "<td>" .$row['gross']. "</td>";
echo "<td>" .$row['batch']. "</td>";
echo "<td>" .$row['pallet']. "</td>";
echo "<td>" .$row['height']. "</td>";
$date=date_create($row['date']);
echo "<td>" .date_format($date,'M Y'). "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "none results";
}
$conn->close();
?>