PHP ve MySQL Yardım

1 Cevap php

Hello there I have a database that has 2 tables in it, one is the categoryTable and the is the userMenuTable, the categoryTable currently has two columns in it, categoryId and categoryTitle it currently holds 2 rows of data, the categoryId's = 1 and 2 and the categoryTitles = News and Blog, in the the userMenuTable I keep a record of what categories that user has selected, the table has 3 columns, menuEntryId, categoryId and cookieId, this table keeps a record of which cookie has which category selected, the ID is to then run these queries,

İlk sorgu, kullanıcıların seçilen kategorileri alır

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

 }

Sonraki sorgu, kendilerine atanmış hiçbir cookieId tüm kategoriler alır

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); return $query->result_array(); }

ancak bu gibi görünen bir dizi döndürür,

[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
    )
)

Her nasılsa orada karşı bu Kimliği userMenuTable nerede bir tane eşleşen olduğunu tüm categoryTable olan kategoriler ve kontrolleri döndüren bir sorgu oluşturmak için gereken {[(2) ]}, kullanıcıların bu maçlar, ve sonra bu gibi yoluyla elimden bu kadar döngü bir dizi döndürür

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

1 Cevap

Bir sol çalıştırdığınızda userMenuTable sütunları sol sütun ve null değerleri tüm categoryTable öğeleri alırsınız burada katılın. Kaydedilmemiş kategoriler userMenuTable sütunlar için null değerleri içerir çünkü sizin yararınıza kullanabilirsiniz.

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

Bu, şuna benzer bir sonuç verebilir:

[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
    )
)