homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Split Results in two columns using array

Msg#: 4285654 posted 1:05 pm on Mar 22, 2011 (gmt 0)

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



5+ Year Member

Msg#: 4285654 posted 8:57 pm on Mar 23, 2011 (gmt 0)

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.


Msg#: 4285654 posted 9:45 pm on Mar 23, 2011 (gmt 0)

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 :)


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

Msg#: 4285654 posted 3:53 pm on Mar 25, 2011 (gmt 0)

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.


Msg#: 4285654 posted 10:01 am on Mar 28, 2011 (gmt 0)

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?


Msg#: 4285654 posted 10:27 am on Mar 28, 2011 (gmt 0)

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved