Jump to content

Looping problem (dropping a record every 6th row)


shamwowy

Recommended Posts

Hi all, I'm trying to print a list of records out of my database in a table. Each row contains 5 cells and is then supposed to loop into the next row of 5 cells until there are no more rows. The table is building almost correctly, with the slight problem of every 6th record is not showing up. Here's the code:

 

<tr>
$query1 ="	SELECT *
	FROM jitem JOIN jloc
	ON jloc.jloc_id IN(jitem.jitem_location)
	WHERE jitem.jitem_display = 1
	ORDER BY jitem_listorder";

	if( $r = mysql_query($query1) )
	{
	$ii = 1;
		while( $row = mysql_fetch_assoc($r) )
		{
			if ($ii <= 5)
			{
				print "<td width=20% valign=top align=center>";
				print "<a href='slides/{$row['jitem_slide']}' title='{$row['jitem_title']}' border=0 onclick=\"window.open(this.href, '','height=545,width=500');return false;\">";
				print "<img border=0 src='thumbs/{$row['jitem_thumb']}' alt='{$row['jitem_title']}'/><br/>";
				print "<font size=2 color=white><strong>{$row['jitem_title']}</strong></a><br/>{$row['jitem_desc']}<br/>";
				print "\${$row['jitem_price']}</font>";
				print "<br/><font size=2><a href=cart.php?itemid=".$row['jitem_id']."&action=add>Add To Cart</a></font>";
				print "</td>";
				$ii ++;
			}

			else
			{
				print "</tr><tr><td colspan=5> </td></tr><tr>";
				$ii = 1;
			}
		}
		}
	else
	{
		print "Database returned no records. query1 is ".$query1."";
	}

	mysql_close();

 

Something is wrong with my counter but I'm not sure what. My output looks like this:

Item1 Item2 Item3 Item4 Item5

Item7 Item8 Item9 Item10 Item11

Item13 Item14

 

Any thoughts on my counter? Is <= 5 my problem? Or am I resetting $ii in the wrong spot? I've tried a few variations but it just makes things really bad. Any help greatly appreciated.

Link to comment
Share on other sites

<?php
$query1 ="    SELECT *
        FROM jitem JOIN jloc
        ON jloc.jloc_id IN(jitem.jitem_location)
        WHERE jitem.jitem_display = 1
        ORDER BY jitem_listorder";

        if( $r = mysql_query($query1) )
        {
        $ii = 1;
            while( $row = mysql_fetch_assoc($r) )
            {
                if ($ii <= 5)
                {
                    print "<td width=20% valign=top align=center>";
                    print "<a href='slides/{$row['jitem_slide']}' title='{$row['jitem_title']}' border=0 onclick=\"window.open(this.href, '','height=545,width=500');return false;\">";
                    print "<img border=0 src='thumbs/{$row['jitem_thumb']}' alt='{$row['jitem_title']}'/><br/>";
                    print "<font size=2 color=white><strong>{$row['jitem_title']}</strong></a><br/>{$row['jitem_desc']}<br/>";
                    print "\${$row['jitem_price']}</font>";
                    print "<br/><font size=2><a href=cart.php?itemid=".$row['jitem_id']."&action=add>Add To Cart</a></font>";
                    print "</td>";
                    $ii ++;
                    if($ii==5){
                        print "</tr><tr><td colspan=5> </td></tr><tr>";
                        $ii = 1;
                            }
                }

                
            }
            }
        else
        {
            print "Database returned no records. query1 is ".$query1."";
        }

        mysql_close();
?>

Link to comment
Share on other sites

<tr>
$query1 ="	SELECT *
	FROM jitem JOIN jloc
	ON jloc.jloc_id IN(jitem.jitem_location)
	WHERE jitem.jitem_display = 1
	ORDER BY jitem_listorder";

	if( $r = mysql_query($query1) )
	{
	$ii = 0; //before is 0 echoed
		while( $row = mysql_fetch_assoc($r) )
		{
			if ($ii == 0) echo '<tr>'; //start table row 
			print "<td width=20% valign=top align=center>";
			print "<a href='slides/{$row['jitem_slide']}' title='{$row['jitem_title']}' border=0 onclick=\"window.open(this.href, '','height=545,width=500');return false;\">";
			print "<img border=0 src='thumbs/{$row['jitem_thumb']}' alt='{$row['jitem_title']}'/><br/>";
			print "<font size=2 color=white><strong>{$row['jitem_title']}</strong></a><br/>{$row['jitem_desc']}<br/>";
			print "\${$row['jitem_price']}</font>";
			print "<br/><font size=2><a href=cart.php?itemid=".$row['jitem_id']."&action=add>Add To Cart</a></font>";
			print "</td>";
			$ii ++;
			if($ii == 5) //end row

			{
				print "</tr><tr><td colspan=5> </td></tr>";
				$ii = 0;
			}
		}
		else
	{
		print "Database returned no records. query1 is ".$query1."";
	}

	mysql_close();

Link to comment
Share on other sites

Not to knock the others' solutions, I'd prefer using a modulus operator in this situation. Also, I see there was no handling to close the last row if the results weren't an exact multiple of 5. I don't like having mis-formatted HTML.

 

Here's my solution. you can easily change how many columns are used by changing the value of $cols_per_row. Also, the last row will always be properly closed even if there are only 1-4 record on the last row

$query1 ="SELECT *
          FROM jitem JOIN jloc
            ON jloc.jloc_id IN(jitem.jitem_location)
          WHERE jitem.jitem_display = 1
          ORDER BY jitem_listorder";

if( $r = mysql_query($query1) )
{
    $cols_per_row = 5;
    $column = 0;
    while( $row = mysql_fetch_assoc($r) )
    {
        $column++;
        if ($column % $cols_per_row == 1)
        {
            echo "<tr>\n"; //start table row 
        }
        print "<td width=20% valign=top align=center>";
        print "<a href='slides/{$row['jitem_slide']}' title='{$row['jitem_title']}' border=0 onclick=\"window.open(this.href, '','height=545,width=500');return false;\">";
        print "<img border=0 src='thumbs/{$row['jitem_thumb']}' alt='{$row['jitem_title']}'/><br/>";
        print "<font size=2 color=white><strong>{$row['jitem_title']}</strong></a><br/>{$row['jitem_desc']}<br/>";
        print "\${$row['jitem_price']}</font>";
        print "<br/><font size=2><a href=cart.php?itemid={$row['jitem_id']}&action=add>Add To Cart</a></font>";
        print "</td>\n";
        if ($column % $cols_per_row == 0)
        {
            echo "</tr>\n"; //end table row 
        }
    }
    //Close last row if not already closed
    if ($column % $cols_per_row != 0)
    {
        echo "</tr>\n"; //end last table row 
    }
}
else
{
    print "Database returned no records. query1 is {$query1}";
}

mysql_close();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.