Jump to content


Photo

Need help displaying things correctly.


  • Please log in to reply
8 replies to this topic

#1 catlover

catlover
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 29 March 2006 - 05:20 PM

Hello,

I'm new to PHP and have a little problem with a script. I hope someone will be kind enough to point me in the right direction to fix what I've scripted. If so, thank you.

I need the script to display breeds of cats and how many articles (or links to articles) have been submitted about that breed into my database.

For example:

Bengal (1)
Burmese (3)
Persian (2)
Siamese (2)



What the script is currently returning is :

Bengal (8)
Burmese (8)
Burmese (8)
Burmese (8)
Persian (8)
Persian (8)
Siamese (8)
Siamese (8)


I need the script to return only one instance of each breed and only the number of articles for that breed.

Here is the script:

<table class="text1" width="100%" border="0" cellpadding="2" cellspacing="0"> <?

$result=mysql_query("SELECT breed FROM animals WHERE animals.breed = animals.breed ORDER BY breed")or die( "Error! Could not select the database: " . mysql_error() );
$num_rows = mysql_num_rows($result);
while ($category = mysql_fetch_array($result))  { ?>

<tr>
<td width="11"><img src="images/down_arrow.jpg" width="10" height="10"></td>
<td width="100%"><a href="<?=$path;?><?=$category['breed'];?>.php">
<? echo ($category['breed']);?></a>
<a href="<?=$path;?><?=$category['breed'];?>.php">
(<? echo "$num_rows";?>)
<?}?>

Thank you for your time!



#2 firedrop84

firedrop84
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 29 March 2006 - 05:24 PM

I think you should use GROUP BY in your SQL query

#3 catlover

catlover
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 29 March 2006 - 05:54 PM

GROUP BY returns:

Bengal (4)
Burmese (4)
Persian (4)
Siamese (4)


That fixed the duplicate breed names. Thank you.

Any idea about how to sort the results of the articles instead of it counting the breeds?

#4 catlover

catlover
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 30 March 2006 - 12:44 AM

I'm not familiar with how Invision boards work, so I didn't know the reply was "meshing" with the last reply.

Does anyone know how to sort how many "articles" are in a particular "breed" based on the previous code change (thanks to Firedrop for the first problem!)?

All the script needs now is the sorting of "articles" pertaining to the particular "breed".

I've fooled with quite a few queries, arrays, etc, but I don't "get it".



#5 sambib

sambib
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 30 March 2006 - 05:10 AM

[!--quoteo(post=359861:date=Mar 30 2006, 10:44 AM:name=catlover)--][div class=\'quotetop\']QUOTE(catlover @ Mar 30 2006, 10:44 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I'm not familiar with how Invision boards work, so I didn't know the reply was "meshing" with the last reply.

Does anyone know how to sort how many "articles" are in a particular "breed" based on the previous code change (thanks to Firedrop for the first problem!)?

All the script needs now is the sorting of "articles" pertaining to the particular "breed".

I've fooled with quite a few queries, arrays, etc, but I don't "get it".
[/quote]

only guessing but--->

$breed = animals['breed'];
$num = count(animals['breed']);

echo '<p>$breed ($num)</p>';

#6 firedrop84

firedrop84
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 30 March 2006 - 09:25 AM

I think u should use COUNT function in your SQL. for exmaple SELECT COUNT(breed) FROM ...

check out this link for more details of COUNT function

[a href=\"http://www.techonthenet.com/sql/count.php\" target=\"_blank\"]http://www.techonthenet.com/sql/count.php[/a]


Regards,
Ahmed

#7 catlover

catlover
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 30 March 2006 - 02:56 PM

Thanks every one for the help!

I've added a second query using COUNT. I used "mysql_fetch_array" again.

COUNT seems to work, but it's only showing the count for the last "breed".

Bengal
Burmese
Persian
Siamese (2)


Any idea on how to make it return the COUNT for all "breeds"?

#8 LordStanley

LordStanley
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 30 March 2006 - 03:24 PM

$result=mysql_query("SELECT breed FROM animals WHERE animals.breed = animals.breed ORDER BY breed")or die( "Error! Could not select the database: " . mysql_error() );

that query doesnt make sense

where animals.breed=animals.breed will ALWAYS Be true. and you dont even need the animals. because your only selecting from one database

heres a logical way of doing what you need

<table class="text1" width="100%" border="0" cellpadding="2" cellspacing="0"> <?

$result=mysql_query("SELECT breed, count(breed) as num FROM animals ORDER BY breed")or die( "Error! Could not select the database: " . mysql_error() );
while ($category = mysql_fetch_array($result))  {

echo '<tr>
<td width="11"><img src="images/down_arrow.jpg" width="10" height="10"></td>
<td width="100%"><a href="'.$path.$category['breed'].'.php">'.$category['breed'].'</a>
<a href="'.$path.$category['breed'].'.php">
( '.$category['num'].')';
}

thats not tested, but it should work...

try that
I am available for jobs

My IPB Modifications (8)

#9 catlover

catlover
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 30 March 2006 - 03:31 PM

LordStanley,

Thanks for your help!

I had to change ORDER BY to GROUP BY, but it now works!

Thank you!


[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Error! Could not select the database: Mixing of GROUP columns (MIN(),MAX(),COUNT()...) with no GROUP columns is illegal if there is no GROUP BY clause[/quote]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users