2014-04-22 30 views
1

我想從4個消息後面選擇表中的最後12個。 我試過,但沒有results.I得到未定義$新聞SQL - select id其中id不在數組中

public function get_all_news_home() 
{ 
    $news = array(); 
    # ne conectam la baza de date 
    $this->load->database(); 
    # selectam stirile de top 
    $last_news_query = $this->db->query("SELECT * FROM News WHERE Type = 1 AND Ready='Y' ORDER BY Date DESC LIMIT 4"); 
    $last_news = $this->db->query($last_news_query); 
    $last_news = ($last_news->num_rows()) ? $last_news->result_array() : NULL; 
    foreach ($last_news as $ln) 
    { 
     array_push($news,array('id' => $ln['ID'])); 
    } 

    $all_news = $this->db->query("SELECT * FROM News WHERE News.Ready = 'Y' AND News.ID NOT IN ('$news') ORDER BY Date DESC LIMIT 12"); 

    if($all_news->num_rows()) 
    { 
     $all_news = $all_news->result_array(); 
    } 
    else 
    { 
     $all_news = NULL; 
    } 
    return $all_news; 
} 

幫我please.How我可以排除第4號

+0

你缺少一線分號在'$新聞=陣列();'。 – Rikesh

+0

在代碼中存在 – user3532183

+0

並從函數get_all_news_home中獲取'$ news'變量的位置? – Rikesh

回答

1

試試這個:

... 
$newsIds = implode(',', array_values($news)); 
...  
$all_news = $this->db->query("SELECT * FROM News WHERE News.ID NOT IN ('$newsIds') ORDER BY Date DESC LIMIT 12"); 
... 
+0

此解決方案不起作用 – user3532183

+0

無結果... :( – user3532183

0

只需使用限制第

...

$last_news_query = $this->db->query("SELECT * FROM News WHERE Type = 1 AND Ready='Y' ORDER BY Date DESC LIMIT 0,4"); 

...

$all_news = $this->db->query("SELECT * FROM News WHERE News.Ready = 'Y' AND News.ID NOT IN ('$news') ORDER BY Date DESC LIMIT 4,12"); 

...