Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-11-2013, 03:43 PM   PM User | #1
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
change Background color.

How can you set it up to change the background color every second record.?


Thanks in advance.


Slayer.
SlayerACC is offline   Reply With Quote
Old 02-11-2013, 04:12 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Use modulus or bit operands for the zebra striping. I'll use bit operands which is only useful for every other row, as opposed to modulus which can use multipliers like 3 or 4 row changes. I'll also use a table since I'm a bit on the lazy side:
PHP Code:
$a range(110);
$i 0;
print 
'<table>';
while (++
$i && $current current($a))
{
    if (
$i 1)
    {
        
$colour '#fff';
    }
    else
    {
        
$colour '#eee';
    }
    
printf('<tr style="background-color: %s"><td>%d</td></tr>'$colour$current);
    
next($a);
}
print 
'</table>'
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
SlayerACC (02-11-2013)
Old 02-11-2013, 05:40 PM   PM User | #3
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
Hey Fou Lou,

I was trying to figure our what you had posted... to no eval...

Thanks...


I am trying to incorperate it in with retrieving records from the database.
PHP Code:
$query "SELECT application_id, firstName, middleName, lastName, phone, email, date_format(appdate, '%m/%d/%Y') as appdate FROM applications";
$results mysqli_query($mysqli,$query)or die(mysqli_error($mysqli));

while(
$row mysqli_fetch_array$results )) {
$fname=$row['firstName'];
$mname=$row['middleName'];
$lname=$row['lastName'];
$appdate=$row['appdate'];
$phone=$row['phone'];
$email=$row['email'];


echo 
"<tr><td width=\"255\" class=\"lborder\">";
echo 
$fname;
echo 
" ";
echo 
$mname;
echo 
" ";
echo 
$lname;
echo 
"</td><td width=\"100\">";
echo 
$appdate;
echo 
"</td><td width=\"125\">";
echo 
$phone;
echo 
"</td><td width=\"200\">";
echo 
$email;
echo 
"</td><td width=\"80\" class=\"rborder\">";
echo 
"E | D | P";
echo 
"</td></tr>";

Thanks again Fou Lou.


Slayer
SlayerACC is offline   Reply With Quote
Old 02-11-2013, 05:49 PM   PM User | #4
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Basically what i did with mine (and thanks Fou-Lu for the official tech term zebra striping )

The easiest way i found is to set your first color before the record loops start.

Then as you finish the output of each row, check the color, if the color is color 1 then change it to color 2, if it is color 2 change it to color 1. That way each row is a different color.

rowa and rowb are set as class in css or you can set them in the file your in using style

Code:
.rowa {
background-color:#FFFFFF;  /* white */
}

.rowb{
background-color:#000000; /* black */
}
set your rowb color and rowa colors before the loop starts, then at the beginning of the loop before the output of the first row, set $switchrow to either rowa or rowb,

then like

Code:
<tr class="<?=$switchrow;?>" >
then at the end of the loop before the next record try something like this.

PHP Code:
//switch the row color if need to
if($switchrow == "rowb")
  {
  
$switchrow "rowa";
   }else{
          
$switchrow "rowb";
           }
//close else 

Last edited by durangod; 02-11-2013 at 06:11 PM.. Reason: sorry forgot something, corrected
durangod is offline   Reply With Quote
Users who have thanked durangod for this post:
SlayerACC (02-11-2013)
Old 02-11-2013, 06:22 PM   PM User | #5
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
Thanks Durangod..

That works like a charm...

I found that I was trying the same thing but could not get it to work..

This was the part I was doing wrong.
Quote:
then at the end of the loop before the next record try something like this.
Thanks...

Slayer
SlayerACC is offline   Reply With Quote
Old 02-11-2013, 07:26 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yep, that'll work too.
Personally, I don't like working with strings when I can avoid them. So I prefer dealing with the integers or booleans to flip them instead of using strings.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:52 AM.


Advertisement
Log in to turn off these ads.