Previous Thread
Next Thread
Print Thread
Rate Thread
#272999 05/03/2004 9:29 AM
Joined: Mar 2000
Posts: 528
Junior Member
Junior Member
Offline
Joined: Mar 2000
Posts: 528
Ran into an issue with a script I am writing, and hey, I have never posted on this board...figured I would toss it out here since I am overlooking something.

2 Tables - cats and subcats. The c_autonum = s_cat below...so that ties the two tables together.

CATS fields = c_autonum, c_title
SUBCATS fields = s_autonum, s_cat, s_title, s_desc

I am trying to get information to display in the following format, but having no luck, and have a mental block at the moment.

1. (c_title) General Questions?
- (s_title) What is this?
- (s_title) Why are we here?

2. (c_title) Specific Questions?
- (s_title) Exactly what is this?
- (s_title) Why do we press this button?

Here is my code:

Code
<?php <br /><br />// declare variables and make the database connection <br /><br />MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database"); <br />@mysql_select_db( "$dbName") or die( "Unable to select database"); <br /><br />// query information for faq <br /><br />$result = mysql_query("SELECT * FROM cats,subcats WHERE c_autonum=s_cat"); <br />$i = 1; <br />if ($myrow = mysql_fetch_array($result)) { <br /><br />// display list if there are records to display <br /><br />  do { <br />     printf("%s. <a href=%s>%s</a><br />&nbsp;&nbsp;&nbsp;%s. %s<br />\n", <br />     $myrow["c_autonum"], <br />     $PHP_SELF, <br />     $myrow["c_title"], <br />     $i, <br />     $myrow["s_title"]); <br />$i++; <br />   } while ($myrow = mysql_fetch_array($result)); <br /> } <br /><br />else {echo "Sorry, nothing found!"; <br />     } <br />?> 

This is WORKING...just not correctly. It is showing up with the category above everyone sub-category...ie:

1. (c_title) General Questions?
- (s_title) What is this?
1. (c_title) General Questions?
- (s_title) Why are we here?

Is there a quick fix for this?

Sponsored Links
Joined: May 1999
Posts: 1,715
Addict
Addict
Joined: May 1999
Posts: 1,715
When you select rows from two tables like this:
Code
SELECT * FROM cats,subcats WHERE c_autonum=s_cat


All information from the rows in both tables will be joined together, so for each row returned you will get the following information:
Code
c_autonum, c_title, s_autonum, s_cat, s_title, s_desc


This is why you get the category title for each question, since the query you have written actually returns the category for each row.

If you want to keep the current query, you could change the code to only print the category title if it has changed from the last row.

Code
<br />  $lastcat = ""; // Set it to empty the before the loop, to make the category print the first time.<br />  do { <br />     // First, print the category, but only if it has changed from the last row.<br />     if ( $lastcat != $myrow["c_title"] ) {<br />        printf( "%s. <a href=%s>%s</a><br />\n", <br />        $myrow["c_autonum"], <br />        $PHP_SELF, <br />        $myrow["c_title"]);<br />        // Set last category to the one we printed, so we won't print it again<br />        $lastcat = $myrow["c_title"];<br />     }<br /><br />     // Now we can print the questions in this category<br />     printf("&nbsp;&nbsp;&nbsp;%s. %s<br />\n", <br />     $i, <br />     $myrow["s_title"]); <br />$i++; <br />   } while ($myrow = mysql_fetch_array($result)); <br />


I think that should be it. You will also have to do an ORDER BY s_cat to make sure you get all questions in the same category next to each other.

c0bra #273001 05/07/2004 10:50 AM
Joined: Mar 2000
Posts: 528
Junior Member
Junior Member
Offline
Joined: Mar 2000
Posts: 528
Awesome, thanks Gardener...never crossed my mind to IF statement the query right there.


Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
isaac
isaac
California
Posts: 1,157
Joined: July 2001
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20221218)