Forum Moderators: coopster

Message Too Old, No Replies

Variable Variables

variables

         

XavierG

9:14 am on Dec 4, 2008 (gmt 0)

10+ Year Member



I've a strange behaviour trying to put array values into a dynamically defined variables. I've googled around, without any help.

Here's the issue. I've got array values picked from MySQL table: periods of year that have their own events ('$ArrayPeriod[0]' to '$ArrayPeriod[24]', number of results of that array is stored in '$nr_of_periods' variable, in this case: 25)

Those periods are later used as column titles in MySQL recordset while looped through FOR loop in order to define dynamic variables 'Event0' to 'Event24' for storing values in fields found at periods
'$ArrayPeriod[1]' to '$ArrayPeriod[24]'.

Problem is that 'Event0' variable takes '$ArrayPeriod[0]' column value Ok (first loop of FOR loop,$ColLoop=0), but in another pass (ColLoop=1) dynamic variable '$Event1' is created OK, but value from '$ArrayPeriod[1]' cannot be passed to it ?! So, echo shows that $Event1 till $Event24 are empty.. :-(

If I put echo 'directly' from '$Event1','$Event2','$Event3' etc. value shows OK!?

Where did I go wrong!?

So the code looks like this

$ArrayPeriod = array("01.01.2008-15.01.2008", "16.01.2008-31.01.2008", "01.02.2008.-15.02.2008" ...);

$sql_query ="SELECT this from that";
$result = mysql_query($sql_query);

while ($row = mysql_fetch_array($result))
{

for ($ColLoop=0; $ColLoop<=$nr_of_periods; $ColLoop++)
{
$varMainName = "Event" . $ColLoop;
$$varMainName = $row[$ArrayPeriod[$ColLoop]];

echo "$varMainName, ${$varMainName}<br>";

}

}

I didn't specify $sql_query, since it's not important for this, since I'm getting same issus to every $sql_query that I put.

Tnx in advance,

Xavier.

milocold

9:58 am on Dec 4, 2008 (gmt 0)

10+ Year Member



Hi,

I'm curious, can you provide data on what a sample row from $row = mysql_fetch_array($result) would look like?

When I did a test (assuming that the date range where the sql columns names) I used this:

$ArrayPeriod = array("01.01.2008-15.01.2008", "16.01.2008-31.01.2008", "01.02.2008.-15.02.2008");

$PretendSqlResultsRow = array("01.01.2008-15.01.2008"=>"Result1",
"16.01.2008-31.01.2008"=>"Result2",
"01.02.2008.-15.02.2008"=>"Result3");

and got:

Event0, Result1
Event1, Result2
Event2, Result3
Event3,

but when the sql cols didn't match $ArrayPeriod's values:

$ArrayPeriod = array("01.01.2008-15.01.2008", "16.01.2008-31.01.2008", "01.02.2008.-15.02.2008");

$PretendSqlResultsRow = array("01.01.2008-15.01.2008"=>"Result1",
"16.01.2008-31.01.2008Foo"=>"Result2",
"01.02.2008.-15.02.2008Foo"=>"Result3");

Event0, Result1
Event1,
Event2,
Event3,

So I may be completely off base, but I was leaning towards the sql cols not matching up.

M. Cold

XavierG

12:07 pm on Dec 4, 2008 (gmt 0)

10+ Year Member



Tnx for reply, you're completely 'on' base ;-)

Complete Periods Array is this:


ArrayPeriod=array("03.01.2008-16.01.2008", "17.01.2008-30.01.2008","31.01.2008-13.02.2008","14.02.2008-27.02.2008", "28.02.2008-12.03.2008","13.03.2008-26.03.2008","27.03.2008-09.04.2008", "10.04.2008-23.04.2008","24.04.2008-07.05.2008","08.05.2008-21.05.2008", "22.05.2008-04.06.2008","05.06.2008-18.06.2008","19.06.2008-02.07.2008", "03.07.2008-16.07.2008","17.07.2008-30.07.2008","31.07.2008-13.08.2008", "14.08.2008-27.08.2008","28.08.2008-10.09.2008","11.09.2008-24.09.2008", "25.09.2008-08.10.2008","09.10.2008-22.10.2008","23.10.2008-05.11.2008", "06.11.2008-19.11.2008","20.11.2008-03.12.2008","04.12.2008-17.12.2008", "18.12.2008-31.12.2008")

After that 'Select' query looks like this:


SELECT
'03.01.2008-16.01.2008', '17.01.2008-30.01.2008', '31.01.2008-13.02.2008', '14.02.2008-27.02.2008', '28.02.2008-12.03.2008', '13.03.2008-26.03.2008', '27.03.2008-09.04.2008', '10.04.2008-23.04.2008', '24.04.2008-07.05.2008', '08.05.2008-21.05.2008', '22.05.2008-04.06.2008', '05.06.2008-18.06.2008', '19.06.2008-02.07.2008', '03.07.2008-16.07.2008', '17.07.2008-30.07.2008', '31.07.2008-13.08.2008', '14.08.2008-27.08.2008', '28.08.2008-10.09.2008', '11.09.2008-24.09.2008', '25.09.2008-08.10.2008', '09.10.2008-22.10.2008', '23.10.2008-05.11.2008', '06.11.2008-19.11.2008', '20.11.2008-03.12.2008', '04.12.2008-17.12.2008', '18.12.2008-31.12.2008' FROM Calendar WHERE EventLocationID='20'

so in recordset should get name of event for every EventLocationID during the year.

Result row variables:


$Event1=$row['03.01.2008-16.01.2008']
$Event2=$row['17.01.2008-30.01.2008']
$Event3=$row['31.01.2008-13.02.2008']

Result row shows OK in SQL editor:


+-----------------------+-----------------------+-----------------------+
+ 03.01.2008-16.01.2008 + 17.01.2008-30.01.2008 + 31.01.2008-13.02.2008 +
+-----------------------+-----------------------+-----------------------+
+ $Evant1 + $Event2 + $Event3 +
+------------------------------

sorry 'bout messy spacing,

hope this helps,

tnx,

X

[edited by: XavierG at 12:09 pm (utc) on Dec. 4, 2008]

[edited by: dreamcatcher at 2:43 pm (utc) on Dec. 4, 2008]
[edit reason] Fixed Side Scroll [/edit]

XavierG

1:07 pm on Dec 4, 2008 (gmt 0)

10+ Year Member



I've found it..

My mistake, I've created another query inside For loop with same $row array name, so it got confized with previous query results.

After change of that array variable name, everything worked fine.

tnx alot.