Forums

Passing Variable between Header and Body

sasserc 26 Jan, 2010
Ok, I give up. I'm a newbie but I have done two Joomla sites and one was launched last week as an event registration site.

I decided to start a new Thread.

I used CF with no problems, in fact we have 100+ people registered since Friday. I wanted to create accessible with a login and contained alternating colored rows in the list/table. The form/list is work no problem, but I really wanted to alternate row colors.

After reading all the forums this looked fairly simple. Just use php (which I have never used before)and decare a variable in the form header and then increment the variable in the body and use that to determine an "odd" class to set the color in a CSS. No problem, right. 😟

After spending all day Sunday trying to make this work, I finally realized that CC would not pass a variable between the header and body. I have search all the forums and it seems everyone else is able to make this work, however, None of the code I found would work.

So I finally decided to do something really simple and created the following VERY simple code:

Header Code:
<?php $i=0; ?>

Body Code:
<?php echo $i; ?>

The result, nothing is created. However if I echo the value of the variable in the header, it is viewable or if I declare the variable and echo in the Body, it is viewable.

Conclusion:

I can NOT pass a variable from header to footer.

Any thought??

CC version is 2.0 RC
Joomla Version is 1.5.15
php is 4.4.9
sql is 5.0.81-community
host is Lunarpages shared host
Apache version is 1.3.41 (Unix)
GreyHead 26 Jan, 2010
Hi sasserc,

The body scope is different but I think this will work if you declare the variable as global:
Head:
<?php 
global $i;
$i=0; 
?>

Body:
<?php 
global $i;
echo $i; 
?>
. . . // display data record
<?php
$i++;
?>


Bob
sasserc 26 Jan, 2010
Awesome. Works great I'll be dangerous now. (my first working php)

I moved your footer code:
<?php $i++; ?>
to the bottom of the body to increment by "1" for each record. Now I can number the rows or force a class to generate an odd or even row for formating a table background color, etc. via css.

Thanks!!
GreyHead 26 Jan, 2010
Hi sasserc,

Yes my mistake, sorry. I'll go back and correct the post for other readers.

Bob
GMiranda 04 Mar, 2010
Hi Bob,

It seems like if the global variable passes the value from the Header to the Body, but not to the Footer (?!).

I put this code in the Header:

<?php
global $num;
$num=30;
?>


Then I put this in the Footer:

<? echo "Number: $num"; ?>


The result in the Footer is: Number:

Is it the case perhaps that a Global variable is not so "Global"?

Tnaks in advance,
Gonzalo
GreyHead 04 Mar, 2010
Hi GMiranda,

Declare the variable as global in the Footer box too.

Bob
GMiranda 04 Mar, 2010
Hi Bob,

Aha! So the trick is to declare "global" the variable in any section where you want to use it. Simple!
I worked perfectly.

Thans a lot,
Gonzalo
GreyHead 04 Mar, 2010
Hi Gonzalo,

Yes, in PHP variable scope is local unless you declare the variable global in both places.

Bob
This topic is locked and no more replies can be posted.