How can I modify MySQL query from Php, so If I selected ALL, there will not be Where statement, If I select some user login, there will show only this user records?
I need that for example If I selected <option value="All">ALL</option>
Query will be without WHERE statement, But if, for example I selected <option value="admin">Admin</option>
there will be in this query Where tsales_funnel.Darijuma_vaditajs = 'admin'
SQL Statement:
$sql = "Select
tsales_funnel.ID As ID,
wp_users.display_name As Darijuma_vaditajs,
tcportal_starpnieks.Cp_Name As Starpnieks,
tcportal_stucture.Cp_Name As OWCA,
n_products.Product_Nos As Produkts,
tsales_funnel_mrecord.Deadline As Deadline,
tsales_funnel_clients.Reg_nr As Klienta_Regnr,
tfirmas_reg.name_in_quotes As Klients,
tsales_funnel_mrecord.ID As Prod_ID,
tsales_funnel.Statuss As Statuss
From
tsales_funnel Left Join
tsales_funnel_mrecord On tsales_funnel_mrecord.Funnel_ID = tsales_funnel.ID
Left Join
tcportal_starpnieks On tcportal_starpnieks.Cp_code = tsales_funnel.Starpnieks
Left Join
tcportal_stucture On tcportal_stucture.Cp_code = tsales_funnel.OWCA Left Join
tsales_funnel_clients On tsales_funnel_clients.Funnel_ID = tsales_funnel.ID
Left Join
tfirmas_reg On tfirmas_reg.regcode = tsales_funnel_clients.Reg_nr Left Join
n_products On tsales_funnel_mrecord.Product_type = n_products.Product_Code
Left Join
wp_users On tsales_funnel.Darijuma_vaditajs = wp_users.user_login ".$sqlselecteduser."
Order By
Deadline";
GET method from form:
<?php
if(isset($_GET['selecteduser'])){
$selecteduser = esc_sql($_GET['selecteduser']);
} else {
$selecteduser = $_SESSION['selecteduser'];
}
if($selecteduser == 'all') {
$sqlselecteduser = '';
} else {
$sqlselecteduser = " Where tsales_funnel.Darijuma_vaditajs = $selecteduser ";
}
?>
FORM TO SEND GET REQUEST TO THIS PAGE:
<form action="" method="GET" id="selectdo">
<table border="1">
<tr>
<td>
<label for="selector1">Darījuma vadītājs:</label>
</td>
<td>
<select id="selector1" name="selecteduser">
<option value="all">-ALL-</option>
<?php
$sql = "SELECT display_name, user_login FROM wp_users";
$results = $wpdb->get_results($sql); // return an object, not ARRAY_N
if ($results) {
foreach ($results as $row) {
echo "<option value = '".$row->user_login."'>".$row->display_name."</option>";
}}
echo "</select></br>";
?>
</td>
<td><input type="submit" value="ok" /></td>
</tr>
</table>
</form>
After that data displays into table.