Page 1 of 1

Displaying data in 3 columns

Posted: Wed Oct 24, 2007 11:00 am
by Wardy7
I have a database with products in and I want ti to automatically show every product on the page. Now I am able to do this easy enough but what I am struggling to do is that I want the products to be displayed in 3 columns. I can get them to display one under the other easy enough but I want them to be in rows with 3 columns instead.

Can anyone please help.

Code: Select all

$limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM products ORDER BY price LIMIT $limitvalue, $limit";          
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){ 
        echo("Products are missing! Ooops!"); 
    } 

    $bgcolor = "#FFFFFF"; //
    $bgcolor2 = "#BFDFFF"; //
    $bgcolor3 = "#FFCC33"; //
    $bgcolor4 = "#FFDF80"; //
    $bgcolor5 = "#FFEEB9"; //
    $font =    "Verdana, Arial, Helvetica, sans-serif"; //
    $fontsize = "2"; //
    $tdwidth = "170"; //
    $tdwidth2 = "120"; //
    $tdwidth3 = "145"; //

    echo('<table width="760" border="0" cellspacing="1" cellpadding="5" align="center" bgcolor="#3333FF"><tr valign="top" bgcolor="#FFFFFF">'); 
      
    while($row = mysql_fetch_array($result)){ 
        if ($bgcolor == "#FFFFFF"){ 
            $bgcolor = "#FFFFFF"; 
        }else{ 
            $bgcolor = "#FFFFFF"; 
     } 

    echo('<td width="240">');
    echo("<font face=".$font.">");
    echo("<font size=".$fontsize.">");
    echo($row["name"]);
    echo("<br>");
    echo('<img src="' . $row["smallimage"] . '">');
    }
    echo("</table>");
?>
Cheers
Wardy

Posted: Wed Oct 24, 2007 11:28 am
by califdon
The code you provided doesn't produce valid HTML, to begin with. It never closes the <td> tags, and there aren't any <tr> tags. I'm guessing that you copied this code from somewhere and are trying to modify it. There's nothing wrong with doing that if you understand what you're doing, but you can't do that sort of thing without knowing at least the basics of PHP and HTML.

That said, the way you achieve 3 columns is by generating 1 row <tr> with 3 cells <td>'s within the while loop, which in turn requires that you fetch 3 rows for each iteration of the loop.

Beyond that, the syntax of your code uses deprecated (outdated) HTML tags.

Posted: Wed Oct 24, 2007 1:08 pm
by feyd
The first two threads linked from Useful Posts should be of interest.

Posted: Thu Oct 25, 2007 8:27 am
by Wardy7
OK, can anyone help with this bit of code then. The original bit I posted was all mucked up due to me tryign to get this to work. :(

Code: Select all

echo("<table>"); 
      
    while($row = mysql_fetch_array($result)){ 
        if ($bgcolor == "#F7DF42"){ 
            $bgcolor = "#FFFFFF"; 
        }else{ 
            $bgcolor = "#F7DF42"; 
     } 
    echo("<tr bgcolor=".$bgcolor.">\n<td>");
    echo('<a href="../products/' . $row["product_code"] . '">' . $row["description1"] . '</a>'); 
    echo('<img src="' . $row["small_image"] . '" alt="' . $row["description1"] . ' ' . $row["description2"] . '"></a>');
    echo("</td>\n"); 
    } 

    echo("</table>");
Cheers
Wardy

Posted: Thu Oct 25, 2007 9:47 am
by feyd
Have you looked at the posts I referred to?

Use a counter variable.

Posted: Thu Oct 25, 2007 11:53 am
by churt
Although the thread feyd linked to has a solution, just wanted to post a second solution if you prefer to stick with the while loop.

Code: Select all

echo("<table>");
  $ct=1; //Initialize counter  
  while($row = mysql_fetch_array($result)){
        if ($bgcolor == "#F7DF42"){
            $bgcolor = "#FFFFFF";
        }else{
            $bgcolor = "#F7DF42";
        }
        if($ct==1) echo "<tr bgcolor=$bgcolor>";//Start row
        echo "\n<td>";
        echo '<a href="../products/' . $row["product_code"] . '">' . $row["description1"] . '</a>';
        echo '<img src="' . $row["small_image"] . '" alt="' . $row["description1"] . ' ' . $row["description2"] . '"></a>';
        echo "</td>\n";
        if($ct==3){ echo "</tr>"; $ct=0; }//End row 3rd time through and reset counter to 0 so that $ct++ will set it to 1 for the new row.  For more columns change $ct==3 to $ct==x.  x being the number of columns you want.
        $ct++;
    }
    if($ct!==1) echo "</tr>";//If the loop ends on 3 then the $ct=0; and $ct++; will still fire making it a one.  Therefore we test for $ct not equal to 1 in order to terminate the row properly.
    echo("</table>");