Forums

PHP

This topic is locked

Cat/Subact (again) once and for all

Posted 06 Jun 2002 11:11:29
1
has voted
06 Jun 2002 11:11:29 Julio Taylor posted:
Hello,

I know that this thread has gone around the forums over the past numerous times, but it never seems to end in a clear concise result. I'll try again.

The problem is simple: A number of categories (in this example, 'table nav_cat') and subcategories (nav_subcat). I want to display them like this:

Cat 1
Subcat 1.1
Subcat 1.2
Subcat 1.3
Cat 2
Subcat 2.1
Subcat 2.2

And so forth....

I've been trying to work out some SQL code to do this, but obviously SQL has no capability (as far as i know) to create an output in such dimensions. Now.... i've seen as ASP tutorial on how to do this; unfortunetly i am ASP illiterate and i need some help on how i can do this using PHP.

The SQL code i've used so far is this:

SELECT cat_txt, sub_txt
FROM nav_cat, nav_subcat
WHERE nav_subcat.cat_id = nav_cat.cat_id

this returns mixed results, something along the lines of what i'm looking for but of course in a totally un-structured format.

Can someone help me out and at least help me understand the logical aspects of this, as i am totally lost from a code point of view and a logical aspect.

Thanks a million in advance!<font size=2></font id=size2><font size=1></font id=size1>

i hope this helps.

--J

P.S. i killed kenny

Replies

Replied 07 Jun 2002 10:56:02
07 Jun 2002 10:56:02 Aurel Sorin Cirstoiu replied:
I suggest to use a single table with this structure:

cat_id int(4)
cat_parentid int(4)
cat_name varchar(50)

cat_id is the primary key.
Primary categories will have cat_parentid NULL.

So now you will do this:

rsCat=Execute("SELECT * FROM table_name WHERE cat_parentid is NULL"
while (! rsCat.EOF ) {
echo rsCat("cat_name"
rsSubCat=Execute("SELECT * FROM table_name WHERE cat_parentid = '" . rsCat("cat_id" . "'"
while (!rsSubCat.EOF) {
echo " -" . rsSubCat("cat_name"
rsSubCat.MoveNext()
}
rsCat.moveNext()
}

What I wrote is not php code; is a kind of pseudocode. I hope you will understand it. It's very easy to implement it into php.
Please let me know if that helped you.

-----------------------
Cirstoiu Aurel Sorin
InterAKT Support
www.interakt.ro

Edited by - csorin on 07 Jun 2002 10:57:11
Replied 07 Jun 2002 10:59:36
07 Jun 2002 10:59:36 Julio Taylor replied:
Csorin,

Thanks a lot, that does help me a lot. Unfortunetly i don't really know how to implement it into PHP (i'm not that great at hand-coding PHP yet). But the table structure you've showed me is much better than what i had been doing so far...

Thanks for your help.
Replied 07 Jun 2002 11:42:50
07 Jun 2002 11:42:50 Aurel Sorin Cirstoiu replied:
I could implement it for you. Please tell me how to do it: using native functions or PHP-ADODB library???
What database are you using?


-----------------------
Cirstoiu Aurel Sorin
InterAKT Support
www.interakt.ro
Replied 07 Jun 2002 11:45:10
07 Jun 2002 11:45:10 Julio Taylor replied:
Cirstoiou,

I'm using the ADODB functions that come with Phakt, and i am using MySQL as the database.

Thanks.
Replied 07 Jun 2002 12:31:33
07 Jun 2002 12:31:33 Aurel Sorin Cirstoiu replied:
Please send me a mail with the PHAkt version and I will send you the entire code and table script.

-----------------------
Cirstoiu Aurel Sorin
InterAKT Support
www.interakt.ro
Replied 07 Jun 2002 13:12:58
07 Jun 2002 13:12:58 Aurel Sorin Cirstoiu replied:
Here is the code:

&lt;?php require_once('adodb/adodb.inc.php'); ?&gt;
&lt;?php require_once('Connections/cat.php'); ?&gt;
&lt;?php
$rsCat = $cat-&gt;Execute("SELECT * FROM categories WHERE cat_parentid IS NULL";
$i = 0;
while(!$rsCat-&gt;EOF) {
$i++;
echo $i . ". " . $rsCat-&gt;Fields("cat_name". "&lt;br&gt;";
$rsSubCat = $cat-&gt;Execute("SELECT * FROM categories WHERE cat_parentid='".$rsCat-&gt;Fields("cat_id"."'";
$j = 0;
while(!$rsSubCat-&gt;EOF) {
$j++;
echo "&nbsp&nbsp&nbsp" . $i . "." . $j . ". " . $rsSubCat-&gt;Fields("cat_name" . "&lt;br&gt;";
$rsSubCat-&gt;MoveNext();
}
$rsCat-&gt;MoveNext();
}
?&gt;

-----------------------
Cirstoiu Aurel Sorin
InterAKT Support
www.interakt.ro
Replied 10 Jun 2002 09:19:51
10 Jun 2002 09:19:51 Julio Taylor replied:
Csorin,

Thanks for you help, finally i can see how this is done... <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

I'll try a zip containing all the files soon and i'll post the address here for everyone to see.

-- Julio

Reply to this topic