Displaying Categories?

Discussion in 'PHP' started by adamjblakey, Jun 22, 2007.

  1. #1
    Hi,

    I have a database where the table is set out like this:

    id | title | category

    There is about 1000 records in there and i want to be able to create a list of the categories but if i do a foreach statement i just get the same categories over and over.

    How can i do this so that if the category is the same as another then it will only display the one and not multiple of the same category.

    Cheers,
    Adam
     
    adamjblakey, Jun 22, 2007 IP
  2. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    don't do this in php, use a mysql query. The next queries will both return the different categories (each category only 1 time)
    SELECT DISTINCT ( category ) FROM mytable
    Code (markup):
    or:
    SELECT category FROM mytable GROUP BY category
    Code (markup):
     
    UnrealEd, Jun 22, 2007 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    I can't believe i did not know about that sql statement.

    I guess you really do learn something new everyday.

    Thanks a lot :)
     
    adamjblakey, Jun 22, 2007 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Sorry to be a pain but just have one other question. I also have next to category a subcategory field. How would i go about listing the subcategories under the main category?

    I tried doing this:

    $select = "SELECT subcategory FROM recipes WHERE category = '$row[category]' GROUP BY subcategory";
    Code (markup):
    But does not output anything.

    Any ideas?
    Cheers,
    Adam
     
    adamjblakey, Jun 22, 2007 IP
  5. nanolab

    nanolab Active Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Can you show recipes table structure?
     
    nanolab, Jun 22, 2007 IP
  6. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    try this:
    $select = "SELECT subcategory FROM recipes WHERE category = '" . $row['category'] . "' GROUP BY subcategory";
    PHP:
    or use the DISTINCT query
     
    UnrealEd, Jun 22, 2007 IP
  7. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #7
    Still does not work :(

    I now get this error:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 66

    Here is all the code. It is setup so that each category is added to a new colum so there is 3 colums going down with a category in each. Now i want the sub categories under the main category.

    
    
    <table width="690" border="0" cellspacing="0" cellpadding="0">
    						<?php
    												
    					$selects = "SELECT category FROM recipes GROUP BY category";
    					$result = mysql_query($selects);
    
    					while ($res = mysql_fetch_array($result)) {
    						$resu[] = $res;	
    					}
    					mysql_free_result($result);
    					
    						$item=0;	
    						foreach ($resu AS $row) {
    
    							if($item==0){print "\t<tr><td><tr>\n";}
                      ?>
    
      <td bgcolor="#E8F3BE"><table width='220' border='0' cellspacing='0' cellpadding='0'>
        
        <tr>
          <td valign="top"><table width='100%' border='0' cellspacing='0' cellpadding='8'>
            <tr>
              <td ><h1> <a href="recipe-details.php?id=<?php echo"".$row['id'].""; ?>"><?php echo"<span class='categories'>".$row['category']."</span>"; ?></a></h1> 			<?php
    												
    					$selectc = "SELECT subcategory FROM recipes WHERE category = '" . $row['category'] . "' GROUP BY subcategory";
    					$resultc = mysql_query($selectc);
    
    					while ($resc = mysql_fetch_array($resultc)) {
    						$resuc[] = $resc;	
    					}
    					mysql_free_result($resultc);
    	
    						foreach ($resuc AS $rowc) {
    
                      ?>
    				  <a href="recipe-details.php?id=<?php echo"".$rowc['id'].""; ?>"><?php echo"<span class='categories'>".$rowc['category2']."</span>"; ?></a>
    				  <?php
    				  }
    				  ?>
    				  </td>
            </tr>
          </table></td>
        </tr>
      </table></td>
          <?php
    				$item++;
    				if($item==3){$item=0;}
    				if($item==0){print "\t</tr>\n";}
    					}
    				?>
                    </table>
    Code (markup):

    Cheers,
    Adam
     
    adamjblakey, Jun 22, 2007 IP
  8. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #8
    Why the "group by"? Doesn't seem to make sense there. Shouldn't you be grouping by the parent, if anything?

    By the way, your script uses far too many queries. I'd use the one:
    $selects = "SELECT category, subcategory FROM recipes ORDER BY category ASC, subcategory ASC";
    Code (markup):
    Then, when looping, add a check (if category != last category, start a new category heading), otherwise just show the subcategory. The "last category" would be a variable set at the end of each loop.
     
    krt, Jun 22, 2007 IP
  9. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #9
    I know i was going about it the wrong way :S

    So know it looks like this but am unsure on where the if statement fits in?

    <table width="690" border="0" cellspacing="0" cellpadding="0">
    						<?php
    												
    					$selects = "SELECT category, category2 FROM recipes ORDER BY category ASC, category2 ASC";
    					$result = mysql_query($selects);
    
    					while ($res = mysql_fetch_array($result)) {
    						$resu[] = $res;	
    					}
    					mysql_free_result($result);
    					
    						$item=0;	
    						foreach ($resu AS $row) {
    
    							if($item==0){print "\t<tr><td><tr>\n";}
                      ?>
    
      <td bgcolor="#E8F3BE"><table width='220' border='0' cellspacing='0' cellpadding='0'>
        
        <tr>
          <td valign="top"><table width='100%' border='0' cellspacing='0' cellpadding='8'>
            <tr>
              <td >
    		
    		  <h1> <a href="recipe-details.php?id=<?php echo"".$row['id'].""; ?>"><?php echo"<span class='categories'>".$row['category']."</span>"; ?></a></h1> 			
    				  </td>
            </tr>
          </table></td>
        </tr>
      </table></td>
          <?php
    				$item++;
    				if($item==3){$item=0;}
    				if($item==0){print "\t</tr>\n";}
    					}
    				?>
                    </table>
    Code (markup):
    Cheers,
    Adam
     
    adamjblakey, Jun 22, 2007 IP
  10. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #10
    Let's start over with that section ;)
    $last_category = '';
    foreach ($resu AS $row) {
    
        if($item % 3 == 0)
            print "  <tr>\n";
    
        if ($row['category'] != $last_category) {
    ?>
        <td bgcolor="#E8F3BE">
          <h1><?php echo $row['category']; ?></h1>
          <p>
    <?php
        }
    ?>
          <a href="recipe-details.php?id=<?php echo"".$row['id'].""; ?>">
            <?php echo"<span class='categories'>".$row['subcategory']."</span>"; ?>
          </a>
    <?php
        if ($row['category'] != $last_category) {
    ?>
          </p>
        </td>
    <?php
            $item++;
        }
    
        if($item % 3 == 0)
            print "  </tr>\n";
        $last_category = $row['category'];
    }
    Code (markup):
    I don't intersperse PHP/HTML often so that could look cleaner. Anyway, it should work and should be easy to customise to how you want it.
     
    krt, Jun 22, 2007 IP
  11. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #11
    I know sorry about this, i think some people ie me was not meant for coding i.e me.

    Right i have tried what you suggested but still have problems is it just repeating the categories over and over again. This is the code now with your code i have added.

    <table width="690" border="0" cellspacing="0" cellpadding="0">
    						<?php
    												
    					$selects = "SELECT category, category2 FROM recipes ORDER BY category ASC, category2 ASC";
    					$result = mysql_query($selects);
    
    					while ($res = mysql_fetch_array($result)) {
    						$resu[] = $res;	
    					}
    					mysql_free_result($result);
    					
    						$item=0;	
    						$last_category = '';
    foreach ($resu AS $row) {
    
        if($item % 3 == 0)
            print "  <tr>\n";
    
        if ($row['category'] != $last_category) {
    ?>
        <td bgcolor="#E8F3BE">
          <h1><?php echo $row['category']; ?></h1>
          <p>
    <?php
        }
    ?>
          <a href="recipe-details.php?id=<?php echo"".$row['id'].""; ?>">
            <?php echo"<span class='categories'>".$row['category2']."</span>"; ?>
          </a>
    <?php
        if ($row['category'] != $last_category) {
    ?>
          </p>
        </td>
    <?php
            $item++;
        }
    
        if($item % 3 == 0)
            print "  </tr>\n";
        $last_category = $row['category'];
    }
    				?>
                    </table>
    Code (markup):
     
    adamjblakey, Jun 22, 2007 IP
  12. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #12
    Anyone help me on this, almost there :)
     
    adamjblakey, Jun 22, 2007 IP
  13. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #13
    change the query to this:
    "SELECT category, DISTINCT(category2) FROM recipes ORDER BY category ASC, category2 ASC"
    Code (markup):
     
    UnrealEd, Jun 23, 2007 IP
  14. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #14
    Just tried that but getting the following error :(

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/red/public_html/index.php on line 44
    
    Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/red/public_html/index.php on line 47
    
    Warning: Invalid argument supplied for foreach() in /home/red/public_html/index.php on line 51
    Code (markup):
     
    adamjblakey, Jun 23, 2007 IP
  15. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #15
    replace this:
    $result = mysql_query($selects);
    PHP:
    by this:
    $result = mysql_query($selects) or die ( mysql_error ( ) );
    PHP:
    the errors you got mean that there's an error when you try to run the query in mysql_query. mysql_error returns the error that occured when running the query. If you're debugging your scripts, it's best to add the "or die ( mysql_error ( ) )" to every query you run
     
    UnrealEd, Jun 24, 2007 IP
  16. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #16
    Thank you for your reply and sorry to bother you further but i am getting another error:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT(category2) FROM recipes ORDER BY category ASC, category2 ASC' at line 1
     
    adamjblakey, Jun 24, 2007 IP
  17. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #17
    Almost there? Any ideas?
     
    adamjblakey, Jun 25, 2007 IP
  18. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #18
    lol: i tested it on my server, and the only thing you need to change is switch the order of fields. not:
    category, DISTINCT(category2)
    Code (markup):
    bu:
    DISTINCT(category2), category
    Code (markup):
     
    UnrealEd, Jun 25, 2007 IP
  19. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #19
    Thank you for your further reply UnrealEd

    It just does not want to work for me though.

    This is the code that i am using:

    <table width="690" border="0" cellspacing="0" cellpadding="0">
    						<?php
    												
    					$selects = "SELECT DISTINCT(category2), category FROM recipes ORDER BY category ASC, category2 ASC";
    					$result = mysql_query($selects) or die ( mysql_error ( ) );
    
    					while ($res = mysql_fetch_array($result)) {
    						$resu[] = $res;	
    					}
    					mysql_free_result($result);
    					
    						$item=0;	
    						$last_category = '';
    foreach ($resu AS $row) {
    
        if($item % 3 == 0)
            print "  <tr>\n";
    
        if ($row['category'] != $last_category) {
    ?>
        <td bgcolor="#E8F3BE">
          <h1><?php echo $row['category']; ?></h1>
          <p>
    <?php
        }
    ?>
          <a href="recipe-details.php?id=<?php echo"".$row['id'].""; ?>">
            <?php echo"<span class='categories'>".$row['category2']."</span>"; ?>
          </a>
    <?php
        if ($row['category'] != $last_category) {
    ?>
          </p>
        </td>
    <?php
            $item++;
        }
    
        if($item % 3 == 0)
            print "  </tr>\n";
        $last_category = $row['category'];
    }
    				?>
                    </table>
    Code (markup):
    But what it is doing now is just listing the cats and sub cats all over and not putting the main cats as a heading and listing the cat2 under the main heading it is associated with.

    Have a look here: foundrecipes.com

    Cheers for your help so far.
     
    adamjblakey, Jun 26, 2007 IP
  20. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #20
    Would it be easier if i uploaded a snipet of the database and the page at all?
     
    adamjblakey, Jun 27, 2007 IP