function addWhere($where, $add, $and=true)
{
if ($where) {
if ($and) $where .= " AND $add";
else $where .= " OR $add";
}
else $where = $add;
return $where;
}
function filter($dateStart, $dateEnd, $typ, $categ, $account, $organ = null)
{
session_start();
$idUser=$_SESSION['id'];
$dateStart= htmlspecialchars($_POST['fromDate']);
$dateEnd= htmlspecialchars($_POST['beforeDate']);
$typ= htmlspecialchars($_POST['filterTyp']);
$categ= htmlspecialchars($_POST['filterCateg']);
$account= htmlspecialchars($_POST['filterAccpunt']);
$organ= htmlspecialchars($_POST['filterOrganiz']);
if (empty($_POST['filter'])) {
if($_POST['fromDate']) $where=addWhere($where, "transactions.data >='$fromDate'");
else $where=addWhere($where, "transactions.data >='".date('Y-m-01')."'");
if($_POST['beforeDate']) $where=addWhere($where, "transactions.data <'$beforeDate'");
if($_POST['filerTyp']) $where=addWhere($where, "transactions.typ = '$typ'");
if($_POST['filerCateg']) $where=addWhere($where, "transactions.category = '$categ'");
if($_POST['filterAccpunt']) $where=addWhere($where, "score.score = '$account'");
if($_POST['filterOrganiz']) $where=addWhere($where, "organization.organization = '$organ'");
$sqlTrans="SELECT transactions.typ, transactions.data, transactions.Sum,transactions.comment, score.score, Categoria.category, subcategory.subcategory, organization.organization
FROM transactions
LEFT JOIN score ON score.idScore = transactions.idScore
LEFT JOIN Categoria ON Categoria.idCateg = transactions.idCateg
LEFT JOIN subcategory ON subcategory.idSubCat = transactions.idSubCat
LEFT JOIN organization ON organization.idOrg = transactions.idOrg
WHERE
transactions.idUser='$idUser'";
$second=" ORDER BY transactions.data ASC";
if ($where) $sqlTrans .= " $where".$second;
else $sqlTrans .= " ".$second;
$queryTrans=mysql_query($sqlTrans) or die(mysql_error());
$transactions=array();
while($rowTrans = mysql_fetch_array($queryTrans))
{
$transactions[]=$rowTrans;
}
mysql_close();
return $transactions;
}
И где сам клиент делает эту фильтрацию
<div id="controls">
<form id="form1" method="POST" action="transactions.php">
<!--Фильтр-->
<?php
$transactions = filter($dateStart, $dateEnd, $typ, $categ, $account, $organ);
print_r($_POST);
?>
<div id="filter">
<div>
<input type="submit" name="filter" id="filterButtom" value="Фильтровать">
<label for="fromDate">От </label><input type="date" name="fromDate" id="fromDate">
<label for="beforeDate">До </label><input type="date" name="beforeDate" id="beforeDate">
</div>
<div id="filterExtended">
<div id="filterTyp">
<select name="filterTyp">
<option selected>Тип операции</option>
<option value="Доход">Доход</option>
<option value="Расход">Расход</option>
<option value="Перевод">Перевод</option>
</select>
</div>
<div>
<select id="filterCateg" name="filterCateg" class="filterCateg">
<option selected>Категорию</option>
<?php foreach ($categoria as $categ): ?>
<option value="<?=$categ['idCateg']?>"><?=$categ['category']?></option>
<?php endforeach; ?>
</select>
</div>
<select name="filterAccount" id="filterAccount">
<option selected>
Счет</option>
<?php foreach ($score as $scores): ?>
<option value="<?=$scores['idScore']?>"><?=$scores['score']?></option>
<?php endforeach; ?>
</select>
<input type="text" name="filterOrganiz" id="filterOrganiz" list="organList" placeholder="Организация">
<datalist id="organList">
<?php foreach ($organization as $organ): ?>
<option value="<?=$organ['organization']?>"></option>
<?php endforeach; ?>
</datalist>
</div>
<div id="dataView">
<?php dateView();?>
</div>
</div>
<table cellpadding="2" cellspacing="0" border="0" id="table_id" class="display">
<thead>
<tr>
<th><input type="checkbox" name="cb_all"></th>
<th>Дата</th>
<th>Тип</th>
<th>Счет</th>
<th>Категория</th>
<th>Подкатегория</th>
<th>Организация</th>
<th>Сумма</th>
<th>Комментарий</th>
</tr>
</thead>
<tbody>
<?php foreach ($transactions as $transact): ?>
<tr>
<td><input type="checkbox" name="cb" class ="qwe"></td>
<td><?=date('d.m', strtotime($transact['data']))?></td>
<td><?=$transact['typ']?></td>
<td><?=$transact['score']?></td>
<td><?=$transact['category']?></td>
<td><?=$transact['subcategory']?></td>
<td><?=$transact['organization']?></td>
<td><?=$transact['Sum']?></td>
<td><?=$transact['comment']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
<script>
allCheckbox()
sorter()
</script>
</div>
</div>