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>£<?=$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.
|
|