2012-08-12 64 views
0

下面的php函數只能用於一個用戶記錄,並不是所有的用戶記錄在weekly_email_f1列中的值爲「1」,任何想法?php函數沒有循環遍歷所有用戶,只是第一個用戶

function getSummaryEmailInfo() { 
    $sql = " 
     SELECT u.id, u.master_id, u.email 
     FROM user AS u 
     WHERE u.weekly_email_fl = 1 
    "; 
    $query = $this->db->query($sql); 
    $users = $query->result_array(); 

    foreach ($users as &$user) { 
     $user_id = ($user['master_id'] == '0') ? $user['id'] : $user['master_id']; 

     $sql = " 
     SELECT ui.custom_name 
     FROM user_item AS ui 
     WHERE ui.user_id = '" . $user_id . "' 
     AND added_to_list_dt IS NOT NULL 
     ORDER BY added_to_list_dt DESC 
     ; 
     "; 
     $query = $this->db->query($sql); 
     $user['s_list'] = $query->result_array(); 
    } 
    return $users; 
    } 

有多個用戶與weekly_email_f1 = 1,我覺得有什麼不對與result_array()如何;被填充,但無法弄清楚,可是...

function getUserInfo($user_id) { 
    $query = $this->db->query(" 
     SELECT name AS first_name, email, zip_code, weekly_email_fl 
     FROM user 
     WHERE id = '" . $user_id . "' 
    ;"); 
    if ($query->num_rows() > 0) { 
     $results = $query->result_array(); 
     $results = $results[0]; 
    } else { 
     $results = null; 
    } 
    return $results; 
    } 
+3

有你測試的功能,無需在foreach循環來檢查問題是否是內部或外部的循環? – 2012-08-12 17:53:14

回答

0
  1. 只有1 weekly_email_f1用戶= 1
  2. $查詢 - > result_array();僅返回第一/最後/一行。
+0

謝謝,增加了一些更多的細節...多個用戶有weekly_email_f1 = 1。感謝布拉克 – user1263003 2012-08-12 23:09:49

1
while($users = $query->result_array()) { 
    /* You can use $users['master_id'] here */ 
} 
+0

。那麼,所有當前的「foreach」代碼應該位於括號之間? {} – user1263003 2012-08-13 02:17:44

+0

對不起,您必須刪除foreach,而$ users ['master_id']將正常工作。 – 2012-08-13 10:03:11