0

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
            )
    )

Darius Pum
  • 29
  • 5
  • it looks like the print_r inside your function is to debug, right? If you want to use the data which is being send/created just return an array or true/false. Within your code you could do something like this: `$result = $this->target_query()`. Within target_query you return for example `[1=>1, 2=>2]'. $result will contain these returned results. Is this what you need? If thats the case ill write an example for you. – Ronnie Oosting Mar 28 '18 at 13:18
  • I am sorry, i hope you can figure this out with the answer which is provided in another question. It seems your question is closed. I was about to write an answer for you with explanation. – Ronnie Oosting Mar 28 '18 at 13:25
  • first of all Thanks for your help. anyway your explanation and example is needed to finish the project. so your help is very appreciated. – Darius Pum Mar 28 '18 at 14:42
  • the explanation given in the topic that marked this one we are speaking as duplicate, is fairly distant from this situation explained here so please give your example in this context beacause im unable to customize the code from the other topic – Darius Pum Mar 28 '18 at 14:44
  • You need to `return` the value from `target_query` and assign it to a variable in `fputToFile`. Just `$this->target_query()` isn't going to magically instantiate `$arraytoclass` inside your function. You need to assign it: `$arraytoclass = $this->target_query()`; the value `return`ed from `target_query` will be assigned to `$arraytoclass`. – deceze Mar 28 '18 at 15:13
  • I was making an error, assigning `$this->target_query();` to a different variable instead of `$arraytoclass ` so the back end of Prestashop gaved me error. I've inserted the code `$arraytoclass = $this->target_query();` then `print_r $arraytoclass;` and now i see the array populated, can you please now guide me how to put this results inside $readyForExport['codice_target'] ? thanks for your explanetion and patience. – Darius Pum Mar 28 '18 at 16:09

0 Answers0