Page 1 of 1
Use vars in Array for COM?
Posted: Wed Apr 21, 2004 5:54 pm
by rxsid
Hi all,
I'm extracting data from a MySQL db, and populating the info into a MS Excel spreadsheet.
Everything is working just fine, with the exception of trying to put the rows returned from the db into an Array (there are more than 5 rows returned...just simplified here) and then spinning through that array and populating different cells with that variabls in the array. Can I store $varibles in an Array() as opposed to 'strings' ?
Code: Select all
...//other data and strings inserted into other Cells above here just fine, then:
$typeData = Array ($var1,
$something2,
$anothername3,
$word4,
$input5);
$xRow=5;$yRow=6; //Starting cell.
for ($y=0;$y<=4;$y++) {
$cell = $sheets->Cells($yRow,$xRow);
$cell->activate;
$cell->value = $typeData[$y]; //**I THINK this is where it's crashing....
unset($cell);
if ($xRow == {
$xRow = 5;
$yRow ++;
} elseif ($xRow == 6 ) {
//$xRow = ($xRow +2);
$xRow = 8;
} else {
$xRow++;
}
}
Any ideas?
Thanks!
Posted: Wed Apr 21, 2004 6:05 pm
by feyd
yeah, you can store varibles in strings..
does $sheets->Cells() return a reference?
Posted: Wed Apr 21, 2004 6:14 pm
by rxsid
thanks for the reply.
yes...it does return, I'm using something similar in code above this with:
$wArray[] is populated during the spin through each record returned in a query.
Code: Select all
//Now spin through each in the array
for ($wCnt=1;$wCnt<=14;$wCnt++) {
$cell = $sheets->Cells($xRow,$yRow); //Select the cell
$cell->activate; //Activate the cell
$cell->value = $wArray[$wCnt]; //print value to cell
unset($cell);
//unset the cell var to create anew.
if ($yRow == 18) {
$yRow++;
} else {
$yRow --;
$xRow ++;
}
}
the code above works excellent. but the original code posted doesnt. I think because the $typeData array has $vars in it...whereas the $wArray array has strings of data from a query result.
How would I stored the variables into strings into the array?
Posted: Wed Apr 21, 2004 6:19 pm
by feyd
"$var"
Posted: Wed Apr 21, 2004 6:21 pm
by rxsid
I can also do individual lines/cells just fine:
Code: Select all
$cell = $sheets->Cells(5,2) ;
$cell->activate;
$cell->value = $var1;
unset($cell);
referencing the variable directly works...but if i put all the variables in an array...there are over 20 different variables, then try to reference them in the for loop...it hangs the program.
I could do the above over 20 plus times...I just wanted to clean it up a bit with far less code by using the array and for loop.
Posted: Wed Apr 21, 2004 6:22 pm
by rxsid
neither
Code: Select all
$typeData = Array ("$var1",
"$something2",
"$anothername3",
"$word4",
"$input5");
$xRow=5;$yRow=6; //Starting cell.
for ($y=0;$y<=4;$y++) {
$cell = $sheets->Cells($yRow,$xRow);
$cell->activate;
$cell->value = $typeData[$y]; //**I THINK this is where it's crashing....
unset($cell);
if ($xRow == 8 ) {
$xRow = 5;
$yRow ++;
} elseif ($xRow == 6 ) {
//$xRow = ($xRow +2);
$xRow = 8;
} else {
$xRow++;
}
}
nor
Code: Select all
$typeData = Array ($var1,
$something2,
$anothername3,
$word4,
$input5);
$xRow=5;$yRow=6; //Starting cell.
for ($y=0;$y<=4;$y++) {
$cell = $sheets->Cells($yRow,$xRow);
$cell->activate;
$cell->value = "$typeData[$y]"; //**I THINK this is where it's crashing....
unset($cell);
if ($xRow == {
$xRow = 5;
$yRow ++;
} elseif ($xRow == 6 ) {
//$xRow = ($xRow +2);
$xRow = 8;
} else {
$xRow++;
}
}
works.
Posted: Wed Apr 21, 2004 6:30 pm
by feyd
you say that Cells returns.. but does it return a reference to the cell, or the value of the cell?
Posted: Wed Apr 21, 2004 6:38 pm
by rxsid
a reference to the cell
that's why this works just fine:
Code: Select all
$cell = $sheets->Cells(5,2) ; //$cell is assinged to the cell at (5,2)
$cell->activate; //(5,2) cell is activated.
$cell->value = $var1; //(5,2) cell is written with value from $var1
unset($cell); //clear cell object
Posted: Wed Apr 21, 2004 6:38 pm
by feyd
just making sure..

Posted: Wed Apr 21, 2004 11:25 pm
by rxsid
ok...so any other suggestions?
Posted: Wed Apr 21, 2004 11:31 pm
by feyd
not from me.
Posted: Thu Apr 22, 2004 6:22 am
by magicrobotmonkey
try theese and see if any work
Code: Select all
<?php
echo $typeData['$y'];
echo $typeData["$y"];
echo $typeData[$y];
//just to see if that much is working!
?>