Jump to content

[SOLVED] Need some help with a while loop! thanks!


zhTonic

Recommended Posts

Hey there,

 

I'm not completely done with my code here but what i want to do is have it loop 5 results and then start a brand new row after 5 and then continue on with the loop. As an example if i have 12 rows in my table i would want the output to look something like this.

 

1 2 3 4 5

6 7 8 9 10

11 12

 

I'm new to using while loops for this purpose so i need some help thanks :) function is below.(ignore the LIMIT 5 i was just looking at how 5 results looked)

 

function productsforsale($font){
$sql = mysql_query("SELECT * FROM products LIMIT 5");


echo "  <TR>
    <TD class=maintext bgColor=#c2b868>
      <TABLE cellSpacing=0 cellPadding=0 width=740 border=0>
        <TBODY>
        <TR>
          <TD colSpan=6 height=10></TD></TR>
        <TR>
          <TD> </TD>
          <TD colSpan=5>
            <DIV align=center>
            <P><STRONG>$font Click on an image to link to each section below. 
            </STRONG></P></DIV></TD></TR> ";
       
       echo "<TR>
          <TD></TD>
          <TD colSpan=5 height=15></TD></TR>
        <TR>
          <TD width=20> </TD>";
             
while($sqlly = mysql_fetch_array($sql)){
$title = $sqlly["title"];
$image = $sqlly["image"];      
$prod = $sqlly["prod"];
        
        echo "<TD vAlign=top align=middle width=144><A 
            href='?action=productsforsale&prod=$prod'><IMG 
            height=75 src='images/$image' 
            width=100 border=0><BR>$font $title</A></TD>";
}
        echo "</TR><TR>
          <TD> </TD>
          <TD align=middle colSpan=5 height=15></TD></TR>";

}

 

what you want is a while loop and a nested if loop

 

 

$i = 0 //counter

while($sqlly = mysql_fetch_array($sql)){

if($i=<5){

insert html to create a new row

$i=0

}else{

$i++;

}

insert html to create a new cell in your row

}

 

 

btw HTML  should not be written in CAPS

<TR><TD></TD></TR> is wrong

<tr><td></td></tr> is correct

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.