[thelist] php display 3x2

Howard Cheng howcheng at ix.netcom.com
Mon Dec 2 16:58:01 CST 2002


Still working on this, eh? (:

At 06:38 PM 12/2/2002 +0000, Andrew Maynes wrote:
>$record_column == 3;

'==' is an equality test operator. Use a single = to assign a value to a
variable. Also, you should probably be setting this to 0 (zero).

>while($row = mysql_fetch_object($result)){
>// how many columns do we want...3
>    if($record_column <=3){

0 to 3 will get you four columns. This should be < (less-than), not <=.

>//if this is the first column we need to print the table row tag
>       if($record_column == 0){
>          print("<tr>");
>       }
>// print out 3 books for this column with proper tags
>       print("<td>" . $row->image . $row->title . "</td>");
>// increase the column number by one and get the next record
>       $record_column++;
>    } else {
>// if there are 3 columns (like set above) then print the end tag
>       print("</tr>\n");
>// reset the column record to begin new row
>           $record_column = 1;
>    }

Notice that earlier you have it so that when $record_column is zero, you
start a new table row. In this case now, you are never going to have zero
again because every time you iterate through the loop, you reset it to 1.
So you should change this to $record_column = 0.

>}
>print("</table>");
>?>

To limit yourself to 3x2 (i.e., 6 items total), it's probably best to limit
your SQL query to six items. I assume that if you have more than 6 items in
your database you'll want to paginate as well?

So try:

// get the page variable from the query string
$page = $_GET['page'];

// if page variable doesn't exist, you're on page 1,
// otherwise convert to integer.
if (!$page) $page = 1;
else settype($page, "integer");

$sql = "SELECT ... FROM ... ORDER BY ... LIMIT " . ($page - 1 * 3) . ", 6";

This ensures that you will only six items in your result set.


::::::::::::::::::::::::::::::::::
Howard Cheng
http://www.howcheng.com/
howcheng at ix dot netcom dot com
AIM: bennyphoebe
ICQ: 47319315




More information about the thelist mailing list