Answer the question
In order to leave comments, you need to log in
How to process data?
Goodnight!
I can't figure out how to process the data.
The bottom line is that there are n-th number of pairs of fields in the form:
<input type="text" name="text[god][1]" value="">
<input type="text" name="text[god][2]" value="">
foreach ($_POST["text"] as $key3 => $value3) {
foreach ($value3 as $key4 => $value4) {
if($value4 != '') {
if($key4 == 1) $wheres1 = $key3.' >= '.$value4;
if($key4 == 2) $wheres2 = $key3.' <= '.$value4;
}
}
if ($wheres1!='' && $wheres2!='') {
$wheres = '('.$wheres1.' AND '.$wheres2.')';
}else{
if($wheres1!='') $wheres = $wheres1;
if($wheres2!='') $wheres = $wheres2;
}
}
Answer the question
In order to leave comments, you need to log in
To begin with, I would recommend that you familiarize yourself with prepared statements - php.net/manual/ru/pdo.prepared-statements.php
And then, use some kind of query builder, like framework.zend.com/manual/current/en/modules/ zend....
and use it like this:
if ($condition) {
$query->where('blabla = ?', $blabla)
} else {
$query->where('foo = ?', $foo);
}
Let's say I collected your request
$test_data['text']['god'][1] = 1234;
$test_data['text']['god'][2] = 5678;
$test_data['text']['cat'][1] = 111;
$test_data['text']['cat'][2] = 222;
$test_data['text']['meow'][2] = 888; // there is only one "meow"
$conditions = [];
foreach ($test_data['text'] as $fieldname => $values) {
$subconditions = [];
if (!empty($values[1])) $subconditions[] = $fieldname." >= ".intval($values[1]);
if (!empty($values[2])) $subconditions[] = $fieldname." <= ".intval($values[2]);
if(!empty($subconditions)) $conditions[] = '('.implode(' AND ', $subconditions).")\n";
}
$sql = "SELECT * FROM tbl_name\n";
if (!empty($conditions)) $sql .= 'WHERE '.implode(' AND ', $conditions);
echo '<pre>' . $sql;
/*
SELECT * FROM tbl_name
WHERE (god >= 1234 AND god <= 5678)
AND (cat >= 111 AND cat <= 222)
AND (meow <= 888)
*/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question