Answer the question
In order to leave comments, you need to log in
(php) How to organize the execution of two conditions with the output of one result?
In general, I have the following problem.
I need to organize a report. I made the following criteria by which it is compiled - description (search by words), owner (sorting), cost (from-to), dimensions (from-to), condition (from worst to best).
It is necessary to display records from the database according to these criteria, if they are selected. In general, that's what I did. When one of the criteria is selected, the output proceeds as it should. I did this on the basis of one function, but depending on the condition in which it is executed, the variable that is responsible for sorting, the data that is displayed, changes in it. But the following problem arose - when selecting several checkboxes, the result is displayed for two queries - that is, two results are sequential. It is necessary for me, that one priority result was deduced. There are a lot of massive outputs in the function, so they cannot be thrown into the condition. The output is produced by foreach and require of the template file, which specifies the record variables.
Problem of two results:
Code for more understanding:
// Получение данных через POST.
if (@$_REQUEST['repHandle']) {
function entReport($sql) {
if(isset($_POST['entTextValue'])) {
// Коннект к БД.
$cn_db = new cn_db();
// Получение результата.
$row = $cn_db->select_list($sql);
if(count($row)) {
$end_result = '';
foreach($row as $r) {
$result = $r['ent_text'];
$bold = '<span class="found">'.$word.'</span>';
$entResult = sql_sec('
SELECT *, UNIX_TIMESTAMP(ent_stamp) AS ent_stamp
FROM '.TBLENT.'
WHERE ent_text="'.mysql_real_escape_string(str_replace(strtolower($word), $bold, $result)).'"
ORDER BY ent_stamp DESC
') or die(mysql_error());
for ($entBase = array(); $entRow = mysql_fetch_array($entResult); $entBase[] = $entRow);
require_once "templates/default/rprt_top.tpl";
foreach ($entBase as $entElement) {
require "templates/default/rprt.tpl";
}
echo "</tr>";
}
echo $end_result;
} else {
echo '<li>По вашему запросу ничего не найдено</li>';
}
}
}
if(isset($_POST['entTextOn']) && $_POST['entTextOn'] == 'On') {
$word = mysql_real_escape_string($_POST['entTextValue']);
$sql = "SELECT * FROM entries WHERE ent_text LIKE '%".$word."%' ORDER BY ent_text LIMIT 10";
entReport($sql);
} else {
print "none!";
}
if(isset($_POST['entCreatorOn']) && $_POST['entCreatorOn'] == 'On') {
$sql = "SELECT * FROM entries WHERE ent_text LIKE '%".$word."%' ORDER BY ent_creator LIMIT 10";
entReport($sql);
} else {
print "none!";
}
}
Answer the question
In order to leave comments, you need to log in
You need to write a query constructor:
if(){}
elseif(){}
else{}
$query = "SELECT * FROM entries WHERE ent_text LIKE '%".$word."%' ORDER BY $sort $limit";
$sql = "$query";
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question