Jump to content

Archived

This topic is now archived and is closed to further replies.

lpxxfaintxx

Counting Errors :(

Recommended Posts

Hi,

As a lot of you may know, I am working on a tutorial CMS. When I list all the tutorial categories, I try to add how many tuts are in it. I used the following:

[code]    <div id="menu">
      <div class="sideheader">Tutoial Categorgies</div>
      <ul>
<?php

$sql = "SELECT * FROM `category` order by category";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
$cat = $rows["category"];
$catid = $rows["cat_id"];

$sqlc = "SELECT * FROM `tutorials` WHERE `catid` = '$catid'";
$queryc = mysql_query($sqlc);
while($rowc = mysql_fetch_array($queryc))
$num_rowsc = mysql_num_rows($queryc);


echo <<<__HTML_END

<li><a href="viewcat.php?act=maincat&id=$catid">$cat ($num_rowsc)</a></li>

__HTML_END;

}
[/code]

The categories list perfectly, except for one thing. The counting is messed up... it shows 1 more than there really is, and sometimes it shows 4 when there's none in the category!

For example, at [a href=\"http://informedia.ws/projects/tuts/viewcat.php?act=maincat&id=1\" target=\"_blank\"]http://informedia.ws/projects/tuts/viewcat...ct=maincat&id=1[/a] there are clearly 3 tutorials, but if you look to the left, it says there are "4"

Now try to click on "ASP Coding." There is no tutorials in that category, as you can see... but it shows on the left that it has 4.

Something went totally wrong, can anyone help me out here?

Regards

Share this post


Link to post
Share on other sites
[!--quoteo(post=385084:date=Jun 17 2006, 03:32 PM:name=klaroen)--][div class=\'quotetop\']QUOTE(klaroen @ Jun 17 2006, 03:32 PM) [snapback]385084[/snapback][/div][div class=\'quotemain\'][!--quotec--]
you haven't got a '{' thing after your second while...
[/quote]

1. If that was an issue then he would have received a parse error which he clearly hasn't
2. Curly braces are not needed for one line of code (although I tend to use them anyway)

I doubt this is your problem but I don't think your second while loop is necessary as mysql_num_rows() counts all the rows returned by the query. You could also try using the COUNT(*) function in SQL but that would require an extra query.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.