Jump to content

[SOLVED] Limiting result display by 3 across?


wmguk

Recommended Posts

hey guys,

 

I am trying to pull out the result for a database, within a table, and so far I have managed to get them all coming out, however I only want 3 results per row.

 

basically i want a table with 3 columns, and as many rows as required, ie, 9 results then 3 rows with 3 columns etc....

 

what i currently have

 

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="leftmenu">
            <tr>
<?
$result = mysql_query("SELECT * FROM products WHERE colour = '$colour'");
while($row = mysql_fetch_array($result)) 
{ 
$name = $row['name'];
$style = $row['style'];
$size = $row['size'];
$price = $row['price'];
$clear = $row['clear'];
$special = $row['special'];
$pic = $row['pic'];
?>
<td><table width="135" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td align="left" valign="top"><img src="images/tables/tiles4u_table_lefttop.gif" alt="TILES 4 U" width="8" height="11" /></td>
                  <td><img src="images/tables/tiles4u_table_top.gif" alt="TILES 4 U" width="100%" height="11" /></td>
                  <td align="left" valign="top"><img src="images/tables/tiles4u_table_righttop.gif" alt="TILES 4 U" width="8" height="11" /></td>
                </tr>
                <tr>
                  <td background="images/tables/tiles4u_table_left.gif"> </td>
                  <td width="119" align="center" valign="middle" class="leftmenu">
                  <p align="left">
			  	 <? echo $name ; ?><br />
                    <img src="/images/tiles/<? echo $pic ; ?>" width="150px" /><br />
				£<? echo $price ; ?> per sq/m</p>                      </td>
                  <td background="images/tables/tiles4u_table_right.gif"> </td>  
                </tr>
                <tr>
                  <td align="left" valign="top"><img src="images/tables/tiles4u_table_leftbottom.gif" alt="TILES 4 U" width="8" height="11" /></td>
                  <td><img src="images/tables/tiles4u_table_bottom.gif" alt="TILES 4 U" width="100%" height="11" /></td>
                  <td align="left" valign="top"><img src="images/tables/tiles4u_table_rightbottom.gif" alt="TILES 4 U" width="8" height="11" /></td>
                </tr>
              </table>                </td><? }?> 
            </tr>
          </table>

 

I have put the results in to a <td> so that each result comes out in a new column, however I dont know how to create a new row after the 3rd result?

 

can anyone help?

 

you can see this working at http://tiles4u.wicked-websites.co.uk

 

Thanks guys

Link to comment
Share on other sites

bingo :)

 

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="leftmenu">
            <tr>
<?
$result = mysql_query("SELECT * FROM products WHERE colour = '$colour'");
while($row = mysql_fetch_array($result)) 
{ 
$name = $row['name'];
$style = $row['style'];
$price = $row['price'];
$clear = $row['clear'];
$special = $row['special'];
$pic = $row['pic'];
?>
<td><table width="135" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td align="left" valign="top"><img src="images/tables/tiles4u_table_lefttop.gif" alt="TILES 4 U" width="8" height="11" /></td>
                  <td><img src="images/tables/tiles4u_table_top.gif" alt="TILES 4 U" width="100%" height="11" /></td>
                  <td align="left" valign="top"><img src="images/tables/tiles4u_table_righttop.gif" alt="TILES 4 U" width="8" height="11" /></td>
                </tr>
                <tr>
                  <td background="images/tables/tiles4u_table_left.gif"> </td>
                  <td width="119" align="center" valign="middle" class="leftmenu">
                  <p align="left">
			  	 <? echo $name ; ?><br />
                    <img src="/images/tiles/<? echo $pic ; ?>" width="150px" /><br />
				£<? echo $price ; ?> per sq/m</p>                      </td>
                  <td background="images/tables/tiles4u_table_right.gif"> </td>  
                </tr>
                <tr>
                  <td align="left" valign="top"><img src="images/tables/tiles4u_table_leftbottom.gif" alt="TILES 4 U" width="8" height="11" /></td>
                  <td><img src="images/tables/tiles4u_table_bottom.gif" alt="TILES 4 U" width="100%" height="11" /></td>
                  <td align="left" valign="top"><img src="images/tables/tiles4u_table_rightbottom.gif" alt="TILES 4 U" width="8" height="11" /></td>
                </tr>
              </table>                </td><? }?> 
            </tr>
          </table>

 

basically there is one table with 1 td created. then each result is in a different table for formatting and borders etc....

 

so yes

 

res1 res2 res3

res4 res5 res6

Link to comment
Share on other sites

Been playing and so far I have this:

 

         <table width="100%" border="0" cellpadding="0" cellspacing="0" class="leftmenu">
            <tr>
            <?php

$result = mysql_query("SELECT * FROM products WHERE colour = '$colour'");

$i = 1; //set $i to 1 outside the loop
while ($row=mysql_fetch_assoc($result)
{  //ERROR RELATES TO THIS ERROR
echo '<td><table width="135" border="0" cellspacing="0" cellpadding="0">';
echo '<tr><td align="left" valign="top"><img src="images/tables/tiles4u_table_lefttop.gif" alt="TILES 4 U" width="8" height="11" /></td>';
echo '<td><img src="images/tables/tiles4u_table_top.gif" alt="TILES 4 U" width="100%" height="11" /></td>';
echo '<td align="left" valign="top"><img src="images/tables/tiles4u_table_righttop.gif" alt="TILES 4 U" width="8" height="11" /></td></tr>';
echo '<tr>';
echo '<td background="images/tables/tiles4u_table_left.gif"> </td>';
echo '<td width="119" align="center" valign="middle" class="leftmenu"><p align="left">';
echo $row['name']; 
echo '<br /><img src="/images/tiles/';
echo $row['pic']; 
echo '" width="150px" /><br />£';
echo $row['price'];
echo 'per sq/m</p></td><td background="images/tables/tiles4u_table_right.gif"> </td></tr><tr>';
echo '<td align="left" valign="top"><img src="images/tables/tiles4u_table_leftbottom.gif" alt="TILES 4 U" width="8" height="11" /></td>';
echo '<td><img src="images/tables/tiles4u_table_bottom.gif" alt="TILES 4 U" width="100%" height="11" /></td>';
echo '<td align="left" valign="top"><img src="images/tables/tiles4u_table_rightbottom.gif" alt="TILES 4 U" width="8" height="11" /></td></tr></table></td>';
$i = $i + 1; //add 1 to $i
if ($i == 3) { //when you have echoed 3 <td>'s
echo '</tr><tr>'; //echo a new row
$i = 1; //reset $i
} //close the if
}//close the while loop
echo '</tr></table>' //close out the table
?> 
            </tr>
          </table>

 

however i get unexpected { error on line 5 (where i have // )

 

any ideas?

Link to comment
Share on other sites

while ($row=mysql_fetch_assoc($result)

missed a second ) :P

 

and reguarding to the original problem, this should have your desired effect

<?php
$i=0;
echo '<table>';
while ($row=mysql_fetch_assoc($result)) {
if($i==0) echo '<tr>';

echo '<td>';
// do rest of column
echo '</td>';

if($i==2) {
	echo '</tr>';
	$i=0;
} else {
	$i++
}
}
echo '<table>';
?>

 

 

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.