SitePoint Sponsor |
|
User Tag List
Results 1 to 22 of 22
-
Jul 29, 2006, 03:12 #1
- Join Date
- Sep 2005
- Location
- Zurconion Soon!
- Posts
- 1,549
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Even and Odd rows of color, my best try.
This is the only way I could figure this out.
Is this a good way to go about it? I dont know if I have errors or not.
Is there perhaps an easier way?
I tried to make it decide by even/odd #'s skipping 0
(I dont know how to work the even/odd thing very well so i hope its right)
PHP Code:<?php
function row($y) {
echo $y++;
return $y;
}
$y = 1;
if ($y% 2 == 0 ) {
echo "#fff";
}
else {
echo "#000";
}
?>
PHP Code:<?php include_once ("color-rows.php"); ?>
PHP Code:<div style="background-color:"<?php $y= row($y); ?>;">
-
Jul 29, 2006, 03:36 #2
Take a look at this:
http://www.hamidof.com/Programmers_F...opic.php?t=156
Its not exactly what you asked, but it might help!
-
Jul 29, 2006, 10:28 #3
- Join Date
- Mar 2006
- Posts
- 6,132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
if you just want to alternate between 2 colors, its pretty easy
you can use any type of loop, im just going to use foreach to demo
PHP Code:$y = 0;
foreach ($foo as $bar) {
$color = ($y++ % 2) ? 'red' : 'green';
echo $color;
}
// $color = ($y++ % 2) ? 'red' : 'green'; is the same as the following, just shorter
if (($y % 2) != 0) {
$y++;
$color = 'red';
} else {
$y++;
$color = 'green';
}
-
Jul 29, 2006, 23:32 #4
- Join Date
- Sep 2005
- Location
- Zurconion Soon!
- Posts
- 1,549
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thats a lot easier way to do it, I am still uncomfortable with using the ()?x:y function even though Ive read over it a few times.
I dont understand what $foo and $bar is. What would replace those variables?
-
Jul 30, 2006, 02:06 #5
- Join Date
- Dec 2005
- Location
- Cambridge, England
- Posts
- 2,443
- Mentioned
- 82 Post(s)
- Tagged
- 3 Thread(s)
This is a method I found somewhere and use.
PHP Code:// Define your colors for the alternating rows
$color1 = "#90BCDE";
$color2 = "#D2E3F0";
$row_count = 0;
while ($row = mysql_fetch_array($result)) {
/* Now we do this small line which is basically going to tell
PHP to alternate the colors between the two colors we defined above. */
$row_color = ($row_count % 2) ? $color1 : $color2;
// Display the results
echo "<TR bgcolor='".$row_color."'><TD align='center'>".$row['referance_num']."</TD></TR>";
// Add 1 to the row count
$row_count++;
}
Last edited by Rubble; Jul 30, 2006 at 02:08. Reason: Added link
-
Jul 30, 2006, 09:49 #6
- Join Date
- Jul 2006
- Location
- Kansas City, MO
- Posts
- 280
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This will be the easiest and quickest method for accomplishing this.
CSS File
--------
tr.row0 { background-color: red; }
tr.row1 { background-color: blue; }
PHP File
-------
PHP Code:// inside while loop for displaying data
$c ^= 1;
echo "<tr class=\"row$c\">";
-
Jul 30, 2006, 10:48 #7
- Join Date
- Mar 2006
- Posts
- 6,132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by lostseed
in fact, you actually dont even need to place it inside of a loop, but i would imagine thats what you want to do.
-
Jul 31, 2006, 14:38 #8
- Join Date
- Sep 2005
- Location
- Illinois
- Posts
- 189
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by ZareMedia
.table_row_0 {background-color:#EEEEEE;}
.table_row_1 {background-color:#DDDDDD;}
$row_style = 0;
while($row = mysql_fetch_row($result)) {
$row_style = 1 - $row_style;
...
<tr class='table_row_".$row_style."'>
-
May 1, 2007, 07:54 #9
- Join Date
- Jun 2005
- Posts
- 1,441
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I would love to use this code, but am unsure where to place the php part ?
At the moment, on http://www.130605.com/alternate-rows I have this in the page :
<?
$tablerow_count=0;
function tablerowswitch() {
global $tablerow_count;
$tablerow_count++;
if ($tablerow_count % 2) {
echo "odd";
}
else {
echo "even";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="description" content="Test">
<link rel="stylesheet" type="text/css" href="http://www.130605.com/styles.css">
</head>
<body>
<table summary="Alternate rows">
<tr class="<? tablerowswitch(); ?>">
<td>First row</td>
</tr>
<tr class="<? tablerowswitch(); ?>">
<td>Second row</td>
</tr>
<tr class="<? tablerowswitch(); ?>">
<td>Third row</td>
</tr>
<tr class="<? tablerowswitch(); ?>">
<td>Fourth row</td>
</tr>
</table>
</body>
</html>
Any help much appreciated.
Dez.
p.s. I can't seem to get the table into the center, but that's another problem!
-
May 1, 2007, 08:23 #10
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
Hi Dez, not much wrong with what you have now. You are just missing one last ingredient.....
you haven't echoed the class!
PHP Code:<tr class="<?php echo tablerowswitch(); ?>">
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
May 1, 2007, 10:28 #11
- Join Date
- Jun 2005
- Posts
- 1,441
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Many thanks for that SpikeZ, it's appreciated. Thanks also for the aligning tip - got that one solved now.
Is there any way of not including any php code within the tables and for it to still work please ? Looking at some of these posts on this thread, it seems as though there might be a way to do it, but because I'm a little new to this, am a little unsure. It would sure be great to add and take out rows, without worrying about the php code. It would be great if this could be done without javascript anywhere.
Any help very much appreciated.
Dez.Last edited by Dez; May 2, 2007 at 07:49.
-
Jun 20, 2007, 03:14 #12
- Join Date
- Jun 2005
- Posts
- 1,441
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jun 20, 2007, 03:51 #13
- Join Date
- Aug 2005
- Posts
- 122
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
rather than doing the modulus (%
.... just do a compare... % is more cpu intensive than a simple compare (not much, but its obviously in a loop);
use classes (css) and just do the class names...like this
then just put the class name in. Having a function for it is WAY overkill... (my opinion, of course)
PHP Code:
$class = ($class == 'row1') ? 'row2' : 'row1';
Developer
Grow Interactive
-
Jun 20, 2007, 03:51 #14
- Join Date
- Oct 2006
- Location
- Kathmandu, Nepal
- Posts
- 4,013
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How about for the alternate two colors?
PHP Code:while($rows = $objDb->FetchRows($result)){
$bgcolor = ($bgcolor == "#E4E4E4") ? "#FFFFFF" : "#E4E4E4";
echo '<tr bgcolor="' . $bgcolor . '">';
echo '<td align="left" style="height:24px;"> </td>';
echo '<td align="left">' . $rows['company_name']. '</td>';
echo '<td align="left">' . $rows['project_name'] .'</td>';
echo '<td align="left"> </td>';
echo '<td align="left"> </td>';
echo '<td align="left">' . $rows['leadfromdate'] .'</td>';
echo '<td align="left"> </td>';
echo '</tr>';
}
Mistakes are proof that you are trying.....
------------------------------------------------------------------------
PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5
-
Jun 20, 2007, 06:31 #15
- Join Date
- Aug 2005
- Posts
- 122
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
rajug...
that works...same idea as mine...
I would never do that because 1) bgcolor is depreciated... and 2) classes will save you when the client decides he wants to change the colors on all the tables on the siteDeveloper
Grow Interactive
-
Jun 20, 2007, 06:51 #16
- Join Date
- Jun 2005
- Posts
- 1,441
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jun 20, 2007, 07:30 #17
- Join Date
- Aug 2004
- Location
- Colwyn Bay, Wales, UK
- Posts
- 287
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
just use a static variable!
Code:function rowcolor() { static $col = 1; if($col == 1) { $col = 2; return "#fff"; } else { $col = 1; return "#000"; } }
-
Jun 20, 2007, 07:40 #18
- Join Date
- Jun 2005
- Posts
- 1,441
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Many thanks for that - it's appreciated.
Would that work for the headers and footers as well please ?
-
Jun 20, 2007, 07:45 #19
- Join Date
- Aug 2004
- Location
- Colwyn Bay, Wales, UK
- Posts
- 287
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
you would need to create a new function for each set of rows, or use this modified function:
Code:function rowcolor($num=1) { static $col$num = 1; if($col$num == 1) { $col$num = 2; return "#fff"; } else { $col$num = 1; return "#000"; } }
-
Jun 20, 2007, 09:29 #20
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
I spotted this tutorial today. Shows a couple of ways of doing the same thing, has timings too, showing which is quickest.
-
Jun 20, 2007, 12:49 #21
- Join Date
- Jun 2005
- Posts
- 1,441
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jun 20, 2007, 13:03 #22
- Join Date
- Jun 2005
- Posts
- 1,441
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Bookmarks