2010-02-15 129 views
0

你好我有一個其中有2個表的數據庫,一個是categoryTable和是userMenuTable,該categoryTable目前在這兩個列,的categoryIdcategoryTitle 它目前持有2行的數據,該的categoryId年代= 和和categoryTitles = 新聞博客,在該userMenuTable我一直認爲用戶已經選擇了哪些類別的記錄,該表有3列,menuEntryId的categoryIdCookie編號,該表記錄哪些cookie擁有的類別選擇,該ID是再運行這些查詢的記錄,PHP和MySQL幫助

的第一個查詢,獲取用戶選擇的類別

function getMenu($cookieId) { 
    $this->db->select('*'); 
    $this->db->from('categoryTable'); 
    $this->db->join('userMenuTable', 'categoryTable.categoryId = userMenuTable.categoryId', 'left'); 
    $this->db->where('userMenuTable.cookieId', $cookieId); 

    $query = $this->db->get(); 
    return $query->result_array(); 

} 

下一個查詢獲取所有沒有分配給他們的Cookie編號類別,

function getAllMenus($cookieId) { 
$sql ="SELECT categoryTable.categoryTitle, categoryTable.categoryId, userMenuTable.cookieId, userMenuTable.menuEntryId, categoryTable.categoryOnline, 
categoryTable.categoryIsSpecial, categoryTable.categoryDateCreated, categoryTable.categorySlug, categoryTable.dashboardUserId, categoryTable.categoryAbstract 
FROM categoryTable LEFT JOIN userMenuTable 
    ON categoryTable.categoryId = userMenuTable.categoryId 
UNION ALL 
SELECT categoryTable.categoryTitle, categoryTable.categoryId, userMenuTable.cookieId, userMenuTable.menuEntryId, categoryTable.categoryOnline, 
categoryTable.categoryIsSpecial, categoryTable.categoryDateCreated, categoryTable.categorySlug, categoryTable.dashboardUserId, categoryTable.categoryAbstract FROM categoryTable RIGHT JOIN userMenuTable 
    ON categoryTable.categoryId = userMenuTable.categoryId 
WHERE userMenuTable.cookieId = NULL"; 

$查詢= $這個 - > DB->查詢($ SQL); return $ query-> result_array(); }

然而,這返回一個數組,看起來像這樣,

[0] => Array 
    (
     [categoryId] => 1 
     [categoryTitle] => blog 
     [categoryAbstract] => <p>asdsdsadasdsadfdsgdgdsgdsgssssssssssss</p> 
     [categorySlug] => blog 
     [categoryIsSpecial] => 0 
     [categoryOnline] => 1 
     [categoryDateCreated] => 1265123745 
     [dashboardUserId] => 0 
     [menuEntryId] => 5 
     [cookieId] => bang4b696152b4869 
    ) 

[1] => Array 
    (
     [categoryId] => 8 
     [categoryTitle] => News 
     [categoryAbstract] => <p>The world at Bang Marketing moves fast, keep up to date w 
     [categorySlug] => news 
     [categoryIsSpecial] => 0 
     [categoryOnline] => 1 
     [categoryDateCreated] => 1265283717 
     [dashboardUserId] => 0 
     [menuEntryId] => 6 
     [cookieId] => bang4b696152b4869 
    ) 

[2] => Array 
    (
     [categoryTitle] => blog 
     [categoryId] => 1 
     [cookieId] => bang4b696152b4869 
     [menuEntryId] => 5 
     [categoryOnline] => 1 
     [categoryIsSpecial] => 0 
     [categoryDateCreated] => 1265123745 
     [categorySlug] => blog 
     [dashboardUserId] => 0 
     [categoryAbstract] => <p>asdsdsadasdsadfdsgdgdsgdsgssssssssssss</p> 
    ) 

[3] => Array 
    (
     [categoryTitle] => News 
     [categoryId] => 8 
     [cookieId] => bang4b696152b4869 
     [menuEntryId] => 6 
     [categoryOnline] => 1 
     [categoryIsSpecial] => 0 
     [categoryDateCreated] => 1265283717 
     [categorySlug] => news 
     [dashboardUserId] => 0 
     [categoryAbstract] => <p>The world at Bang Marketing moves fast, keep up to date w 
    ) 
) 

不知怎的,我需要建立一個返回所有都在categoryTable和檢查,對那裏的ID匹配的類別查詢一個是在userMenuTable其中Cookie編號匹配的用戶,然後通過像這樣返回一個數組,所以我可以循環,

if(isset($mainMenu)) { 
    //die(print_r($mainMenu)); 
    foreach ($mainMenu as $k => $v) { 
    if($v['menuEntryId'] == '') { 
    echo "<li class='menuItem'> 
    <a href='".base_url()."welcome/getContent/$v[categoryId]' class='navLink' id='$v[categoryTitle]'>".$v['categoryTitle']."</a> 
    </li>"; 
    } else { 
    echo "<li class='menuItem'> 
    <a href='".base_url()."welcome/getContent/$v[categoryId]' class='saved navLink' id='$v[categoryTitle]'>".$v['categoryTitle']."</a> 
    </li>"; 
    } 
    } 
    } else { 
    // do something else 
    //echo "here"; 
    } 
+0

究竟是你想用你的第一個查詢完成什麼?似乎過於複雜,試圖找到沒有Cookie ID的類別。另外,我不確定你的實際問題是什麼?我不明白你的最後一段。 – thetaiko

+0

基本上我需要一種方法,以便將表中的類別返回給用戶,但如果用戶以前已經讀過類別,那麼我需要更改HTML – Udders

回答

0

在此處運行左連接時,您將獲得左列中的所有categoryTable項和userMenuTable列中的null值。您可以使用它來獲得優勢,因爲尚未保存的類別將爲userMenuTable列包含空值。

SELECT categoryTable.*, userMenuTable.* 
FROM categoryTable LEFT JOIN userMenuTable ON categoryTable.categoryID = userMenuTable.categoryID 
WHERE userMenuTable.cookieID == '{$cookieID}' OR userMenuTable.cookieID IS NULL; 

這可能給你一個結果,看起來是這樣的:

[0] => Array 
    (
     [categoryId] => 1 
     [categoryTitle] => blog 
     [categoryAbstract] => <p>asdsdsadasdsadfdsgdgdsgdsgssssssssssss</p> 
     [categorySlug] => blog 
     [categoryIsSpecial] => 0 
     [categoryOnline] => 1 
     [categoryDateCreated] => 1265123745 
     [dashboardUserId] => 0 
     [menuEntryId] => 5 
     [cookieId] => bang4b696152b4869 
    ) 

[1] => Array 
    (
     [categoryId] => 8 
     [categoryTitle] => News 
     [categoryAbstract] => <p>The world at Bang Marketing moves fast, keep up to date w 
     [categorySlug] => news 
     [categoryIsSpecial] => 0 
     [categoryOnline] => 1 
     [categoryDateCreated] => 1265283717 
     [dashboardUserId] => 0 
     [menuEntryId] => 6 
     [cookieId] => bang4b696152b4869 
    ) 

[2] => Array 
    (
     [categoryId] => 9 
     [categoryTitle] => Extra 
     [categoryAbstract] => <p>Another category you haven't saved 
     [categorySlug] => extra 
     [categoryIsSpecial] => 0 
     [categoryOnline] => 1 
     [categoryDateCreated] => 1265283717 
     [dashboardUserId] => 0 
     [menuEntryId] => NULL 
     [cookieId] => NULL 
    ) 
)