1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

MYSQL Group By problem

Discussion in 'PHP' started by MesCasinos, Aug 20, 2010.

  1. #1
    Hi there, i'm currently making a image gallery and i need help for the Categories listing. :confused:

    In my database, i've a column named Categorie which is exemple Abstracts, Womens, Mens, Animal/Bears, Animal/Birds, Animal/Cow etc..

    [​IMG]

    **The if and else if only for my lnaguages file, that make the categories translated

    In my website i can list the categories fine but its listing exemple:

    Abstracts
    Animal/Bears
    Animal/Birds
    Animal/Cow
    [...]
    Anime
    Nature
    Movies
    [...]

    The result i expect is:

    Abstracts
    Animal
    [...]
    Anime
    Nature
    Movies
    [...]

    Here is my code:

    
    $result_cat = mysql_query("SELECT categorie FROM wallpapers GROUP BY categorie ASC");
    
        <?php
    	while ($row = mysql_fetch_array($result_cat)){
    		?>
    	  <tr>
          <?php
    	  // str les catégories pour etre utilisable avec les fichiers langs
    	  if (strstr($row['categorie'], '/')){ //si la categorie est Animal/subfolder
    		  $catStr = str_replace('/', '_', $row['categorie']);
    		  $str_sub="\$categorie=&\$cat_".$catStr.";";
    	      eval($str_sub);
    	  }else{ //si categorie est normal ex.: Animal ou Sexy
    		  $str_sub="\$categorie=&\$cat_".$row['categorie'].";";
    	      eval($str_sub);
    	  }
    	  ?>
          <td><a href='?cat=<?php echo $row['categorie']; ?>'><span style='color:#CCC; font-size:20px'><?php echo $categorie; ?></span></a></td>
          </tr>
    	<?php
    	}
    	?>
    
    
    PHP:

     
    MesCasinos, Aug 20, 2010 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Wouldn't it be best to denormalize and have a separate table for categories and in this table just have a category ID?
     
    exam, Aug 20, 2010 IP
  3. BackOfTheNet

    BackOfTheNet Peon

    Messages:
    344
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this new code dude it will do the trick basically use http://php.net/manual/en/function.str-replace.php its a function in PHP that allows you to replace anything in a string I use this function frequently :)
    New Code
    $result_cat = mysql_query("SELECT categorie FROM wallpapers GROUP BY categorie ASC");
    
        <?php
        while ($row = mysql_fetch_array($result_cat)){
            ?>
          <tr>
          <?php
          // str les catégories pour etre utilisable avec les fichiers langs
          if (strstr($row['categorie'], '/')){ //si la categorie est Animal/subfolder
              $catStr = str_replace('/', '_', $row['categorie']);
              $str_sub="\$categorie=&\$cat_".$catStr.";";
              eval($str_sub);
          }else{ //si categorie est normal ex.: Animal ou Sexy
              $str_sub="\$categorie=&\$cat_".$row['categorie'].";";
              eval($str_sub);
          }
          $CatTitle = str_replace("Animal/", "", "$categorie");
          ?>
          <td><a href='?cat=<?php echo $row['categorie']; ?>'><span style='color:#CCC; font-size:20px'><?php echo  $CatTitle; ?></span></a></td>
          </tr>
        <?php
        }
        ?>
    PHP:
     
    BackOfTheNet, Aug 20, 2010 IP
  4. MesCasinos

    MesCasinos Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Mm yeah the problem is theres other cat and subcat, theres not only Animal/..., theres exemple Cars/Porsche etc..

    But thanks for took the time to try ;)
     
    MesCasinos, Aug 20, 2010 IP
  5. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #5
    So you just want a category listing that only shows the main categories and no sub-categories?

    
    <?php
    
    $result_cat = mysql_query("SELECT DISTINCT categorie FROM wallpapers GROUP BY categorie ASC");
    
    $cats = array();
    while ($row = mysql_fetch_array($result_cat))
    {
    	list($main_category) = explode('/', $row['categorie']);
    	$cats[$main_category] = $main_category;
    }
    
    echo '<ul>';
    foreach ($cats as $cat)
    {
    	echo '<li><a href="?cat=' . $cat . '">' . $cat . '</a></li>';
    }
    echo '</ul>';
    
    ?>
    
    
    PHP:
     
    exam, Aug 20, 2010 IP
  6. MesCasinos

    MesCasinos Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    GEATTT!!! this is working! I was on that since 4 hours!!!!!! :cool:

    Thanks a lot dude, finaly.. its quite simple heu..?! :p

    Hey dude, i'm looking for a while(few days) a code that only tell me if the directory contain subfolder or not. I've the code for listing the dirs, without the . and .. and no files, only dirs.

    I've the idea to make a count and go with a if dir count is <= 1 or something like this, but im not sure how to make this count..

    im good with database and other stuff but arrays and stuff like that.. not yet. Thanks!

    Here is the final code for people who have the same problem:

    //$result_cat = mysql_query("SELECT categorie FROM wallpapers GROUP BY categorie ASC");
    $result_cat = mysql_query("SELECT  categorie FROM wallpapers GROUP BY categorie ASC");
    ?>
    
    <?php
    $cats = array();
    while ($row = mysql_fetch_array($result_cat)){
        list($main_category) = explode('/', $row['categorie']);
        $cats[$main_category] = $main_category;
    }
    ?>
    <?php
    foreach ($cats as $cat){
    	$str_sub="\$categorie=&\$cat_".$cat.";";
    	eval($str_sub);
    ?>
    <tr>
    <td><a href='?cat=<?php echo $cat; ?>'><span style='color:#CCC; font-size:20px'><?php echo $categorie; ?></span></a></td>
    </tr>
    <?php
    }
    ?>
    
    PHP:
     
    MesCasinos, Aug 20, 2010 IP
  7. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?php
    
    $result_cat = mysql_query("SELECT DISTINCT categorie FROM wallpapers GROUP BY categorie ASC");
    
    $cats = array();
    while ($row = mysql_fetch_array($result_cat))
    {
    	list($main_category, $first_sub_category) = explode('/', $row['categorie']);
    	if (!isset($cats[$main_category]))
    	{
    		$cats[$main_category] = 0;
    	}
    	if (!empty($first_sub_category))
    	{
    		++$cats[$main_category];
    	}
    }
    
    echo '<ul>';
    foreach ($cats as $cat => $sub_cat_count)
    {
    	echo '<li><a href="?cat=' . $cat . '">' . $cat . '</a> Subcategories: ' . $sub_cat_count . '</li>';
    }
    echo '</ul>';
    
    ?>
    PHP:
     
    exam, Aug 20, 2010 IP
  8. MesCasinos

    MesCasinos Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks again!! :)
     
    MesCasinos, Aug 20, 2010 IP