FORUM HOME | WHUK BLOG   
WEB HOSTING UK AFFORDABLE WEBSITE HOSTING SERVICES IN UNITED KINGDOM
PHP LINUX SHARED HOSTING WINDOWS ASP.NET HOSTING PACKAGES
ECOMMERCE HOSTING ASP MSSQL MS ACCESS ODBC FRONTPAGE HOSTING
CPANEL WHM FANTASTICO RESELLER DEDICATED SERVER WEB HOSTING
CHEAP PLESK CPANEL HTML MYSQL BEST UK VPS HOSTING COMPANY
CHEAP RELIABLE UK HOSTING PROVIDER SINCE 2001
MANAGED WEB HOSTING SERVICE
AFFORDABLE WEBSITE HOSTING SERVICES IN UNITED KINGDOM

Web Hosting UK Forums | Linux Windows Dedicated Server and cPanel VPS Hosting Forum » Technical Support » PHP Hosting

View Poll Results: Please rate this tip
Very useful 1 33.33%
Useful 2 66.67%
May be useful 0 0%
Not that useful 0 0%
Voters: 3. You may not vote on this poll

Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)  
Old 14-05-09, 10:04 AM
black-dog's Avatar
Senior Member
 
Join Date: May 2008
Location: Newcastle under Lyme
Posts: 205
Send a message via Yahoo to black-dog
Default PHP tips. Zebra stripes.

A number of web designers could make their lives a lot easier with some simple scripting. One such occasion is zebra stripes, ie using different colours for alternating rows in lists and tables.

Sure you can hard code these easily enough. The problem is that if a row is added or deleted, the rest of the list has to be reformatted. So save yourself some effort and let PHP do the work for you.

Start by making two classes for the alternating rows
Code:
<style type="text/css" >
.zebra2{
background-color:#ebb;
padding:2px;
font-family:"Trebuchet MS";
}
.zebra1{
background-color:#bbe;
padding:2px;
font-family:"Trebuchet MS";
}
</style>
I have this inline in the examples but it can be in the stylesheet if you like.

Then, instead of hard coding your list of items or table rows, add them to an array like so:

PHP Code:
<?php
$listitems
=array(
"apples",
"bananas",
"oranges",
"lemons",
"pears"
);?>
Add or take away items from the list as you like, the zebra striping will take care of itself

Finally, enbed the following snippet of PHP in your html to output the zebra striped list items

PHP Code:
<?php
$count
=0;
foreach(
$listitems as $item){
$style=($count%2>0)?"class='zebra1'":"class='zebra2'";
echo 
"<li $style>$item</li>\n";
$count++;
}
?>
Edit the 'echo' line to change this to a table row etc.

Example here http://www.4theweb.co.uk/php_tips/zebralist.php

Source: http://www.4theweb.co.uk/php_tips/zebralist.phps
__________________
black-dog
4theweb.co.uk Web stuff
slipperyhill.co.uk Band
Reply With Quote

  #2 (permalink)  
Old 15-05-09, 09:30 AM
Senior Member
 
Join Date: Jan 2007
Location: Dorset
Posts: 1,119
Default

much better and tidier than mine. I also count the amount of rows then change background colour if it divides by 2

PHP Code:
if ($row 2) { 
    
$colour"#EBEBEB"

PHP Code:
 echo"
<tr bgcolor=$colour>................................"

I did try and use 3 or 4 different colours by creating arrays and using in_array, but using more then 2 colours in table rows or divs never looks right to me
Reply With Quote

Reply

Tags
css, html, php, web

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT. The time now is 08:11 AM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Copyright 2001-2010 Web Hosting UK. All rights reserved.
Web Hosting UK Forum





Site Map

Shared Cloud
Shared Cloud From £1

Affiliate Program
Earn up to £300 Per Sale

Dedicated Servers
Dedicated Server Hosting

Cloud Hosting
Cloud Server Hosting

Load Balanced Server
Load Balancing Server

VPS Hosting
Linux VPS Hosting

Windows VPS
Windows 2003 VPS

Zimbra Hosting
Zimbra Email Hosting

cPanel Hosting
Shared Linux Hosting

Windows Hosting
Shared Windows Hosting

Coldfusion Hosting
Windows Coldfusion Hosting

cPanel Reseller Hosting
Reseller Hosting

Windows Reseller
Windows Reseller Hosting

Email Web Hosting
Email Hosting

Semi-Dedicated Server
Semi-Dedicated Hosting

Remote Backup Plans
Offsite Backup Service


cpanel hosting
Knowledgebase Articles

Pre-Sales Question
Web Hosting FAQ's

Dedicated Hosting
Dedicated Server FAQ's

Virtual Private Servers
VPS Hosting

PHP MySQL Hosting
cPanel Hosting

Windows Hosting
ASP MSSQL Hosting

Domain Name
Domain registration FAQ's

CMS Hosting
CMS Hosting FAQ's

Payment Gateways
Payment FAQ's


Support Tutorials

cPanel Tutorials
cPanel Flash Tutorials

Wordpress Tutorials
Wordpress Flash Tutorials

Plesk Tutorials
Plesk Flash Tutorials

PhpMyadmin Tutorials
PhpMyadmin Flash Tutorials

Drupal Tutorials
Drupal Flash Tutorials

Mambo Tutorials
Mambo Flash Tutorials

Joomla Tutorials
Joomla Flash Tutorials

More Hosting Tutorials