PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old September 9th, 2002, 07:04 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 25
Help with catagories and sub-catagories! Confused.

Hello everyone. I know this is my first post but I have been at this
for awhile now and cannot seem to get it to work properly. This
is a 2 part question the first part is of the most important however.

Any help would be greatly appreciated it.

Part 1:

I have a table setup with catagories and subcats like so:

id main_id cat_name
1 0 Main
2 1 Catagory 1
3 1 Catagory 2
4 1 Catagory 3
5 2 Cat 1 Sub-Cat 1
6 2 Cat 1 Sub Cat-2
7 5 Cat 1 Sub-Cat 1 Sub-Cat

and so on up to three total sub-cats, could be as little as 1
sub-cat.

Now what I am trying to do in this part is if there are no other
sub-cats I would like to go to query a different table and give
its output. This is for a classified ads system. If we get to the last
sub-cat I will query the ads table and show the ads list. Here is
what I have for displaying the sub-cats for each catagory:

php Code:
Original - php Code
  1.  
  2. <?php
  3.                                        
  4. // we need to print the proper catagories now
  5. $alternate = "1";
  6. $query = "select * from $tblcats where main_id='$catid';";
  7. $results = db_query($db, $dbhost, $dbuser, $dbpass, $query);
  8. while($row = mysql_fetch_array($results)){
  9. // lets print the sub category for this one
  10.     $mainid = $row['id'];
  11.     if ($alternate == "1") {
  12.         echo "<tr>";
  13.     }
  14.     if($numdays){
  15.         echo "<td valign="top"><img src="$catimg/square.gif" border="0" align="absmiddle">&nbsp;&nbsp;<a href="viewsubcats.php?catid=$row[id]">$row[cat_name]</a><br>";
  16.     }else{
  17.         echo "<td valign="top"><img src="$catimg/square.gif" border="0" align="absmiddle">&nbsp;&nbsp;<a href="viewsubcats.php?catid=$row[id]">$row[cat_name]</a><br>";
  18.     }
  19.     if ($alternate == "1") {
  20.         echo "</td>n";
  21.         $alternate = "2";
  22.     }else {
  23.         echo "</td></tr>n";
  24.         $alternate = "1";
  25.     }
  26.         $count++;
  27. }
  28. ?>


If anyone can see how I can make it where it will stop looking for
catagories and go onto a different query I would greatly appreciate it.

Part 2:

What I am trying to do here is no matter how deep the sub-cats
go I can get them all without haveing to do a different loop for
each. This way there can be unlimited sub-cats like so:

Cat 1
-- Sub Cat 1
---- Sub-Sub Cat 1
------ Sub-Sub-Sub Cat 1

and so on and so on. This would go into a select drop down box. Here is the code I am currently using:

php Code:
Original - php Code
  1.  
  2.             <select name="catid" size="1">
  3.             <option value="0" SELECTED>-- Select Category --</option>
  4.             <?php
  5.     $alternate = "1";
  6.     $catquery = "select * from $tblcats where main_id='1';";
  7.     $catresults = db_query($db, $dbhost, $dbuser, $dbpass, $catquery);
  8.     while($catrow = mysql_fetch_array($catresults)){
  9.     // lets print the sub category for this one
  10.         $catmainid = $catrow['id'];
  11.         echo "<option value="$catrow[id]"><b>$catrow[cat_name]</b></option>n";
  12.             $catsubquery = "select * from $tblcats where main_id='$catmainid' ORDER by cat_name ASC;";
  13.             $catsubresult = db_query($db, $dbhost, $dbuser, $dbpass, $catsubquery);
  14.             while($catsubinfo = mysql_fetch_array($catsubresult)) {
  15.                 $catsubid = $catsubinfo['id'];
  16.                 echo "<option value="$catsubinfo[id]">&nbsp;&nbsp;&nbsp;>>$catsubinfo[cat_name]</option>n";
  17.                 if($catsubid) { 
  18.                     $catsub2query = "select * from $tblcats where main_id='$catsubid' ORDER by cat_name ASC;";
  19.                     $catsub2result = db_query($db, $dbhost, $dbuser, $dbpass, $catsub2query);
  20.                     while($catsub2info = mysql_fetch_array($catsub2result)) {
  21.                         $otcat = $catsub2info['id'];
  22.                         echo "<option value="$catsub2info[id]">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;>>$catsub2info[cat_name]</option>n";
  23.                         if($otcat) {
  24.                             $catsub3query = "select * from $tblcats where main_id='$otcat' ORDER by cat_name ASC;";
  25.                             $catsub3result = db_query($db, $dbhost, $dbuser, $dbpass, $catsub3query);
  26.                             while($catsub3info = mysql_fetch_array($catsub3result)) {
  27.                                 echo "<option value="$catsub3info[id]">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;>>$catsub3info[cat_name]</option>n";
  28.                             }
  29.                         }
  30.                     }
  31.                 }
  32.             }
  33.         }
  34.             ?></select>


Again any help would be greatly appreciated. Thanks in advance.

Regards,
Ray

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Help with catagories and sub-catagories! Confused.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek