|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
What is Wrong w/ this Code??
I am practicing taking code from other scripts (javascript) and reproducing it in PHP. This code produces a hierarchical navigation on the fly based on a CSS and the function sideNav. The background colors alternate depending on the variable $vLevel and the action in the 3rd echo statement within the function // $setCols[ (($rCol % 2) + $vBG) ]) //
Unfortunately, this all works without error except the background color doesn;t display. Further, I cannot get a value for $rCol at all unless I echo it outside of the function code block. I am a novice and I am having a hard time figuring out A) what is missing and B) what I can do to troubleshoot it. Code follows... php Code:
Out put: http://www.moontots.com/test/index_test_php.php |
|
#2
|
|||
|
|||
|
RE: What is Wrong w/ this Code??
Why don't you try doing your math
(($rCol % 2) + $vBG) outside of the string, and make sure your getting a valid value. In other words try: Code:
function sideNav($vLevel, $vLink, $vURL)
{
if ($vLevel ==1 )
$vBG = 0;
if ($vLevel ==2 )
$vBG = 2;
if ($vLevel ==3 )
$vBG = 4;
$x=($rCol % 2) + $vBG;
// echo $x; You could add this to see what value is being calculated each time.
echo "<TR><TD width=10 bgcolor=#003366>";
echo "<font color=#ffcc00><b>></b></TD>";
echo "<TD BGCOLOR=".$setCols[$x].">";
echo "<DIV Class=nav".$vLevel.">";
echo "<a href=".$vURL.">".$vLink."</A>";
echo "</DIV></TD>";
$rCol = $rCol + 1;
}
?>
|
|
#3
|
|||
|
|||
|
RE: What is Wrong w/ this Code??
Also, it appears you have no globals in your function, and you're using variables not called to the function itself.
|
|
#4
|
|||
|
|||
|
RE: What is Wrong w/ this Code??
Thanks Brut, trying that now. And Anonymous, does PHP require I declare variables for functions?
If so, can you give me an example? |
|
#5
|
|||
|
|||
|
RE: What is Wrong w/ this Code??
It is the variable $rCol that is not registering.
And it is not incrimenting. Why is that?? |
|
#6
|
|||
|
|||
|
RE: RE: What is Wrong w/ this Code??
PHP does not enforce variable declaration. Which, IMHO, can lead to some sloppy/spaghetti code and a maintenance nightmare when supporting someone else's code.
To answer your question... Code:
function Whatever() {
$rCol = 0; // declared and initialized variable
.
.
}
HTH Quote:
|
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > What is Wrong w/ this Code?? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|