2015-02-13 12 views
5

Ho 2 tabellePHP Shopping Cart - pagina Categorie non funziona come voluto

prodotti products

e categorie
categories
(CatParentId 0 è categoria superiore).

Quello che sto cercando di ottenere è quando un utente fa clic sulla categoria padre, deve recuperare i prodotti elencati in quella categoria così come i suoi prodotti di categoria figlio e se l'utente fa clic su una qualsiasi categoria di figlio, dovrebbe recuperare solo i prodotti elencati nella categoria figlio.

Ecco il codice completo che ho usato fino ad ora, ma senza successo:

<section class="col-lg-9 col-md-9 col-sm-9"> 
    <?php 
    $catId = $catName = $n = ""; 
    $id = 0; 
    require_once 'Classes/class.Validation.php'; 
    $validate = new Validation(); 
    if (isset($_GET['name']) && $_GET['name'] != "") { 
     $catName = $_GET['name']; 
     $u = "SELECT CatId, CatName, CatParentId FROM categories WHERE CatName = '".$catName."'"; 
     $validate->Query($u); 
     if ($validate->NumRows() >= 1) { 
      while ($rows = $validate->FetchAllDatas()) { 
       $id = $rows['CatId']; 
       $n = $rows['CatName']; 
       $query = "SELECT 
           c.CatName, 
           p.ProdCode, 
           p.ProdName 
          FROM 
           products p, 
           categories c 
          WHERE 
           c.CatId = p.CatId 
           AND 
           c.CatParentId = '".$id."'"; 
       $validate->Query($query); 
       if ($validate->NumRows() >= 1) { 
        while ($row = $validate->FetchAllDatas()) { 
         // show products here 
        } 
       } else { 
        $query = "SELECT 
            c.CatName, 
            p.ProdCode, 
            p.ProdName 
           FROM 
            products p, 
            categories c 
           WHERE 
            c.CatParentId = p.CatId 
            AND 
            c.CatId = '".$id."' 
           "; 
        $validate->Query($query); 
        if ($validate->NumRows() >= 1) { 
         while ($row = $validate->FetchAllDatas()) { 
          // show products here 
         } 
        } 
       } 

      } 
     } 
    } 
    ?> 
</section> 

Sono abbastanza sicuro che ho un errore logico, ma io non riesco a trovarlo in cui l'ho fatto. Gentilmente aiutami. Qualsiasi aiuto sarà molto apprezzato.

Update 1:

ho risolto da solo. Usato INNER JOIN.

Ecco il codice - Per riferimento futuro: ->

<section class="col-lg-9 col-md-9 col-sm-9"> 
    <?php 
    $catId = $catName = $n = ""; 
    $id = 0; 
    require_once 'Classes/class.Validation.php'; 
    $validate = new Validation('benef8w7_ecommerce'); 
    if (isset($_GET['name']) && $_GET['name'] != "") { 
     $catName = $_GET['name']; 
     $query = "SELECT 
         p.ProdCode, 
         p.ProdRate, 
         c1.CatId, 
         c1.CatName, 
         c2.CatParentId 
        FROM 
         categories c2 
          INNER JOIN 
           categories c1 
          ON 
           c2.CatId = c1.CatParentId 
            INNER JOIN 
             products p 
            ON 
             p.CatId = c1.CatId 
        WHERE 
         c2.CatName = '".$catName."'"; 
     $validate->Query($query); 
     if ($validate->NumRows() >= 1) { 
      while ($row = $validate->FetchAllDatas()) { 
       // show all the products here for both parent and child categories. 
      } 
     } else { 
      $query = "SELECT 
          p.ProdCode, 
          p.ProdName, 
          c.CatId, 
          c.CatParentId, 
          c.CatName 
         FROM 
          products p 
           INNER JOIN 
            categories c 
           ON 
            c.CatId = p.CatId 
         WHERE 
          c.CatName = '".$catName."'"; 
      $validate->Query($query); 
      if ($validate->NumRows() >= 1) { 
       while ($row = $validate->FetchAllDatas()) { 
        // show products here if there are no child categories. 
       } 
      } 
     } 
    } 
    ?> 
</section> 

Ma dopo Update 1, credo che sono venuto con altri bug/errore: ->

Le query di cui sopra all'interno l'aggiornamento 1 funziona correttamente purché non ci siano prodotti nella categoria padre ma ci sono prodotti all'interno della categoria figlio. Se ci sono prodotti nella categoria padre (con categoria figlio), i prodotti della categoria padre sono/non sono mostrati e solo/i prodotti/i di categoria bambino sono mostrati.

Desidero visualizzare tutti i prodotti della categoria padre e anche della categoria figlio se l'utente fa clic sulla categoria padre.

Come posso risolvere questo bug/errore?

+0

Gentilmente mi aiutano fuori. Sto provando questo da 3 giorni .. –

risposta

0

Sono molto nuovo a php e ho pensato di poter imparare qualcosa dal tuo problema. Quindi questa rotonda mi è venuta in mente. Ho usato una sorta di pseudocodice ... Spero che tu possa ottenere l'idea.

SELECT CatId, CatParentId FROM categories WHERE CatName = '$catName'; 

you save CatId and CatParentId to php vars... 

//is it child? 
    if($CatParentId > 0) { 
    SELECT ProdName FROM products WHERE CatId = '$CatId'; 

    //display all products from this child category 
} else { 
    //for when our category is a parent 
    //we need to find how many childs it has. 
    //I suggest using an array of all cat IDs we want to display products from 

    SELECT CatId FROM categories WHERE CatParentID = '$CatId'; 
    //we store it to an array 
    //we cannot forget about our Parent category 
    $categories[x]; 

    //we want to create SELECT 
    //now we need to add php code 
    if(sizeof($categories) == 1){ //our parent has 0 childs 
     SELECT ProdName FROM products WHERE CatId = '$CatId'; 
    } else { 
    SELECT ProdName FROM products WHERE CatId = '$CatId' + 
    for($i = 1; $i <= sizeof($categories); $i++) { //I assume we saved our parent to $categories[0] 
     //here you add remaining conditions 
     "OR CatId = '$categories[i]"; 
    } 
     + add last ";" 
    } 
} 

Odio innerjoins:]

divertirsi e spero che anche una buone risposte programmatore in modo da ottenere una valutazione:]