2015-08-03 30 views
0

我有兩個模型爲數組設置。基本上,我想要實現的是根據我設置的訂單ID從數據庫中獲取第一個下一個條目。獲取數組中的下一個元素(PHP)

因此,我發送ID 4,我找到ID爲4的訂單ID爲15的條目。然後,我想獲取它後面的第一個下一個項目,它應該有訂單ID 16.我嘗試遞增在訂單ID之後加+1,但它不起作用。

用我目前的代碼,我得到了兩次相同的數組。

function first($id_domain) { 
     $this->db->select ('*'); 
     $this->db->from ('domains'); 
     $this->db->where ('id_domain', $id_domain); 
     $result = $this->db->get(); 
     return $result->result_array(); 

    } 

    function second ($result) { 
     $this->db->select ('*'); 
     $this->db->from ('domains'); 
     $this->db->where ('order', $result[0]['order'] + 1); //code that should get me the next entry, but doesn't work... 
     $this->db->where ('parent', $result[0]['parent']); 
     $result2 = $this->db->get(); 
     return $result2->result_array(); 

    } 
+0

你嘗試打印' $ result [0] ['order']'來檢查它是一個數字還是你的號碼? –

+0

是的,它是回聲2. – Dragos

+0

您收到數據庫錯誤還是隻有0結果?如果是的話,確實有ID 3的訂單?此外,'$ result [0] ['parent']'條件是指什麼? – Kez

回答

0

的問題是不是因爲你的代碼,但它可能是由於在數據庫中的記錄:他們要麼是不存在的特定條件或您的匹配是不完全正確的。

如果您正在使用CodeIgniter我建議你改變你的second功能如下:

function second($result) { 
    $whereConds = array(
     'order' => intval($result[0]['order'] + 1), 
     'parent'=> $result[0]['parent'] 
    ); 
    //if you don't have firephp print/echo/var_dump as you normally would 
    $this->firephp->log($whereConds); 

    $query = $this->db->get_where('domains', $whereConds); 

    $this->firephp->log($this->db->last_query()); 
    if ($query->num_rows() <= 0) { 
     $this->firephp->log('No results'); 
     return array(); 
    } else { 
     return $query->result_array(); 
    } 
} 

這樣你就可以準確地跟蹤問題。

順便說一句,你也可以(只是與家長的條件也許,因爲你知道,它們是順序排列)使用$offset參數在get_where拿到一張ID:

$limit=1; 
$offset=intval($result[0]['order'] + 1);//adjust depending on the parent condition below: it can be just an integer such as 2 
$whereConds = array(
    'parent'=> $result[0]['parent'] 
); 
$query = $this->db->get_where('domains', $whereConds, $limit, $offset); 
相關問題