/    Sign up×
Community /Pin to ProfileBookmark

A function I have….

[code=php]function aRows($text)
{
if($iadd == 0)
{
echo ‘<table border=”0″ bgcolor=”#CCCCCC”><tr><td>’.$text.'</td></tr></table>’;
$iadd = 1;
}
elseif($iadd == 1)
{
echo ‘<table border=”0″ bgcolor=”#FF0000″><tr><td>’.$text.'</td></tr></table>’;
$iadd = 0;
}
}[/code]

And I might execute the following:

[code=php]<?php

aRows(“First row”);
aRows(“Second row”);
aRows(“Third row”);
// etc.

?>[/code]

However, it appears my function has a bit of a problem alternating $iadd between 1 and 0.

My code works, if I do not call it in a function:

[code=php]if(!preg_match(“/^[a-zA-Z]+w{5,20}$/”,$d_username))
{
if($iadd == 0)
{
echo ‘<table width=100% border=”0″ bgcolor=”#CCCCCC”><tr><td>’.’Invalid username. Min 5 characters and must start with a letter. No symbols, only underscores.’.'</td></tr></table>’;
$iadd = 1;
}
elseif($iadd == 1)
{
echo ‘<table width=100% border=”0″ bgcolor=”#FFFFFF”><tr><td>’.’Invalid username. Min 5 characters and must start with a letter. No symbols, only underscores.’.'</td></tr></table>’;
$iadd = 0;
}
}
if($d_password == $d_username)
{
if($iadd == 0)
{
echo ‘<table width=100% border=”0″ bgcolor=”#CCCCCC”><tr><td>’.’Username cannot be same as password.’.'</td></tr></table>’;
$iadd = 1;
}
elseif($iadd == 1)
{
echo ‘<table width=100% border=”0″ bgcolor=”#FFFFFF”><tr><td>’.’Username cannot be same as password.’.'</td></tr></table>’;
$iadd = 0;
}
}[/code]

However this is quite tiresome and messy, I’m not sure how to make the $iadd variable work in the function, and not sure where to call it. I tried calling it before the function is called, however it returns an ‘undefined variable’ error. I guess the only way is to call it inside the function, however, I am not sure where.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@esquiladoauthorMar 13.2010 — is noone online? =/
Copy linkTweet thisAlerts:
@esquiladoauthorMar 13.2010 — bump =)
Copy linkTweet thisAlerts:
@MindzaiMar 13.2010 — I usually alternate like so:

[COLOR="White"]&#37;[/COLOR]
[code=php]
$i = 0;
while ($i < 100) {
if ($i++ % 2 == 0) {
echo "alt1";
} else {
echo "alt2";
}
}[/code]
Copy linkTweet thisAlerts:
@esquiladoauthorMar 13.2010 — Thanks! I use that method too, but I don't know how I would put that method to use with the example I showed above.
Copy linkTweet thisAlerts:
@esquiladoauthorMar 13.2010 — I worked it out by making $iadd global, I simply changed my function to the following:
[code=php]function aRows($text)
{
global $iadd;
if($iadd == 0)
{
echo '<table border="0" bgcolor="#CCCCCC"><tr><td>'.$text.'</td></tr></table>';
$iadd = 1;
}
elseif($iadd == 1)
{
echo '<table border="0" bgcolor="#FF0000"><tr><td>'.$text.'</td></tr></table>';
$iadd = 0;
}
} [/code]
Copy linkTweet thisAlerts:
@MindzaiMar 13.2010 — I would suggest passing the $iadd variable as an argument rather than using a global. There's no risk of accidentally modifying it within the function that way.
Copy linkTweet thisAlerts:
@esquiladoauthorMar 13.2010 — I tried, but couldn't get it to work. I didn't know how to use it when I used it as an argument.
Copy linkTweet thisAlerts:
@MindzaiMar 13.2010 — You'd do it like this:

[COLOR="White"]&#37;[/COLOR]
[code=php]
function aRows($text, $iadd) {
if($iadd == 0) {
return '<table border="0" bgcolor="#CCCCCC"><tr><td>'.$text.'</td></tr></table>';
}
return '<table border="0" bgcolor="#FF0000"><tr><td>'.$text.'</td></tr></table>';
}

// in your main code
$i = 0;
while ($i < 100) {
echo aRows('foo', $i++ % 2);
}
[/code]


The main code takes care of keeping track of odd and even rows, and the function

just gives you the right result.

I also modified the function to return the value rather than echoing it - this makes the function more flexible because you can still echo it easily, plus you can also save the result in a string.

One final point - you should really be doing this sort of thing with CSS rather than HTML attributes. You might want to think about modifying this to return a class name rather than HTML.
Copy linkTweet thisAlerts:
@esquiladoauthorMar 13.2010 — I did the CSS thing first as it is easiest.

'<table style="width:100&#37;; border:0px; background-color:#FFFFFF><tr><td>'.$text.'</td></tr></table>';

That is CSS right lol?

What is 'foo' ?
×

Success!

Help @esquilado spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...