I have a PHP script which displays categories on the home page and categories with subcategories on subpages. I want subcategories to appear on the home page, and in this format: Category subcat1, subcat2, subcat3... This is the code: //create categories $qc = "select * from dd_categories order by CategoryName"; $rc = mysql_query($qc) or die(mysql_error()); if(mysql_num_rows($rc) > '0') { while($ac = mysql_fetch_array($rc)) { //get the items number at this category $qin = "select count(*) from dd_items where ItemCategory = '$ac[CategoryID]' and ItemStatus = 'approved' "; $rin = mysql_query($qin) or die(mysql_error()); $ain = mysql_fetch_array($rin); $categories .= "<img src=\"images/arrow.gif\" alt=\">\" /><a class=\"title\" href=\"ShowCategory.php?CategoryID=$ac[CategoryID]\">$ac[CategoryName] ($ain[0])</a><br />\n\t\t\t"; if($_GET[CategoryID] == $ac[CategoryID]) { $categories .= "<ul style=\"margin-top:0; margin-bottom:0; margin-left:15\">"; //get the subcategories $qsc = "select * from dd_subcategories where CategoryID = '$_GET[CategoryID]' order by SubcategoryName "; $rsc = mysql_query($qsc) or die(mysql_error()); if(mysql_num_rows($rsc) > '0') { while($asc = mysql_fetch_array($rsc)) { //get the items number at this subcategory $qin2 = "select count(*) from dd_items where ItemCategory = '$_GET[CategoryID]' and ItemSubcategory = '$asc[SubcategoryID]' and ItemStatus = 'approved' "; $rin2 = mysql_query($qin2) or die(mysql_error()); $ain2 = mysql_fetch_array($rin2); $categories .= "<li><a href=\"ShowCategory.php?CategoryID=$ac[CategoryID]&SubcategoryID=$asc[SubcategoryID]\">$asc[SubcategoryName] ($ain2[0])</a></li>"; } } $categories .= "</ul>"; } } } PHP: I'd appreciate any advice to get this to work, it doesn't have to work on the subpages so don't worry that you'll break anything with any suggestions for me to try, as I already coded up some custom code for subcats on subpages. Thanks.
Is this what you're looking for? //create categories $qc = "select * from dd_categories order by CategoryName"; $rc = mysql_query($qc) or die(mysql_error()); if(mysql_num_rows($rc) > '0') { while($ac = mysql_fetch_array($rc)) { //get the items number at this category $qin = "select count(*) from dd_items where ItemCategory = '$ac[CategoryID]' and ItemStatus = 'approved' "; $rin = mysql_query($qin) or die(mysql_error()); $ain = mysql_fetch_array($rin); $categories .= "<img src=\"images/arrow.gif\" alt=\">\" /><a class=\"title\" href=\"ShowCategory.php?CategoryID=$ac[CategoryID]\"><b>$ac[CategoryName]</b></a><br />\n\t\t\t"; //get the subcategories $qsc = "select * from dd_subcategories where CategoryID = '$ac[CategoryID]' order by SubcategoryName "; $rsc = mysql_query($qsc) or die(mysql_error()); if(mysql_num_rows($rsc) > '0') { $categories .= "<span>"; while($asc = mysql_fetch_array($rsc)) { // add comma after subcategory if(!is_null($subcategories)) $subcategories .= ", "; //get the items number at this subcategory $qin2 = "select count(*) from dd_items where ItemCategory = '$ac[CategoryID]' and ItemSubcategory = '$asc[SubcategoryID]' and ItemStatus = 'approved' "; $rin2 = mysql_query($qin2) or die(mysql_error()); $ain2 = mysql_fetch_array($rin2); $subcategories .= "<a href=\"ShowCategory.php?CategoryID=$ac[CategoryID]&SubcategoryID=$asc[SubcategoryID]\">$asc[SubcategoryName]</a>"; } $categories .= $subcategories; $categories .= "</span>"; $subcategories = NULL; } } } PHP:
smatts9 - It's listing all my categories correctly on my homepage, I just wanted the addition of three subcats to display on the homepage as well, along with the categories. WMBurg - I will check this tonight when I'm done working and let you know how it goes. Thanks for the help!
Thanks! It worked like a charm! I have one more thing I'd like to do with this list. Can we split the categories into two columns so it breaks half-way? I need it in two divs, side-by-side. <div class="left_side">categories</div> <div class="right_side">categories</div> The template calls <?=$categories?> to list them, it's calling the same code above. So any idea how to split it and have it always split right in the middle of the list even when new categories are added? Thanks so much!
Cool. Which way do you want to do it? <div class="left_side"> category1<br /> sub, sub </div> <div class="right_side"> category2<br /> sub, sub </div> <div class="left_side"> category3<br /> sub, sub </div> <div class="right_side"> category4<br /> sub, sub </div> ETC. or <div class="right_side"> category1 <br /> sub, sub <br /><br /> category2 <br /> sub, sub <br /><br /> category3 <br /> sub, sub <br /><br /> category4 <br /> sub, sub <br /><br /> </div> <div class="left_side"> category5 <br /> sub, sub <br /><br /> category6 <br /> sub, sub <br /><br /> category7 <br /> sub, sub <br /><br /> category8 <br /> sub, sub <br /><br /> </div>
I have it like so. <p>Category<br /> <span>subcat, subcat, subcat...</span></p> <p>Category<br /> <span>subcat, subcat, subcat...</span></p> <p>Category<br /> <span>subcat, subcat, subcat...</span></p> So I want. <div id="left"> <p>Category<br /> <span>subcat, subcat, subcat...</span></p> <p>Category<br /> <span>subcat, subcat, subcat...</span></p> <p>Category<br /> <span>subcat, subcat, subcat...</span></p></div> <div id="right"> <p>Category<br /> <span>subcat, subcat, subcat...</span></p> <p>Category<br /> <span>subcat, subcat, subcat...</span></p> <p>Category<br /> <span>subcat, subcat, subcat...</span></p></div> Thanks! I have it how I want it coded already, just need help with the php integrating it into the script.
Ok try this out Using [ code ] instead of PHP. I noticed [ php ] is a pain in the ass to copy/paste. Firefox copies the line #s and IE doesn't copy the carriage returns. Does anyone know a workaround for this? //create categories $qc = "select * from dd_categories order by CategoryName"; $rc = mysql_query($qc) or die(mysql_error()); //category count $cat_count = mysql_num_rows($rc); //split category count in half $cat_split = ceil($cat_count/2); //increment var $cat_increment = 0; if(mysql_num_rows($rc) > '0') { while($ac = mysql_fetch_array($rc)) { //get the items number at this category $qin = "select count(*) from dd_items where ItemCategory = '$ac[CategoryID]' and ItemStatus = 'approved' "; $rin = mysql_query($qin) or die(mysql_error()); $ain = mysql_fetch_array($rin); //increment category loop count $cat_increment = $cate_increment + 1; //display appropriate div if($cat_increment == 1) $categories .= "<div id=\"left\">"; if($cat_increment == $cat_split+1) $categories .= "</div><div id=\"right\">"; //display category name $categories .= "<p><img src=\"images/arrow.gif\" alt=\">\" /><a class=\"title\" href=\"ShowCategory.php?CategoryID=$ac[CategoryID]\"><b>$ac[CategoryName]</b></a><br />\n\t\t\t"; //get the subcategories $qsc = "select * from dd_subcategories where CategoryID = '$ac[CategoryID]' order by SubcategoryName "; $rsc = mysql_query($qsc) or die(mysql_error()); if(mysql_num_rows($rsc) > '0') { $categories .= "<span>"; while($asc = mysql_fetch_array($rsc)) { // add comma after subcategory if(!is_null($subcategories)) $subcategories .= ", "; //get the items number at this subcategory $qin2 = "select count(*) from dd_items where ItemCategory = '$ac[CategoryID]' and ItemSubcategory = '$asc[SubcategoryID]' and ItemStatus = 'approved' "; $rin2 = mysql_query($qin2) or die(mysql_error()); $ain2 = mysql_fetch_array($rin2); $subcategories .= "<a href=\"ShowCategory.php?CategoryID=$ac[CategoryID]&SubcategoryID=$asc[SubcategoryID]\">$asc[SubcategoryName]</a>"; } $categories .= $subcategories; $categories .= "</span>"; $subcategories = NULL; } $categories .= "</p>"; // end right div if($cat_increment == $cat_count) $categories .= "</div>"; } } Code (markup):
That's much easier to copy and paste, thanks! Ok, this didn't work, it's doing this: <div class="left_side"><p><img src="images/arrow.gif" alt=">" /><a class="title" href="category.php"><b>Category 1</b></a><br /></p> <div class="left_side"><p><img src="images/arrow.gif" alt=">" /><a class="title" href="category.php"><b>Category 2</b></a><br /></p> So it's still one long list and never calls the code for the right side div.
WMBurg, thanks for the help. You're my hero. Tell me what I can do for you. I posted this problem in 3 other forums and no one helped, they posted code despite the fact that I said I am new to this and wouldn't know how to integrate it.