Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Split Results in two columns using array



1:05 pm on Mar 22, 2011 (gmt 0)

5+ Year Member

I've developed a simple page to retrieve all results from a specific table in a database, this works fine. The results are formatted in just one column at the minute but I would like to split this into two columns.

I have searched and read about arrays but I can't get it to work so I'm hoping someone can help me out.

I've also implemented seperate row colours to make things clearer, I would like to keep this if at all possible.

require_once ('includes/config.php');
require_once ('includes/database.php');

$sql="SELECT * FROM pricelist ORDER BY pricelist_grade ASC";
$result = mysql_query($sql) or die(mysql_error());
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<title>Price List</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
<?php include_once ('includes/menu.php'); ?>
<?php if (mysql_num_rows($result) == 0) {
echo "<h1>No Results Found</h1>";
} else { ?>
<table cellpadding="0" cellspacing="0" id="models">
<tr class="headings">
<th class="model">Grade</th>
<th class="weight">PMT</th>
$color1 = "#D7DFFF";
$color2 = "#F3F3F3";
$row_count = 0;

while($row=mysql_fetch_array($result)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
<tr style="background-color:<?php echo $row_color ?>;">
<?php $row_count++;
<?php } ?>

I've pasted the entire page as there isn't much there anyway.

Thanks in advance


8:57 pm on Mar 23, 2011 (gmt 0)

5+ Year Member

whatever happens inside the while loop happens over and over again, once for each record

while($row=mysql_fetch_array($result)) {

Inside your while loop is a full table row: TR TD /TD /TR

<tr style="background-color:<?php echo $row_color ?>;">

What you want is for the TR /TR part to happen every other row, so that you get two TDs for each row.

You could use the same trick you use for rowcolor:

$row_color = ($row_count % 2) ? $color1 : $color2;

like this:

$new_tr = ($row_count % 2) ? 'yes' : 'no':

Now within while you make the TR tags conditional:

if ($new_tr) echo '<tr>';

if ($new_tr) echo '</tr>';

Hope that makes sense.


9:45 pm on Mar 23, 2011 (gmt 0)

5+ Year Member

Thanks jbroder

Your idea is exactly what I'm looking for, however could you give me some help with the coding, I can't get it working.

It's probably obvious, I will keep looking just incase I work it out :)


3:53 pm on Mar 25, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

In that example, change this

$new_tr = ($row_count % 2) ? 'yes' : 'no'; // <-- note it was in error

to this

$new_tr = ($row_count % 2) ? 'yes' : null;

Reason being, it will always be "yes" or "no" so when you do "if $new_tr", "no" returns true. Null will not.


10:01 am on Mar 28, 2011 (gmt 0)

5+ Year Member

I've got it half working using some completely different code but I've lost the alternating row colours now.

Here's what I've got to split the results up into two columns

echo"<table cellpadding=\"0\" cellspacing=\"0\" id=\"models\">\n";
echo"<tr><td valign=\"top\" id=\"leftcol\">\n";
echo"</td><td valign=\"top\" id=\"rightcol\">\n";
echo"<table><tr class=\"headings\"><th class=\"model\">Grade</th><th class=\"weight\">PMT</th></tr>\n";

Is there a way of modifying this code to alternate the colours or do I need to use my original code?


10:27 am on Mar 28, 2011 (gmt 0)

5+ Year Member

I would prefer to use my original code as I have another page that uses the same code but this page updates the prices. Using the new code on the price update page stops it from working.

Featured Threads

Hot Threads This Week

Hot Threads This Month