Using an html FORM
let's convert $core
in 100
and $mhz
in 1000
emag
and koyos
are table rows
So if $core
is set $parameters['emag'] = "$core";
is emag=100
else it is null
AND
and WHERE
are dinamically setted to appear, so the problem is that I collect all data of variables in $parameters[]
and foreach()
them.
With my code I am get exactly what $core
is. This is the code:
if (!empty($core)) {
$parameters['emag'] = "$core";
}
if (!empty($mhz)) {
$parameters['koyos'] = $mhz;
}
$sql = "SELECT * FROM $tbl_name WHERE 1=1 ";
if (!empty($parameters)) {
foreach ($parameters as $k => $v) {
$sql .= " AND " . $k . "='" . $v . "'";
}
}
$sql .= " ORDER BY emag, performanta_cpu_core DESC, performanta_cpu DESC LIMIT $start, $limit";
And it is results just rows with emag=100
, but I have need all numbers equals or little than 100
not just 100
Your query conditions, based on the data you've provided, are:
WHERE 1=1 AND emag = '100' AND koyos = '1000'
And so obviously will only show rows where emag
= 100
. If you want to show all those up to 100
then change =
to <=
when the table name is emag
:
foreach ($parameters as $k => $v) {
if ($k == 'emag')
{
$sql .= " AND " . $k . "<='" . $v . "'";
}
else
{
$sql .= " AND " . $k . "='" . $v . "'";
}
}
Try this:
foreach ($parameters as $k => $v) {
if ($k == 'emag') {
$sql .= " AND " . $k . "<='" . $v . "'";
continue;
}
$sql .= " AND " . $k . "='" . $v . "'";
}