Breaking a cycle .. help needed !

Discussion in 'PHP' started by Shimurai, Nov 9, 2009.

  1. #1
    Hello,

    I don't know if the title is correct for what I need so if its not the correct title sorry, i'm a beginner.

    I'm trying to make a shopping cart and I'm extracting some info from a mysql datadabe, and here I have this code:

    
    <?php
    $sql = 'SELECT * FROM sets ORDER BY id';
    $result = $db->query($sql);
    
    
    $output[] = '<table width="100%" cellpadding="10" cellspacing="0" align="center">
    
    <tr width="644" align="center">';
    
    while ($row = $result->fetch()) {
    	$output[] = '
    <td><table class="item"> 
    <tr><td class="itemname">'.$row['name'].'</td></tr> 
    <tr><td class="imgtable"> <img class="testimage" width="161" height="204" src="'.$row['image'].'"> </td></tr> 
    <tr><td class="itemprice">$'.$row['price'].' USD</td></tr>
    <tr><td> <a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a> </td></tr>
    </table></td>
    ';
    }
    $output[] = '</tr>
    
    </table>';
    
    echo join('',$output);
    ?>
    
    PHP:
    that code is showing me this ( with the info i have in the database ):
    http://geupload.com/images/image1.png

    But what I want is, after 4 items that have been listed the cycle should break and start a new one to display the next items ( making a new table to display another 4 items ) until there are no more items.

    as you see in the image there are 5 items, well instead of that I want only 4 items and then display another 4 items BELOW.

    something like this:
    http://geupload.com/images/image2.png


    what do I need to add to the code above to do this?



    Thanks !
     
    Shimurai, Nov 9, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    following should help..


    
    <?php
    $sql = 'SELECT * FROM sets ORDER BY id';
    $result = $db->query($sql);
    
    
    $output[] = '<table width="100%" cellpadding="10" cellspacing="0" align="center">
    
    <tr width="644" align="center">';
    
    $column_ctr = 0;
    while ($row = $result->fetch())
    {
    	$column_ctr++;
    	if($column_ctr % 4 == 0)
    	{
    		$output[] = '</tr>';
    		$output[] = '<tr width="644" align="center">';
    	}
    	
    	$output[] = '
    	<td><table class="item"> 
    	<tr><td class="itemname">'.$row['name'].'</td></tr> 
    	<tr><td class="imgtable"> <img class="testimage" width="161" height="204" src="'.$row['image'].'"> </td></tr> 
    	<tr><td class="itemprice">$'.$row['price'].' USD</td></tr>
    	<tr><td> <a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a> </td></tr>
    	</table></td>
    	';
    }
    $output[] = '</tr>
    
    </table>';
    
    echo join('',$output);
    ?>
    
    PHP:
     
    mastermunj, Nov 9, 2009 IP
  3. Shimurai

    Shimurai Well-Known Member

    Messages:
    186
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Omg ! It worked..

    thank you very much ! i cant believe it ha! .. thanks !!!!
     
    Shimurai, Nov 9, 2009 IP