Hi there, i'm currently making a image gallery and i need help for the Categories listing. In my database, i've a column named Categorie which is exemple Abstracts, Womens, Mens, Animal/Bears, Animal/Birds, Animal/Cow etc.. **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:
Wouldn't it be best to denormalize and have a separate table for categories and in this table just have a category ID?
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:
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
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:
GEATTT!!! this is working! I was on that since 4 hours!!!!!! Thanks a lot dude, finaly.. its quite simple heu..?! 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:
<?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: