Current location: Hot Scripts Forums » Programming Languages » PHP » MySQL/PHP query Select question


MySQL/PHP query Select question

Reply
  #1 (permalink)  
Old 10-15-03, 02:11 PM
PoliticalGateway PoliticalGateway is offline
Newbie Coder
 
Join Date: Jul 2003
Location: florida
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL/PHP query Select question

Hello,

I have a probelm with a little php script I am trying to make...I have a table with "people" and each person has a "category". I simply want to make a list of categories, with each person who is in that category listed under it..

The code below does it "almost right"...it lists teh people under each category, but everytime it comes across a row with the same category it has already listed, it makes another list of people for the same category...(example output is listed below code....


Code:
<? //build and issue query
$sql ="SELECT category FROM authors WHERE approve ='1'";
$result =@mysql_query($sql,$connection)or die(mysql_error());


while ($row =mysql_fetch_array($result)){
		$category = $row['category'];
		print "<div align=\"center\"><font color=\"#FFFF00\"><strong>".$category."</strong></font></div>";
			$sqlx ="SELECT id_num,name,anony,othername FROM authors WHERE category='$category' and approve ='1'";
	$resultx =@mysql_query($sqlx,$connection)or die(mysql_error());
	while ($rowx =mysql_fetch_array($resultx)){
	$ids= $rowx['id_num'];
	$name = $rowx['name'];
	$othername = $rowx['othername'];
	$anony = $rowx['anony'];
	if ($anony == '1') {
	print "<a href=\"/main/columns/surf/readarticle.html?id_num=".$ids."&name=".$othername."\" class=\"pgnav\">".$othername."</a><br>"; } else {
	print "<a href=\"/main/columns/surf/readarticle.html?id_num=".$ids."&name=".$name."\" class=\"pgnav\">".$name."</a><br>";
	} 
	}print "<br>";
} ?>
Columnists

conservative <-- category
Joe Write
Herman the author

Liberal <-- category
Fred baylor
alice cooper

middle of the road<-- category
joe hanger

conservative <-- category (this is a repeated listing!!)
Joe Write
Herman the author

middle of the road
freddy

Green People<-- category
greg greener
mr green jeans


I have spent two days looking on mysql.com and a million tutorials, but since I do not even know the correct syntax for the needed code, it is hard to look it up......

Any help is appreciated...
__________________
www.politicalgateway.com
Reply With Quote
  #2 (permalink)  
Old 10-16-03, 01:25 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
I am not sure why you are using 2 while loops in your code , one is enough ..
but if you wouldn't mind I can write you a new one from scratch that does
exactly what you want but with out this while loop because I hate it
I will write another one using for loop .. is it ok for you ?
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 10-16-03 at 01:29 AM.
Reply With Quote
  #3 (permalink)  
Old 10-16-03, 02:27 AM
PoliticalGateway PoliticalGateway is offline
Newbie Coder
 
Join Date: Jul 2003
Location: florida
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Got IT

Hey, thanks for the offer...

I figured it out, I simply added a "distinct" before the "category" in the first loop and it worked like a charm....

Can't believe I did it...

First application is always the best I guess, it should be rather boring from here out...


bob
__________________
www.politicalgateway.com
Reply With Quote
  #4 (permalink)  
Old 10-16-03, 06:30 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
glad to hear that
self-learning is the best way for learning
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MOHAA Server Query - ASP Stealth Website Reviews 1 11-27-04 10:38 AM
Make a query that makes a field a link to the entire row mdhall PHP 8 10-09-03 06:18 PM
Query help tgarske PHP 0 09-10-03 01:12 PM
Insert...select andreasberglind PHP 5 08-21-03 05:07 PM
creating calculated fields in mysql resource after query Abu Mami PHP 0 07-31-03 06:47 AM


All times are GMT -5. The time now is 07:10 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.