Welcome to WebmasterWorld Guest from 54.163.54.95

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)

Full Member

5+ Year Member

joined:Aug 3, 2010
posts: 243
votes: 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.

<?php
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">
<head>
<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" />
</head>
<body>
<?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>
</tr>
<?php
$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 ?>;">
<td><?=$row['pricelist_grade']?></td>
<td>&pound;<?=$row['pricelist_price']?></td>
</tr>
<?php $row_count++;
}?>
</table>
<?php } ?>
</body>
</html>


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)

Junior Member

5+ Year Member

joined:Oct 3, 2007
posts:61
votes: 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 ?>;">
<td><?=$row['pricelist_grade']?></td>
<td>&pound;<?=$row['pricelist_price']?></td>
</tr>

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)

Full Member

5+ Year Member

joined:Aug 3, 2010
posts: 243
votes: 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 :)
3:53 pm on Mar 25, 2011 (gmt 0)

Senior Member

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

joined:Nov 28, 2004
posts:7999
votes: 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.
10:01 am on Mar 28, 2011 (gmt 0)

Full Member

5+ Year Member

joined:Aug 3, 2010
posts: 243
votes: 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


<?php
echo"<table cellpadding=\"0\" cellspacing=\"0\" id=\"models\">\n";
echo"<tr><td valign=\"top\" id=\"leftcol\">\n";
$num=mysql_num_rows($result);
$col1=ceil($num/2);
$col2=$num-$col1;
for($j=0;$j<2;$j++)
{
if($j==0)
{
$limit=$col1;
}else{
$limit=$col2;
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";
for($i=0;$i<$limit;$i++)
{
$row=mysql_fetch_assoc($result);
echo"<tr><td>".$row['pricelist_grade']."</td><td>".$row['pricelist_price']."</td></tr>\n";
}
echo"</table>\n";
}
echo"</td></tr></table>\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)

Full Member

5+ Year Member

joined:Aug 3, 2010
posts: 243
votes: 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.