【问题标题】:PHP and mySQL helpPHP 和 mySQL 帮助
【发布时间】:2011-01-16 22:47:04
【问题描述】:

您好,我有一个数据库,其中有 2 个表,一个是 categoryTable,一个是 userMenuTable,categoryTable 目前有两列,categoryIdcategoryTitle强> 它目前包含 2 行数据, categoryId 的 = 12 以及 categoryTitles = NewsBlog,在 userMenuTable 中,我记录了用户选择的类别,该表有 3 列,menuEntryIdcategoryId cookieId,这个表记录了哪个cookie选择了哪个类别,ID是用来运行这些查询的,

第一次查询,获取用户选择的类别

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();

 }

下一个查询获取所有没有分配 cookieId 的类别,

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";

$query = $this->db->query($sql); 返回 $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 中的 Id 匹配,其中cookieId 匹配用户的,然后返回一个数组,这样我就可以像这样循环遍历它,

 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";
  }

【问题讨论】:

  • 您的第一个查询到底想完成什么?尝试查找没有 cookie id 的类别似乎过于复杂。另外,我不确定您的实际问题是什么?我不明白你的最后一段。
  • 基本上我需要一种方法,以便将表中的类别返回给用户,但如果该类别之前已被用户读取,那么我需要更改 HTML

标签: php database codeigniter mysql


【解决方案1】:

当您在此处运行左连接时,您将在左列中获得所有 categoryTable 项目,在 userMenuTable 列中获得空值。您可以利用这一点,因为尚未保存的类别将包含 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
    )
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    相关资源
    最近更新 更多