Problem with counting rows

Discussion in 'PHP' started by SWorld, Feb 2, 2010.

  1. #1
    Hello,

    I am very new to php, and need your help

    I have 2 tables, one for main categories, and the other for sub categories

    I am listing the main categories within 3 columns per each row like this:

    Main Category 1 Main Category 2 Main Category 3
    Main Category 4 Main Category 5 Main Category 6

    Table Name: maincategories
    Fields: id, main



    And at the same time counting sub categories that belong to each main category

    Table Name: subcategories
    Fields: id, main_id, sub

    I keep seeing this error:
    Warning: mysql_result() [function.mysql-result]: Unable to jump to row 27 on MySQL result index 10

    I can see the main category name, but can never see the sub category counts


    this is the code:

       1. <?php
       2.                
       3. include "config.php";
       4.  
       5.                
       6.  
       7. $result = mysql_query("SELECT * FROM maincategories ORDER BY main") or die(mysql_error());
       8.  
       9. while ($row = mysql_fetch_array($result)) {
      10. $myvar = $row[id];
      11. $query = "SELECT main_id, COUNT(sub) FROM subcategories WHERE main_id = '$myvar' GROUP BY main_id";
      12.  
      13.  
      14.  
      15.  
      16.  
      17. $display_columns = 3;
      18. $count = mysql_numrows($result);
      19. $padding = ($display_columns-1)-(($count-1)%$display_columns);
      20.  
      21.  
      22. print "<table border='0' width='100%' cellspacing='0' cellpadding='0'>\n";
      23. for($i=0; $i<$count; $i++){
      24. if($i%$display_columns == 0)
      25.  
      26. print "<tr>\n";
      27. print "<td width=\"33%\">\n";
      28. $result2 = mysql_query($query) or die(mysql_error());
      29. while ($row = mysql_fetch_array($result2)){
      30. echo '<IMG SRC="images/icon.jpg" WIDTH="44" HEIGHT="44" border="0">
      31. <a href="category.php?category=' . mysql_result($result,$i,$main) . '"><strong>
      32. ' . mysql_result($result,$i,"main") . '' . mysql_result($result2,$myvar) . 'Sub Category
      33.  
      34. </strong></a>';
      35.  
      36. }
      37.  
      38. print "<p>&nbsp;</p>\n";
      39. print "</td>\n";
      40. if($i%$display_columns == $display_columns-1)
      41.  
      42. print "</tr>\n";
      43. }
      44.  
      45. if($padding!=0){
      46. for($i=0;$i<$padding;$i++)
      47. print "<td width=\"33%\"></td>\n";
      48. print "</tr>\n";
      49. }
      50.  
      51. print "</table>\n";
      52.  
      53.  
      54. }
      55.                
      56. mysql_close($link);
      57.                
      58. ?>
    PHP:

    Thank you in advance
     
    SWorld, Feb 2, 2010 IP
  2. RJP

    RJP Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This is only from a quick look, so forgive me if I'm mistaken, but I think the problem is in your loop setup.

    In line 9 you are staring a WHILE loop for the main categories. So that goes around once per category.

    Then in line 23 you start a FOR loop that is based on the number of main categories. That's inside the WHILE loop, so for each of the 6 categories it will loop 6 times.

    As such, your $i count variable is getting to row numbers that don't exist, and when calling the mysql_result() function on non-existant rows it produces the error.

    ... I think.

    -Ryan
     
    RJP, Feb 2, 2010 IP
  3. SWorld

    SWorld Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you RJP, I think what you said makes sense
    but I need the FOR loop to list the main categories in 3 columns method
    do you have any idea about how to keep the listing method and counting the sub categories at the same time?
     
    SWorld, Feb 2, 2010 IP