homepage Welcome to WebmasterWorld Guest from 54.166.105.24
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
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
LinusIT




msg:4285656
 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.

<?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

 

jbroder




msg:4286481
 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 ?>;">
<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.

LinusIT




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

rocknbil




msg:4287457
 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.

LinusIT




msg:4288391
 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


<?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?

LinusIT




msg:4288398
 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