ERROR: using '$this' when not in object context [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have been coding and improving the code jszobody provided me that can found on this LINK which It makes me to be my refference but it results me to an error Fatal error: Using $this when not in object context
PHP:
<?php
function getAllPossiblePermutations($mdArray, $firstTime=true, $tempArray=array())
{
// initialize results array
if ($firstTime)
{
$this->permutationsResultsArray = array();
}
// find first sub array and iterate through it
$thisArray = array_shift($mdArray);
foreach ($thisArray as $key => $elem)
{
// if this number has already been used skip this possible permutation
if (in_array($elem, $tempArray))
{
continue;
}
$tempArray[] = $elem;
if (count($mdArray) == 0)
{
$this->permutationsResultsArray[] = $tempArray;
}
else
{
$this->getAllPossiblePermutations($mdArray, false, $tempArray);
}
array_pop($tempArray);
}
}
$traits = array
(
array('Happy', 'Sad', 'Angry', 'Hopeful'),
array('Outgoing', 'Introverted'),
array('Tall', 'Short', 'Medium'),
array('Handsome', 'Plain', 'Ugly')
);
print_r(getAllPossiblePermutations($traits));
?>
EXPECTED OUTPUT:
Array ( [0] => HappyOutgoingTallHandsome 1 => HappyOutgoingTallPlain [2] => HappyOutgoingTallUgly [3] => HappyOutgoingShortHandsome [4] => HappyOutgoingShortPlain [5] => HappyOutgoingShortUgly [6] => HappyOutgoingMediumHandsome [7] => HappyOutgoingMediumPlain [8] => HappyOutgoingMediumUgly [9] => HappyIntrovertedTallHandsome [10] => HappyIntrovertedTallPlain [11] => HappyIntrovertedTallUgly [12] => HappyIntrovertedShortHandsome [13] => HappyIntrovertedShortPlain [14] => HappyIntrovertedShortUgly [15] => HappyIntrovertedMediumHandsome [16] => HappyIntrovertedMediumPlain [17] => HappyIntrovertedMediumUgly [18] => SadOutgoingTallHandsome [19] => SadOutgoingTallPlain [20] => SadOutgoingTallUgly [21] => SadOutgoingShortHandsome [22] => SadOutgoingShortPlain [23] => SadOutgoingShortUgly [24] => SadOutgoingMediumHandsome [25] => SadOutgoingMediumPlain [26] => SadOutgoingMediumUgly [27] => SadIntrovertedTallHandsome [28] => SadIntrovertedTallPlain [29] => SadIntrovertedTallUgly [30] => SadIntrovertedShortHandsome [31] => SadIntrovertedShortPlain [32] => SadIntrovertedShortUgly [33] => SadIntrovertedMediumHandsome [34] => SadIntrovertedMediumPlain [35] => SadIntrovertedMediumUgly [36] => AngryOutgoingTallHandsome [37] => AngryOutgoingTallPlain [38] => AngryOutgoingTallUgly [39] => AngryOutgoingShortHandsome [40] => AngryOutgoingShortPlain [41] => AngryOutgoingShortUgly [42] => AngryOutgoingMediumHandsome [43] => AngryOutgoingMediumPlain [44] => AngryOutgoingMediumUgly [45] => AngryIntrovertedTallHandsome [46] => AngryIntrovertedTallPlain [47] => AngryIntrovertedTallUgly [48] => AngryIntrovertedShortHandsome [49] => AngryIntrovertedShortPlain [50] => AngryIntrovertedShortUgly [51] => AngryIntrovertedMediumHandsome [52] => AngryIntrovertedMediumPlain [53] => AngryIntrovertedMediumUgly [54] => HopefulOutgoingTallHandsome [55] => HopefulOutgoingTallPlain [56] => HopefulOutgoingTallUgly [57] => HopefulOutgoingShortHandsome [58] => HopefulOutgoingShortPlain [59] => HopefulOutgoingShortUgly [60] => HopefulOutgoingMediumHandsome [61] => HopefulOutgoingMediumPlain [62] => HopefulOutgoingMediumUgly [63] => HopefulIntrovertedTallHandsome [64] => HopefulIntrovertedTallPlain [65] => HopefulIntrovertedTallUgly [66] => HopefulIntrovertedShortHandsome [67] => HopefulIntrovertedShortPlain [68] => HopefulIntrovertedShortUgly [69] => HopefulIntrovertedMediumHandsome [70] => HopefulIntrovertedMediumPlain [71] => HopefulIntrovertedMediumUgly [72] => )
where did I go wrong?

You have a lot of referemces to class variables, like this:
$this->permutationsResultsArray = array();
And PHP complains since this function is not a method in a class. It will work if you just remove this-> so you get:
$permutationsResultsArray = array();
In addition when you are all done you never really return the result.. Like this:
return $permutationsResultsArray;
There is a problem with it though. You are recusing and you don't create that array except in the first round but your code uses it as if it was defined.
BTW: Your function could be much easier with 3 foreach loops:
function getCombinations($traits)
{
$combinations = array('');
foreach( $traits as $trait_level ) {
$new_combinations = array();
foreach ( $combinations as $comb ) {
foreach ( $trait_level as $trait ){
$new_combinations[] = "$comb $trait";
}
}
$combinations = $new_combinations;
}
return $combinations;
}

A function doesn't have a self referential $this. Actually, removing all your $this-> references and make $tempArray a pass by reference instead of by value, your code should work...
function getAllPossiblePermutations($mdArray, $firstTime=true, &$tempArray=array())

Related

I need to create variations like woo commerce in codeigniter php

I have an array like this ( array generated dynamically from DB)
$attr = array('color'=>array('red','pink','yello','white','black','light-yellow','maroon','neal'),"brand"=>array('nike','adidas','dg','puma','neaon'),"size"=>array(8,10,11,12,13,14,15));
And I need result like
red-nike-8
red-nike-9
red-nike-10
red-nike-11
red-nike-12
red-nike-13
red-nike-14
red-nike-15
red-adidas-8
red-adidas-9
red-adidas-10
red-adidas-11
red-adidas-12
red-adidas-13
red-adidas-14
red-adidas-15
red-dg-8
red-dg-9
red-dg-10
red-dg-11
red-dg-12
red-dg-13
red-dg-14
red-dg-15
red-puma-8
red-puma-9
red-puma-10
red-puma-11
red-puma-12
red-puma-13
red-puma-14
red-puma-15
red-neaon-8
red-neaon-9
red-neaon-10
red-neaon-11
red-neaon-12
red-neaon-13
red-neaon-14
red-neaon-15
pink-nike-8
pink-nike-9
pink-nike-10
pink-nike-11
pink-nike-12
pink-nike-13
pink-nike-14
pink-nike-15
pink-adidas-8
pink-adidas-9
pink-adidas-10
pink-adidas-11
pink-adidas-12
pink-adidas-13
pink-adidas-14
pink-adidas-15........
Below is my code, This work for static but I need to developed to create dynamic structure.
$attr_color = array('red','pink','yello','white','black','light-yellow','maroon','neal');
$attr_brand = array('nike','adidas','dg','puma','neaon');
$attr_size = array(8,10,11,12,13,14,15);
$array = array();
foreach ($attr_color as $key => $value_one)
{
foreach ($attr_brand as $key => $value_two)
{
foreach ($attr_size as $key => $value_three)
{
$array[] = array($value_one,$value_two,$value_three);
}
}
}
As you have mentioned dynamic array in initial, considering that it's a array of depth two containing multiple subarrays, like below code. You can add any other subarray in this, Like I have added others array.
$attr = [
[
'color1',
'color2',
'color3',
],
[
'brand1',
'brand2',
'brand3',
],
[
'size1',
'size2',
'size3',
],
[
'other1',
'other2',
'other3',
],
];
You have to loop like this to achieve the desired result.
function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return [];
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
// get combinations from subsequent arrays
$tmp = combinations($arrays, $i + 1);
$result = [];
// concat each array from tmp with each element from $arrays[$i]
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ?
array_merge([$v], $t) :
[$v, $t];
}
}
$finalArray = [];
foreach($result as $k => $val){
$finalArray[$k] = implode("-",$val);
}
return $finalArray;
}
$result = combinations($attr);
print_r($result);
Final result is listed below.
Array
(
[0] => color1-brand1-size1-other1
[1] => color1-brand1-size1-other2
[2] => color1-brand1-size1-other3
[3] => color1-brand1-size2-other1
[4] => color1-brand1-size2-other2
[5] => color1-brand1-size2-other3
[6] => color1-brand1-size3-other1
[7] => color1-brand1-size3-other2
[8] => color1-brand1-size3-other3
[9] => color1-brand2-size1-other1
[10] => color1-brand2-size1-other2
[11] => color1-brand2-size1-other3
[12] => color1-brand2-size2-other1
[13] => color1-brand2-size2-other2
[14] => color1-brand2-size2-other3
[15] => color1-brand2-size3-other1
[16] => color1-brand2-size3-other2
[17] => color1-brand2-size3-other3
[18] => color1-brand3-size1-other1
[19] => color1-brand3-size1-other2
[20] => color1-brand3-size1-other3
[21] => color1-brand3-size2-other1
[22] => color1-brand3-size2-other2
[23] => color1-brand3-size2-other3
[24] => color1-brand3-size3-other1
[25] => color1-brand3-size3-other2
[26] => color1-brand3-size3-other3
[27] => color2-brand1-size1-other1
[28] => color2-brand1-size1-other2
[29] => color2-brand1-size1-other3
[30] => color2-brand1-size2-other1
[31] => color2-brand1-size2-other2
[32] => color2-brand1-size2-other3
[33] => color2-brand1-size3-other1
[34] => color2-brand1-size3-other2
[35] => color2-brand1-size3-other3
[36] => color2-brand2-size1-other1
[37] => color2-brand2-size1-other2
[38] => color2-brand2-size1-other3
[39] => color2-brand2-size2-other1
[40] => color2-brand2-size2-other2
[41] => color2-brand2-size2-other3
[42] => color2-brand2-size3-other1
[43] => color2-brand2-size3-other2
[44] => color2-brand2-size3-other3
[45] => color2-brand3-size1-other1
[46] => color2-brand3-size1-other2
[47] => color2-brand3-size1-other3
[48] => color2-brand3-size2-other1
[49] => color2-brand3-size2-other2
[50] => color2-brand3-size2-other3
[51] => color2-brand3-size3-other1
[52] => color2-brand3-size3-other2
[53] => color2-brand3-size3-other3
[54] => color3-brand1-size1-other1
[55] => color3-brand1-size1-other2
[56] => color3-brand1-size1-other3
[57] => color3-brand1-size2-other1
[58] => color3-brand1-size2-other2
[59] => color3-brand1-size2-other3
[60] => color3-brand1-size3-other1
[61] => color3-brand1-size3-other2
[62] => color3-brand1-size3-other3
[63] => color3-brand2-size1-other1
[64] => color3-brand2-size1-other2
[65] => color3-brand2-size1-other3
[66] => color3-brand2-size2-other1
[67] => color3-brand2-size2-other2
[68] => color3-brand2-size2-other3
[69] => color3-brand2-size3-other1
[70] => color3-brand2-size3-other2
[71] => color3-brand2-size3-other3
[72] => color3-brand3-size1-other1
[73] => color3-brand3-size1-other2
[74] => color3-brand3-size1-other3
[75] => color3-brand3-size2-other1
[76] => color3-brand3-size2-other2
[77] => color3-brand3-size2-other3
[78] => color3-brand3-size3-other1
[79] => color3-brand3-size3-other2
[80] => color3-brand3-size3-other3
)
Thanks

PHP Array to XML

I have created a multi-dimensional array using fgetcsv from a CSV file.
Using both DOMDocument and SimpleXML I am trying to create a XML file of the CSV document.
The array and XML variables are being passed to a function within the same class file. The XML document is being created without any issues, but no value is passing from the array into the XML. It does work it I use a static value opposed to passing a value from the array, also if I print_r the array the structure and values are all correct.
I have tried 'htmlspecialcharacters' and 'encode_UTF8' before passing the value into the XML.
An example of the code is below, product is the multi-dimensional array.
public function array_to_xml($product, &$xml)
{
foreach($product as $row)
{
$element = $xml->createElement("Product");
$xml->appendChild($element);
$element = $xml->createElement("ID", ($row[38]));
$xml->appendChild($element);
}
}
The problem is obviously with the array but I can't find the answer. Any help would be gratefully appreciated.
The output currently looks like (with not value in the ID element). Once it is working Product will have about 20 child elements.
<?xml version="1.0"?>
<ProductList/>
<Product>
<ID/>
</Product>
</ProductList>
Example of $row when printed to screen:
Array ( [0] => [1] => [2] => 6/10/2016 [3] => [4] => [5] => 7.35 [6] => N [7] => N [8] => N [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 80 [15] => 0 [16] => 80 [17] => 0 [18] => 80 [19] => N [20] => N [21] => N [22] => N [23] => 236.50 [24] => 0.00 [25] => 4.86 [26] => AFG Home Loans - Alpha [27] => 100% Offset Lo Doc Fixed [28] => 100% Offset Lo Doc 4 Year Fixed Owner Occupied [29] => 250.00 [30] => [31] => 7.35 [32] => 0.00 [33] => 4.9 [34] => N [35] => 325.00 [36] => 48 [37] => 4.52 [38] => 1-1MX78TF [39] => N [40] => [41] => [42] => N [43] => N [44] => [45] => Y [46] => 0.00 [47] => 10,000.00 [48] => 2,000,000.00 [49] => Y [50] => 30 [51] => [52] => [53] => Y [54] => 0.00 )
A couple things stand out. First, you have a syntax error on this line:
$element = $xml->createElement("ID", ($row[38])); (note the errant parentheses around $row[38]. The createElement method takes a String for its second parameter.
Second, you're not adding the ID to the product, but to the root XML. Fixing that, your code should look closer to this.
public function array_to_xml($product, &$xml)
{
foreach ($product as $row)
{
$product= $xml->createElement("Product");
$id = $xml->createElement("ID", $row[38]);
$product->appendChild($id);
$xml->appendChild($product);
}
}
If you need it as an attribute as #Barmar commented, you'd use the DOMElement->setAttribute() method, and it would look like:
public function array_to_xml($product, &$xml)
{
foreach ($product as $row)
{
$product= $xml->createElement("Product");
$product->setAttribute('ID', $row[38]);
$xml->appendChild($product);
}
}

php multidimensional array path segment combination loop

I am trying to figure a way to get this to work. But I have a hard time thinking out the logics.
I have this array:
Array
(
[0] => Array
(
[0] => news
[1] => {section}
[2] => {slug}
[3] => {*}
)
[1] => Array
(
[0] => {id}
[1] => {*}
)
[2] => Array
(
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
)
That I need to convert to this result:
0 news/{id}/{date}
1 news/{id}/25-07-1982
2 news/{id}/{section}
3 news/{id}/{slug}
4 news/{id}/{*}
5 news/{*}/{date}
6 news/{*}/25-07-1982
7 news/{*}/{section}
8 news/{*}/{slug}
9 news/{*}/{*}
10 {section}/{id}/{date}
11 {section}/{id}/25-07-1982
12 {section}/{id}/{section}
13 {section}/{id}/{slug}
14 {section}/{id}/{*}
15 {section}/{*}/{date}
16 {section}/{*}/25-07-1982
17 {section}/{*}/{section}
18 {section}/{*}/{slug}
19 {section}/{*}/{*}
20 {slug}/{id}/{date}
21 {slug}/{id}/25-07-1982
22 {slug}/{id}/{section}
23 {slug}/{id}/{slug}
24 {slug}/{id}/{*}
25 {slug}/{*}/{date}
26 {slug}/{*}/25-07-1982
27 {slug}/{*}/{section}
28 {slug}/{*}/{slug}
29 {slug}/{*}/{*}
30 {*}/{id}/{date}
31 {*}/{id}/25-07-1982
32 {*}/{id}/{section}
33 {*}/{id}/{slug}
34 {*}/{id}/{*}
35 {*}/{*}/{date}
36 {*}/{*}/25-07-1982
37 {*}/{*}/{section}
38 {*}/{*}/{slug}
39 {*}/{*}/{*}
The input array could contain more than three keys, so the solution I'm looking for should be dynamic. And the result should have the same order as the result shown above.
Does someone know how to do this in a efficient way? Can someone give me a push in the right direction? Thanks a lot! :)
Sth like this
foreach ($array[0] as $val0 )
foreach ($array[1] as $val1 )
foreach ($array[2] as $val2 )
$newArray[] = "$val0/$val1/$val2";
EDIT: for variable array length
function recursive($array , $length = 0){
$retval =array();
if($length < count($array) -1){
foreach ($array[$length] as $val0 )
foreach (recursive($array, $length+1) as $val1)
$retval[] = "$val0/$val1";
}
else
{
foreach ($array[$length] as $val0 )
$retval[] = "$val0";
}
return $retval;
}
print_r(recursive($array));
Just because I like writing functions that mis/manage PHP arrays, I put this together, mainly because I was pretty sure you could avoid recursion — because the structure itself isn't recursive. (My head seems to think that is a rule, I'm sure someone somewhere can prove it wrong).
foreach ( array_reverse($array) as $sub ) {
if ( isset($rem) ) {
$ret = array();
foreach ( $sub as $itm ) {
foreach ( $rem as $val ) { $ret[] = "$itm/$val"; }
}
$rem = $ret;
}
else {
$rem = $sub;
}
}
The output found in $rem is as follows:
Array (
[0] => news/{id}/{date}
[1] => news/{id}/25-07-1982
[2] => news/{id}/{section}
[3] => news/{id}/{slug}
[4] => news/{id}/{*}
[5] => news/{*}/{date}
[6] => news/{*}/25-07-1982
[7] => news/{*}/{section}
[8] => news/{*}/{slug}
[9] => news/{*}/{*}
[10] => {section}/{id}/{date}
[11] => {section}/{id}/25-07-1982
[12] => {section}/{id}/{section}
[13] => {section}/{id}/{slug}
[14] => {section}/{id}/{*}
[15] => {section}/{*}/{date}
[16] => {section}/{*}/25-07-1982
[17] => {section}/{*}/{section}
[18] => {section}/{*}/{slug}
[19] => {section}/{*}/{*}
[20] => {slug}/{id}/{date}
[21] => {slug}/{id}/25-07-1982
[22] => {slug}/{id}/{section}
[23] => {slug}/{id}/{slug}
[24] => {slug}/{id}/{*}
[25] => {slug}/{*}/{date}
[26] => {slug}/{*}/25-07-1982
[27] => {slug}/{*}/{section}
[28] => {slug}/{*}/{slug}
[29] => {slug}/{*}/{*}
[30] => {*}/{id}/{date}
[31] => {*}/{id}/25-07-1982
[32] => {*}/{id}/{section}
[33] => {*}/{id}/{slug}
[34] => {*}/{id}/{*}
[35] => {*}/{*}/{date}
[36] => {*}/{*}/25-07-1982
[37] => {*}/{*}/{section}
[38] => {*}/{*}/{slug}
[39] => {*}/{*}/{*}
)
Also, for those that like their arrays multidimensional, this might come in handy (although I'd hate to think what the overheads are for such a code golfed version). Just to be clear, this second example doesn't create the string list as requested by the OP, but a hierarchical array structure instead.
foreach ( array_reverse($array) as $sub ) {
$rem = isset($rem)
? array_combine($sub, array_fill(0, count($sub), $rem))
: $sub
;
}
This generates (again in $rem):
Array (
[news] => Array (
[{id}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
[{*}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
)
[{section}] => Array (
[{id}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
... and so on
Now if only PHP had a join_recursive that included keys.
(it would be almost pointless, save for helping with the above).

Replace value in multi dimension array

I have array format like:
Array
(
[Australia] => Array
(
[0] => [1990,0.01],
[1] => [1991,0.02],
[2] => [1992,0.02],
[3] => [1993,0.02],
[4] => [1994,0.02],
[5] => [1995,0.02],
[6] => [1996,0.02],
[7] => [1997,0.02],
[8] => [1998,0.02],
[9] => [1999,0.02],
[10] => [2000,0.02],
[11] => [2001,0.02],
[12] => [2002,0.02],
[13] => [2003,0.02],
[14] => [2004,0.02],
[15] => [2005,0.02],
[16] => [2006,0.02],
[17] => [2007,0.02],
[18] => [2008,0.02],
[19] => [2009,empty],
[20] => [2010,empty],
[21] => [2011,empty],
[22] => [2012,empty],
[23] => [2013,empty],
[24] => [2014,empty],
[25] => [2015,empty]
)
[Pakistan] => Array
(
[0] => [1990,0.00],
[1] => [1991,0.00],
[2] => [1992,0.00],
[3] => [1993,0.00],
[4] => [1994,0.00],
[5] => [1995,0.00],
[6] => [1996,0.00],
[7] => [1997,0.00],
[8] => [1998,0.00],
[9] => [1999,0.00],
[10] => [2000,0.00],
[11] => [2001,0.00],
[12] => [2002,0.00],
[13] => [2003,0.00],
[14] => [2004,0.01],
[15] => [2005,0.01],
[16] => [2006,0.00],
[17] => [2007,0.00],
[18] => [2008,0.00],
[19] => [2009,empty],
[20] => [2010,empty],
[21] => [2011,empty],
[22] => [2012,empty],
[23] => [2013,empty],
[24] => [2014,empty],
[25] => [2015,empty]
)
)
and i want to replace 'empty' with 0 without change the array structure and elements position. I stuck how to do..
You can use array_walk_recursive function:
function replace_empty(&$item, $key) {
$item = str_replace('empty', '0', $item);
}
array_walk_recursive($your_array, 'replace_empty');
You could use the array_walk_recursive function, with a callback function that would replace empty by 0.
For example, considering your array is declared this way :
$myArray[0] = array(23, empty, 43, 12);
$myArray[1] = array(empty, empty, 53, 19);
Note : I supposed you made a typo, and your arrays are not containing only a string, but several sub-elements.
You could use this kind of code :
array_walk_recursive($myArray, 'replacer');
var_dump($myArray);
With the following callback functon :
function replacer(& $item, $key) {
if ($item === empty) {
$item = 0;
}
}
Note that :
the first parameter is passed by reference !
which means modifying it will modify the corresponding value in your array
I'm using the === operator for the comparison
And you'd get the following output :
array(
0 =>
array
0 => int 23
1 => int 0
2 => int 43
3 => int 12
1 =>
array
0 => int 0
1 => int 0
2 => int 53
3 => int 19)
I would foreach in both indices (not tested):
foreach($array as $country){
foreach($country as &$field){
if($field[1] == 'empty'){
$field[1] = 0;
}
}
}
(I assume empty is a string)
EDIT:
If this [1990,0.00] is not an array but a string, you could use str_replace instead
foreach($array as $country){
foreach($country as &$field){
$field = str_replace('empty', '0.00', $field);
}
}
}

Getting this PHP error: fputcsv() expects parameter 2 to be array

I am trying to insert an array into an array of arrays using array_splice.
array_splice($array, 0, 0, $fieldNames);
The resulting array is then converted to csv format with fputcsv. I get the following error:
fputcsv() expects parameter 2 to be array string given in...
What am I doing wrong? Must be some minor bug. Please any help. Also am I taking the right approach. My PHP knowledge of functions is not huge.
Full php code:
<?php
//MySQL login details
require('sqlAuth.php');
//return error data
function errorReport($error)
{
$data = array("error" => $error);
$json = json_encode($data);
echo $json;
die();
}
//export array to csv file
function outputCSV($data) {
$outstream = fopen("php://output", "w");
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals); // add parameters if you want
}
array_walk($data, "__outputCSV", $outstream);
fclose($outstream);
}
// open connection
$connection = mysql_connect($host, $user, $pass) or errorReport("Unable to Connect");
// select database
mysql_select_db($db, $connection) or errorReport("Unable to Select Database: " .mysql_error());
//build result set array
$array = array();
//get full questions table
$query = "SELECT q.q_id, q.trip_day, q.q0_1, q.q0_2, q.q0_3, q.q0_4, q.q0_5, q.q0_6, g.address, g.latitude, g.longitude, g.method, q.q1_1, q.q1_2, q.q1_3, q.q1_7, q.q1_9, q.q1_10, q.q1_11_1, q.q1_11_2, q.q1_11_3, q.q1_11_4, q.q2_6, q.q6_0,
q.q6_1, q.q6_1_1_1, q.q6_1_1_2, q.q6_1_1_3, q.q6_1_1_4, q.q6_1_1_5, q.q6_1_1_6, q.q6_1_1_7, q.q6_1_1_8, q.q6_1_1_9, q.q6_1_1_10, q.q6_1_1_11, q.q6_1_1_12, q.q6_1_1_13, q.q6_1_1_14,
q.q6_1_1_15, q.q6_1_1_16, q.q6_1_2_1, q.q6_1_2_2, q.q6_1_2_3, q.q6_1_2_4, q.q6_1_2_5, q.q6_1_2_6, q.q6_1_2_7, q.q6_1_2_8, q.q6_1_2_9, q.q6_1_2_10, q.q6_1_2_11, q.q6_1_2_12, q.q6_1_2_13,
q.q6_1_2_14, q.q6_1_2_15, q.q6_1_2_16, q.q6_1_3_1, q.q6_1_3_2, q.q6_2, q.q6_2_1_1, q.q6_2_1_2, q.q6_2_1_3, q.q6_2_1_4, q.q6_2_1_5, q.q6_2_1_6, q.q6_2_1_7, q.q6_2_1_8, q.q6_2_1_9,
q.q6_2_1_10, q.q6_2_1_11, q.q6_2_1_12, q.q6_2_1_13, q.q6_2_1_14, q.q6_2_1_15, q.q6_2_1_16, q.q6_2_2_1, q.q6_2_2_2, q.q6_2_2_3, q.q6_2_2_4, q.q6_2_2_5, q.q6_2_2_6, q.q6_2_2_7,
q.q6_2_2_8, q.q6_2_2_9, q.q6_2_2_10, q.q6_2_2_11, q.q6_2_2_12, q.q6_2_2_13, q.q6_2_2_14, q.q6_2_2_15, q.q6_2_2_16, q.q6_2_3_1, q.q6_2_3_2, q.q6_3, q.q6_3_1_1, q.q6_3_1_2,
q.q6_3_1_3, q.q6_3_1_4, q.q6_3_1_5, q.q6_3_1_6, q.q6_3_1_7, q.q6_3_1_8, q.q6_3_1_9, q.q6_3_1_10, q.q6_3_1_11, q.q6_3_1_12, q.q6_3_1_13, q.q6_3_1_14, q.q6_3_2_1, q.q6_3_2_2, q.q6_3_2_3,
q.q6_3_2_4, q.q6_3_2_5, q.q6_3_2_6, q.q6_3_2_7, q.q6_3_2_8, q.q6_3_2_9, q.q6_3_2_10, q.q6_3_2_11, q.q6_3_2_12, q.q6_3_2_13, q.q6_3_2_14, q.q6_3_3_1, q.q6_3_3_2
FROM questions AS q LEFT JOIN gmap_address_list AS g ON q.q0_7 = g.id";
$result_questions = mysql_query($query) or errorReport("Error in query: $query. ".mysql_error());
while ($row = mysql_fetch_array($result_questions, MYSQL_ASSOC)) {
$array[] = $row;
}
//get field names
$fieldNames = array();
$fieldNum = mysql_num_fields($result_questions);
for ($i = 0; $i < $fieldNum; $i++) {
$fieldNames[] = mysql_field_name($result_questions, $i);
}
array_splice($array, 0, 0, $fieldNames);
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=emmhts_questionnaires.csv");
header("Pragma: no-cache");
header("Expires: 0");
outputCSV($array);
// free result set memory
mysql_free_result($result_questions);
//close connection to mysql db
mysql_close($connection);
?>
Edit:
Here is a section of the array for what should be the first row of the csv file after adding print_r($array) just before outputCSV($array). It looks like the array inserted in position 0 is not closed after "[124] => q6_3_3_2", but rather there is a sub array.
Array
(
[0] => q_id
[1] => trip_day
[2] => q0_1
[3] => q0_2
[4] => q0_3
[5] => q0_4
[6] => q0_5
[7] => q0_6
[8] => address
[9] => latitude
[10] => longitude
[11] => method
[12] => q1_1
[13] => q1_2
[14] => q1_3
[15] => q1_7
[16] => q1_9
[17] => q1_10
[18] => q1_11_1
[19] => q1_11_2
[20] => q1_11_3
[21] => q1_11_4
[22] => q2_6
[23] => q6_0
[24] => q6_1
[25] => q6_1_1_1
[26] => q6_1_1_2
[27] => q6_1_1_3
[28] => q6_1_1_4
[29] => q6_1_1_5
[30] => q6_1_1_6
[31] => q6_1_1_7
[32] => q6_1_1_8
[33] => q6_1_1_9
[34] => q6_1_1_10
[35] => q6_1_1_11
[36] => q6_1_1_12
[37] => q6_1_1_13
[38] => q6_1_1_14
[39] => q6_1_1_15
[40] => q6_1_1_16
[41] => q6_1_2_1
[42] => q6_1_2_2
[43] => q6_1_2_3
[44] => q6_1_2_4
[45] => q6_1_2_5
[46] => q6_1_2_6
[47] => q6_1_2_7
[48] => q6_1_2_8
[49] => q6_1_2_9
[50] => q6_1_2_10
[51] => q6_1_2_11
[52] => q6_1_2_12
[53] => q6_1_2_13
[54] => q6_1_2_14
[55] => q6_1_2_15
[56] => q6_1_2_16
[57] => q6_1_3_1
[58] => q6_1_3_2
[59] => q6_2
[60] => q6_2_1_1
[61] => q6_2_1_2
[62] => q6_2_1_3
[63] => q6_2_1_4
[64] => q6_2_1_5
[65] => q6_2_1_6
[66] => q6_2_1_7
[67] => q6_2_1_8
[68] => q6_2_1_9
[69] => q6_2_1_10
[70] => q6_2_1_11
[71] => q6_2_1_12
[72] => q6_2_1_13
[73] => q6_2_1_14
[74] => q6_2_1_15
[75] => q6_2_1_16
[76] => q6_2_2_1
[77] => q6_2_2_2
[78] => q6_2_2_3
[79] => q6_2_2_4
[80] => q6_2_2_5
[81] => q6_2_2_6
[82] => q6_2_2_7
[83] => q6_2_2_8
[84] => q6_2_2_9
[85] => q6_2_2_10
[86] => q6_2_2_11
[87] => q6_2_2_12
[88] => q6_2_2_13
[89] => q6_2_2_14
[90] => q6_2_2_15
[91] => q6_2_2_16
[92] => q6_2_3_1
[93] => q6_2_3_2
[94] => q6_3
[95] => q6_3_1_1
[96] => q6_3_1_2
[97] => q6_3_1_3
[98] => q6_3_1_4
[99] => q6_3_1_5
[100] => q6_3_1_6
[101] => q6_3_1_7
[102] => q6_3_1_8
[103] => q6_3_1_9
[104] => q6_3_1_10
[105] => q6_3_1_11
[106] => q6_3_1_12
[107] => q6_3_1_13
[108] => q6_3_1_14
[109] => q6_3_2_1
[110] => q6_3_2_2
[111] => q6_3_2_3
[112] => q6_3_2_4
[113] => q6_3_2_5
[114] => q6_3_2_6
[115] => q6_3_2_7
[116] => q6_3_2_8
[117] => q6_3_2_9
[118] => q6_3_2_10
[119] => q6_3_2_11
[120] => q6_3_2_12
[121] => q6_3_2_13
[122] => q6_3_2_14
[123] => q6_3_3_1
[124] => q6_3_3_2
[125] => Array
(
[q_id] => 29
[trip_day] => Thursday
[q0_1] => 4
[q0_2] => 5
[q0_3] => 5
[q0_4] => 5
[q0_5] => 5
[q0_6] => 0732152589
Thanks for posting your array data. The problem is your array is multidimensional. I.e. you have a 'subarray' at 125.
Looking at the docs (http://php.net/manual/en/function.fputcsv.php) it isn't clear, but you can't use multidimensional arrays with fputcsv. If you think about it, the array you pass will be converted to one line in the csv.
You will need to think about the structure of your data, and how you expect it to be formatted in your csv, and modify your code accordingly.

Categories