Hi once again I've got my class with several functions inside. One of these functions is making a query from the database and store data inside an array. I called this function who makes the mysql query inside another function to pass the resulted array inside itself and manipulate. I need to catch the result array from the first function and put it into another array inside the function that call the first one. Hope to have explained correctly. In my first function i launched return $arraytoclass
but print_r
inside the second function does not print anything. Many thanks Im going to post my code below. ____ EDIT --- I modified the code following the suggestions from our friens below.
<?php
class AdvancedExport extends Module
{
public function target_query(){
include_once "connection.php";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT codice_target FROM customer";
$result = $conn->query($sql);
$arraytoclass = array();
if ($result->num_rows > 0) {
// output data of each row
//echo "tutto ok";
while($row = $result->fetch_row()) {
//echo "Codice target: " . $row["codice_target"]."<br>";
$arraytoclass[] = $row;
//echo "codice target:".$arraytoclass[$i]['codice_target'];
}
//print_r($arraytoclass);
return $arraytoclass;
//print_r($arraytoclass);
} else {
echo "NO results";
}
$conn->close();
}
public function fputToFile($file, $allexportfields, $object, $ae)
{
if($allexportfields && $file && $object && $ae)
{
//one ready for export product
$readyForExport = array();
//put in correct sort order
foreach ($allexportfields as $value)
{
$object = $this->processDecimalSettings($object, $ae, $value);
$readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);
}
$arraytoclass = $this->target_query();
print_r ($arraytoclass);
$readyForExport['codice_target'] = $arraytoclass[];
//scambio l'id customer con il codice_target
//$readyForExport['codice_target'] = 8989;
$textTarget = (string)$readyForExport['codice_target'];
$readyForExport['id_customer'] = $textTarget;
// LOTS OF CODE HERE ...
//write into csv line by line
fputcsv($file, $readyForExport, $ae->delimiter, $ae->separator);
}
}
}
?>
print_r ($arraytoclass);
after $arraytoclass = $this->target_query();
gives me this result:
Array ( [0] => Array ( [0] => 0 ) [1] => Array ( [0] => 898989 ) [2] => Array ( [0] => 0 ) [3] => Array ( [0] => 0 ) [4] => Array ( [0] => 0 ) [5] => Array ( [0] => 0 ) [6] => Array ( [0] => 0 ) [7] => Array ( [0] => 0 ) [8] => Array ( [0] => 0 ) [9] => Array ( [0] => 0 ) [10] => Array ( [0] => 0 ) [11] => Array ( [0] => 0 ) [12] => Array ( [0] => 0 ) [13] => Array ( [0] => 0 ) [14] => Array ( [0] => 0 ) [15] => Array ( [0] => 0 ) [16] => Array ( [0] => 0 ) [17] => Array ( [0] => 0 ) [18] => Array ( [0] => 0 ) [19] => Array ( [0] => 0 ) [20] => Array ( [0] => 0 ) )