Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » PHP to alternate row columns
Closed Thread
Old 03-20-2009, 03:08 PM   #1 (permalink)
 
Newb Techie

Join Date: Sep 2006

Posts: 27

bukwus is on a distinguished road

Default PHP to alternate row columns

Hi

I'm a beginner to functions and I tried using the following to alternate the background color of rows with no luck. Can anyone hlep?

<?php
function backgroundColor() {
if ($bg == 'yes') {
echo '';
$bg = 'no';
}
elseIf ($bg == 'no') {
echo 'bgColor';
$bg = 'yes';
}
else {
echo 'error';
}
}

$bg = 'no';
?>

<style>
.bgColor {
color:gray;
}
</style>

<div class="<?php backgroundColor() ?>">
Row 1 content
</div>

<div class="<?php backgroundColor() ?>">
Row 2 content
</div>

<div class="<?php backgroundColor() ?>">
Row 3 content
</div>
bukwus is offline  
Old 03-20-2009, 05:37 PM   #2 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: PHP to alternate row columns

Your function works, however you have a scope problem. By default, functions don't inherit variables from the rest of the script. It has no idea that $bg was ever initialized.

To fix this, just add global $bg; to the top of your function.

PHP Code:
<?php
function backgroundColor() {
global 
$bg;
if (
$bg == 'yes') {
echo 
'';
$bg 'no';
}
elseIf (
$bg == 'no') {
echo 
'bgColor';
$bg 'yes';
}
else {
echo 
'error';
}
}

$bg 'no';
?>

__________________

Need website help? PM me!
CrazeD is offline  
Old 03-24-2009, 12:44 PM   #3 (permalink)
 
Newb Techie

Join Date: Sep 2006

Posts: 27

bukwus is on a distinguished road

Default Re: PHP to alternate row columns

Thank you. This works great.

Quote:
Originally Posted by CrazeD View Post
Your function works, however you have a scope problem. By default, functions don't inherit variables from the rest of the script. It has no idea that $bg was ever initialized.

To fix this, just add global $bg; to the top of your function.

PHP Code:
<?php
function backgroundColor() {
global 
$bg;
if (
$bg == 'yes') {
echo 
'';
$bg 'no';
}
elseIf (
$bg == 'no') {
echo 
'bgColor';
$bg 'yes';
}
else {
echo 
'error';
}
}

$bg 'no';
?>

bukwus is offline  
 
Closed Thread

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Guide to Installing Apache, PHP, MySQL, and PHPMyAdmin on Windows CrazeD Programming Discussions 17 06-17-2009 04:27 PM
How echo works in PHP Osiris Programming Discussions 0 03-04-2009 08:34 AM
PHP - what it does and what it doesn’t Osiris Programming Discussions 1 02-16-2009 04:09 PM
Wev Development: How does PHP work? Osiris Programming Discussions 1 01-08-2009 04:31 PM