php and mysql logic problem, please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robin1
Forum Newbie
Posts: 20
Joined: Thu Aug 01, 2002 4:36 pm

php and mysql logic problem, please help

Post by robin1 »

hello everybody,
what am i trying to do: basically what i'm doing is pulling information from 2 databases and display them in 3 columns. this works but for somereason its not displaying the last couple of records.. this is what i have sofar http://www.comgen.ca/phptest/classified.php
there are 16 records in total but its only displaying 15
please help

Code: Select all

$query1 = "SELECT categorydb.dbcategory AS CATEGORY, " . 
          "COUNT(classifieddb.dbcategory) AS ENTRIES " . 
          "FROM categorydb LEFT JOIN classifieddb " . 
          "ON categorydb.dbcategory =classifieddb.dbcategory " . 
          "GROUP BY CATEGORY";
$results = mysql_query($query1) or die("Query unsuccessful");
$num_of_category = mysql_num_rows($results);

echo $num_of_category;

while ($row = mysql_fetch_array($results)) 
{ if($category_c1 =="" || $category_c2 == "" || $category_c3 == "")
{ if($category_c1 == "") 
   { $category_c1 = $rowї"CATEGORY"];
     $entries_c1 = $rowї"ENTRIES"];
   }
   elseif($category_c2 == "")
    { $category_c2 = $rowї"CATEGORY"];
      $entries_c2 = $rowї"ENTRIES"];
    }
     else
   { $category_c3 = $rowї"CATEGORY"];
     $entries_c3 = $rowї"ENTRIES"];
     echo "<span class='class1'>";
     echo "<TR>";
    //first col in a table format
     echo "<TD align='right' width='3%'> <img           src='bullet.gif'></TD>";
      echo "<TD ALIGN ='LEFT' WIDTH='%20'> <font size=3><strong>";
      echo $category_c1."(".$entries_c1.")</strong> </font></td>";
     // sec. col. in a table format
      echo "<TD align='right' width='3%'><img src='bullet.gif'></TD>";
      echo "<TD ALIGN ='LEFT' WIDTH='%20'><font size=3><strong>";
		      echo $category_c2."(".$entries_c2.")</strong> </font></td>";
      //third col. in table format
      echo "<TD align='right' width='3%'><img src='bullet.gif'></TD>";
      echo"<TD ALIGN ='LEFT' WIDTH='%20'><font size=3><strong>";
		      echo $category_c3."(".$entries_c3.")</strong> </font></td>";
		      echo "</TR>";
		      $category_c1 = ""; $entries_c1 = "";
		      $category_c2 = ""; $entries_c2 = "";
		      $category_c3 = ""; $entries_c3 = "";
		   }  
	    }//if statment checking to see if the var. are empty or for input

}
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post by f1nutter »

It looks like you are getting results in blocks of 3, so your site is displaying 5 rows of 3, leaving you one record short. This is because you are only printing if c1 and c2 are non-empty. Your 17th record, which will be c2 is empty, so the elseif($category_c2 == "") branch is taken, and not the printing block.

You need to change your "table logic" to print this correctly. The final row of the table (for 16 records) should be

Code: Select all

<td>ENTRY</td><td>empty</td><td>empty</td>
to match up with a 3 column table.

Maybe you could keep a column counter, which keeps track of which column you are writing.

Code: Select all

$num_columns = 3; // allows you to be flexable with your table design
$column_counter = 0; // initialise counter

while ($row = mysql_fetch_array($results))  
{
 // get column number (0, 1, 2 in your case)
 $column_counter %= $num_coulmns;

 // if new table row, print HTML tag <tr>
 if ($coulmn_counter == 0)
 {
  print("<tr>");
 }

 // check other columns, and add other formatting
 if ($rowї"CATEROGORY"] != "")
 {
  print("<td>" .$rowї"CATEROGORY"]. "</td>");
 }
 else // print empty table cell
 {
  print("<td></td>");
 }

 // if last table cell, add HTML closing row tag, </tr>
 if ($column_counter == $num_columns - 1)
 {
  print("</tr>");
 }

 // move "across" column for next query row
 $column_counter++;
}
P.S. You also need to change your colour, black writing on a dark blue background?
Post Reply