I am querying a MySQL database for some records by the query which limit result max records to 4, but i want add to this result all records which has "keep_actual" column set on 1 but excludes all previous founded records. What is the best way to do this?
// Sample query which return those 4 i need
// Result should be those 4 + all from current $cat which has keep_actual=1 but not are in those first 4
$news = $ed->getByLanguage($lang_id, null, false, 4,false,true);
public function getByLanguage($lang, $cat=null, $main=true, $limit=null, $hasImage=false, $isTopped=false) {
if ($cat == null) {
$where = "language_id=$lang";
} else {
$where = "language_id=$lang and category_id=$cat";
}
if ($main == false && $isTopped==false) {
$where.=" and main=0";
}
if ($hasImage == true) {
$where.=" and main_picture is not null";
}
if($cat==12){
$limit=10;
}
//Nastavuje, zda se ma zobrazovat top nebo ne(na uvodni strane a aktualne)
if($isTopped==true)
$where.=" and top = 1";
//=========================================================
//$where.=" and entry.date >= '".date("Y-m-d h:i:s", mktime(0, 0, 0, 1, 1, date("Y")))."' and entry.date <= '".date("Y-m-d h:i:s")."'";
if ($limit == 1) {
//nahledy
return parent::selectOne($this->sc->select("entry", "main_picture,url,name,abstract,date,top", $where, null, "date", $limit));
} else {
//main clanek
return parent::selectAll($this->sc->select("entry", "*", $where, null, "date", $limit));
}
}
public function selectAll($sql) {
$res = mysql_query($sql);
if ($res) {
$a = array();
$i = 0;
while ($r = mysql_fetch_assoc($res)) {
// print_r($r);
foreach ($r as $k => $v) {
$a[$i][$k] = $v;
}
$i++;
}
return $a;
} else {
return false;
}
}
Okay, this is 'generic' code, you need to adapt it to your needs, but this is how it should work:
$res = mysqli_query($sql, "SELECT * FROM `table` WHERE `Condition` = 1 LIMIT 4");
$results = array();
while($row = mysqli_fetch_array($res)) $results[] = $row;
$query = "SELECT * FROM `table` WHERE `keep_actual` = 1 AND ID NOT IN (";
for($i = 0; $i < 4; ++$i) $query .= "$row[ID], ";
$query .= "0)"; //To terminate the list of IDs
$res = mysqli_query($sql, $Query);
This code SHOULD give you what you're looking for, but it's completely untested and rather abstract.