Displaying data in 3 columns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wardy7
Forum Commoner
Posts: 38
Joined: Wed Aug 24, 2005 4:45 am
Location: UK

Displaying data in 3 columns

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The first two threads linked from Useful Posts should be of interest.
Wardy7
Forum Commoner
Posts: 38
Joined: Wed Aug 24, 2005 4:45 am
Location: UK

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you looked at the posts I referred to?
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Use a counter variable.

Post 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>");
Post Reply