Just looking for tips on how to do this.. php/mysql two level deep cats.

Discussion in 'PHP' started by exodus, Jan 4, 2008.

  1. #1
    Looking for tips on how to do a php cat's navigation that can be two or three levels deep. I can do a single level deep, but when it comes to two or three I am lost. Examples? Tutorials online?
     
    exodus, Jan 4, 2008 IP
  2. Millar

    Millar Peon

    Messages:
    334
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You should add to the DB for each catergory a parent field. Then, assign each catergory a parent of either 0 for top level or the id of it's parent. Then when you use the PHP code to run through catergories, look for catergorys for a parent of 0 and for each one, add any cats with a parent of the top level cats ID. This will work infinately to any level.
     
    Millar, Jan 4, 2008 IP
  3. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #3
    I done that before, but it is screwy when you try to go three levels or four levels deep. Is there a example or tutor over it that maybe you know of?
     
    exodus, Jan 4, 2008 IP
  4. Millar

    Millar Peon

    Messages:
    334
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I have found no tutorials, but. If you do it right it will work. GO through each of the catergories from the top level.

    Think of it this way.

    First Top Level Catergory ( ID 1 ) - Looks for all caterogy with it as a parent.
    - First Caterogry with parent as 1 ( ID 5 ) - Looks for all catergories with this as a parent.
    - - A catergory with catergory 5 as parent ( ID 9 ) - Looks for catergories with this as parent (none).
    - Second Catergory with parent as 1 ( ID 6 ) - Looks for catergories with this as parent (none).

    Second Top Level Catergory ( ID 2 ) - Looks for all catergories with it as parent.
    - First Caterogry with parent as 2 ( ID 7 ) - Looks for all catergories with this as a parent (none).

    If done in this manor it will work for any depth of catergories.
     
    Millar, Jan 5, 2008 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    Here is something I wrote for you, just now:

    
    <?php
    
    function print_list($parent,$selected,$xes){
    	$get_cat = mysql_query("SELECT * FROM `cats` WHERE `parent`='$parent'");
    	while($each = mysql_fetch_assoc($get_cat)){
    		if($each['catid'] == $selected){$sel = ' selected';}else{$sel=NULL;}
    		foreach($x<0;$x<=$xes;$x++){ $to_add .= '-';}
    		echo '<option value="'.$each['catid'].'"'.$sel.'>'.$to_add.$each['catname'].'</option>';
    		$to_add=NULL;
    		$xes++;
    		print_list($each['catid'],$selected,$xes);
    		$xes--;
    	}
    	
    }
    
    echo '<select>';
    print_list(0,$_POST['catid'],1);
    echo '</select>';
    ?>
    
    PHP:
    This function works recursively to display the cats in a listbox, and highlight the $selected (in case a form submit). You can edit it to make it display a different format. It also keeps track of current spot to print - infront of title.

    Peace,
     
    Barti1987, Jan 5, 2008 IP
  6. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #6
    So, it calls itself recursivly to get at each level?

    
    print_list($each['catid'],$selected,$xes);
    
    PHP:
    Sweet, looking for something like this in a function type of way. I was doing just with while statements and has to loop for each level doing another while. I was getting confused when I tried to put it in a function.

    See check out the recursive crap I was doing trying to get it done right. :( I am going to see if I can use your function to display it better.

      // Time to list all the categories
      $sql_query = "SELECT * FROM `categories` WHERE `parentcatid` = 0 ORDER BY `catname` ASC";
      $result = mysql_query($sql_query);
      if (mysql_num_rows($result)) {
          while ($row = mysql_fetch_array($result)) {
              $catid = $row['catid'];
              $catname = $row['catname'];
              $catname = stripslashes($catname);
              $catstatus = $row['status'];
              if ($catstatus == 1) {
                  $catstatus = "Active";
              } else {
                  $catstatus = "Inactive";
              }
              
              // Finds out how many games there are in each category
              $sql3 = "SELECT count(*) AS total_games FROM games WHERE category = $catid";
              $result3 = mysql_query($sql3) or die(mysql_error());
              $total = mysql_fetch_array($result3);
              $totalgames = $total['total_games'];
              
              // Displays the category information          
              echo "<tr>";
              echo "<td class='defaulttext'><div align='left'><b>" . $catname . "</b></div></td>";
              echo "<td class='defaulttext'><div align='center'>" . $catstatus . "</div></td>";
              echo "<td class='defaulttext'><div align='center'>" . $totalgames . "</div></td>";
              echo "<td class='defaulttext'><div align='center'><a href='index.php?action=editcat&id=" . $catid . "'>Edit</a> / <a onclick='return confirmSubmitCat()'<a href='index.php?action=deletecat&id=" . $catid . "'>Delete</a> / <a href='index.php?action=managegames&catlist=" . $catid . "'>List Games</a></div></td>";
              echo "</tr>";
              
              $sql_query2 = "SELECT * FROM `categories` WHERE `parentcatid` = " . $row['catid'] . " ORDER BY `catname` ASC";
              $result2 = mysql_query($sql_query2);
              if (mysql_num_rows($result2)) {
                  while ($row2 = mysql_fetch_array($result2)) {
                      $catid = $row2['catid'];
                      $catname = $row2['catname'];
                      $catname = stripslashes($catname);
                      $catstatus = $row2['status'];
                      if ($catstatus == 1) {
                          $catstatus = "Active";
                      } else {
                          $catstatus = "Inactive";
                      }
                      
                      // Finds out how many games there are in each category
                      $sql3 = "SELECT count(*) AS total_games FROM games WHERE category = $catid";
                      $result3 = mysql_query($sql3) or die(mysql_error());
                      $total = mysql_fetch_array($result3);
                      $totalgames = $total['total_games'];
                      
                      // Displays the category information          
                      echo "<tr>";
                      echo "<td class='defaulttext'><div align='left'>>" . $catname . "</div></td>";
                      echo "<td class = 'defaulttext'><div align='center'>" . $catstatus . "</div></td>";
                      echo "<td class = 'defaulttext'><div align='center'>" . $totalgames . "</div></td>";
                      echo "<td class = 'defaulttext'><div align='center'><a href='index.php?action=editcat&id=" . $catid . "'>Edit</a> / <a onclick='return confirmSubmitCat()'<a href='index.php?action=deletecat&id=" . $catid . "'>Delete</a> / <a href='index.php?action=managegames&catlist=" . $catid . "'>List Games</a></div></td>";
                      echo "</tr>";
                      
                      $sql_query4 = "SELECT * FROM `categories` WHERE `parentcatid` = " . $row2['catid'] . " ORDER BY `catname` ASC";
                      $result4 = mysql_query($sql_query4);
                      if (mysql_num_rows($result4)) {
                          while ($row4 = mysql_fetch_array($result4)) {
                              $catid = $row4['catid'];
                              $catname = $row4['catname'];
                              $catname = stripslashes($catname);
                              $catstatus = $row2['status'];
                              if ($catstatus == 1) {
                                  $catstatus = "Active";
                              } else {
                                  $catstatus = "Inactive";
                              }
                              
                              // Finds out how many games there are in each category
                              $sql3 = "SELECT count(*) AS total_games FROM games WHERE category = $catid";
                              $result3 = mysql_query($sql3) or die(mysql_error());
                              $total = mysql_fetch_array($result3);
                              $totalgames = $total['total_games'];
                              
                              // Displays the category information          
                              echo "<tr>";
                              echo "<td class='defaulttext'><div align='left'>>>" . $catname . "</div></td>";
                              echo "<td class = 'defaulttext'><div align='center'>" . $catstatus . "</div></td>";
                              echo "<td class = 'defaulttext'><div align='center'>" . $totalgames . "</div></td>";
                              echo "<td class = 'defaulttext'><div align='center'><a href='index.php?action=editcat&id=" . $catid . "'>Edit</a> / <a onclick='return confirmSubmitCat()'<a href='index.php?action=deletecat&id=" . $catid . "'>Delete</a> / <a href='index.php?action=managegames&catlist=" . $catid . "'>List Games</a></div></td>";
                              echo "</tr>";
                          }
                      }
                  }
              }
          }
      }
    PHP:
     
    exodus, Jan 5, 2008 IP