Mysql
 sql >> база данни >  >> RDS >> Mysql

Поставете резултата от Mysql заявка в масив в клас

Стойности на масива, инициализирани на $rtoclass променлива извън класа, след което се предава на AdvancedExport обект чрез неговия метод - $obj->set_arr_needed( $rtoclass );

На $obj->fputToFile() извикване, необходимият масив автоматично ще бъде наличен за използване вътре в него чрез променливата $arr_needed_in .

Опитайте:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";


// 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);
$rtoclass = array();

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id_customer"]. " - Codice target: " . $row["codice_target"]."<br>";
        $rtoclass[] = $row;
    }
} else {
    echo "0 results";
}

$obj = new AdvancedExport();
$obj->set_arr_needed( $rtoclass );

$conn->close();

За класа:

class AdvancedExport extends Module
{

        //other methods here...

        private $arr_needed = array(); 

        public function set_arr_needed( $arr ) {    
            $this->arr_needed = $arr;
        }

        public function get_arr_needed() {  
            return $this->arr_needed;
        }

        public function fputToFile($file, $allexportfields, $object, $ae)
        {
            $arr_needed_in = $this->get_arr_needed(); // array needed already inside..

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

                }

                // need to put mysql query result here inside $readyForExport['codice_target'];

                $this->counter[$readyForExport['id_order']] = (!empty($this->counter[$readyForExport['id_order']])) ? ++$this->counter[$readyForExport['id_order']] : 1; // try here
                $readyForExport['orderLine'] = $this->counter[$readyForExport['id_order']]; // and try here

                //print_r('The id_order is added: ' . $readyForExport['orderLine']); // see if it is added

                //echo '<pre>' . var_dump($readyForExport) . '</pre>';

                // modifiche === Dario === prezzo
                $newPrice = substr($readyForExport['product_price'], 0, strpos($readyForExport['product_price'], "."));
                $readyForExport['product_price'] = $newPrice;

                // === data

                $newDateAdd = new DateTime($readyForExport['date_add']);
                $readyForExport['date_add'] = $newDateAdd->format('d/m/Y');


                // aggiungo 21 giorni - 3 settimane - alla data di acquisto
                $date_mod = clone $newDateAdd;
                $date_mod->add(new DateInterval('P21D'));
                $readyForExport['delivery_date'] = $date_mod->format('d/m/Y');

                // === data invoice
                $newDateInvoice = clone $newDateAdd;
                $readyForExport['invoice_date'] = $newDateAdd->format('d/m/Y');

                //scambio l'id customer con il codice_target

                //$readyForExport['codice_target'] = 8989;

                $textTarget = (string)$readyForExport['codice_target']; 

                $readyForExport['id_customer'] = $textTarget;

                // aggiungo gli zeri davanti al customer id
                $id_count = strlen($readyForExport['id_customer']);            
                if ($id_count == 1) {

                    $newCustomer = "0000000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 2) {

                    $newCustomer = "000000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 3) {

                    $newCustomer = "00000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 4) {
                    $newCustomer = "0000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 5) {
                    $newCustomer = "000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 6) {
                    $newCustomer = "00".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }

                // elaboro lo SKU

                $textSku = (string)$readyForExport['product_name']; 

                $newSku_1 = $readyForExport['product_name'];

                $newSku_1 = substr($newSku_1,0,4);
                $newSku_2 = "/".substr($textSku,-4,4);

                $newSku_tot = $newSku_1.$newSku_2; 

                $newSku_tot = str_replace(' ', '', $newSku_tot);
                $newSku_tot = str_replace('-', '', $newSku_tot);
                $newSku_tot = str_replace('co', '', $newSku_tot);

                $newSku_tot = str_replace('e', '', $newSku_tot);
                $newSku_tot = str_replace('r', '', $newSku_tot);

                $readyForExport['product_name'] = $newSku_tot;



                // aggiungo un campo fisso
                $readyForExport['causale'] = "NR";

                // aggiungo un campo fisso
                $readyForExport['ORCL'] = "ORCL";

                //$readyForExport['G'] = "";
                $readyForExport['J'] = "";
                $readyForExport['K'] = "";
                $readyForExport['L'] = "";
                $readyForExport['M'] = "";
                $readyForExport['N'] = "";
                $readyForExport['P'] = "";
                $readyForExport['Q'] = "";
                $readyForExport['R'] = "30";

                $index_arr=array("id_customer","date_add","ORCL","product_name","causale","product_quantity","product_price","delivery_date","id_order","J","K","L","M","N","orderLine","P","Q","R");

                //riordino i campi in base a come li dispongo nella variabile $index_arr
                $arr_t=array();
                foreach($index_arr as $i=>$v) {
                    foreach($readyForExport as $k=>$b) {
                        if ($k==$v) $arr_t[$k]=$b;
                    }
                }
                $readyForExport=$arr_t;

                //write into csv line by line
                fputcsv($file, $readyForExport, $ae->delimiter, $ae->separator);
            }
        }
}
'; // modifiche ===Дарио ===цена $newPrice =substr($readyForExport['product_price'], 0, strpos($readyForExport['product_price'], ".")); $readyForExport['product_price'] =$newPrice; // ===данни $newDateAdd =new DateTime($readyForExport['date_add']); $readyForExport['date_add'] =$newDateAdd->format('d/m/Y'); // добавяне на 21 дни - 3 часа - всички данни от acquisto $date_mod =клонинг $newDateAdd; $date_mod->add(new DateInterval('P21D')); $readyForExport['delivery_date'] =$date_mod->format('d/m/Y'); // ===фактура за данни $newDateInvoice =клонинг $newDateAdd; $readyForExport['invoice_date'] =$newDateAdd->format('d/m/Y'); //scambio l'id client con il codice_target //$readyForExport['codice_target'] =8989; $textTarget =(низ)$readyForExport['codice_target']; $readyForExport['id_customer'] =$textTarget; // добавям идентификатора на клиента $id_count =strlen($readyForExport['id_customer']); if ($id_count ==1) { $newCustomer ="0000000".$readyForExport['id_customer']; $readyForExport['id_customer'] =$newCustomer; }elseif ($id_count ==2) { $newCustomer ="000000".$readyForExport['id_customer']; $readyForExport['id_customer'] =$newCustomer; }elseif ($id_count ==3) { $newCustomer ="00000".$readyForExport['id_customer']; $readyForExport['id_customer'] =$newCustomer; }elseif ($id_count ==4) { $newCustomer ="0000".$readyForExport['id_customer']; $readyForExport['id_customer'] =$newCustomer; }elseif ($id_count ==5) { $newCustomer ="000".$readyForExport['id_customer']; $readyForExport['id_customer'] =$newCustomer; }elseif ($id_count ==6) { $newCustomer ="00".$readyForExport['id_customer']; $readyForExport['id_customer'] =$newCustomer; } // elaboro lo SKU $textSku =(string)$readyForExport['product_name']; $newSku_1 =$readyForExport['product_name']; $newSku_1 =substr($newSku_1,0,4); $newSku_2 ="/".substr($textSku,-4,4); $newSku_tot =$newSku_1.$newSku_2; $newSku_tot =str_replace(' ', '', $newSku_tot); $newSku_tot =str_replace('-', '', $newSku_tot); $newSku_tot =str_replace('co', '', $newSku_tot); $newSku_tot =str_replace('e', '', $newSku_tot); $newSku_tot =str_replace('r', '', $newSku_tot); $readyForExport['product_name'] =$newSku_tot; // добавка към мястото на разпадане $readyForExport['causale'] ="NR"; // добавка към мястото за разпадане $readyForExport['ORCL'] ="ORCL"; //$readyForExport['G'] =""; $readyForExport['J'] =""; $readyForExport['K'] =""; $readyForExport['L'] =""; $readyForExport['M'] =""; $readyForExport['N'] =""; $readyForExport['P'] =""; $readyForExport['Q'] =""; $readyForExport['R'] ="30"; $index_arr=array("id_customer","date_add","ORCL","product_name","causale","product_quantity","product_price","delivery_date","id_order","J","K"," L","M","N","orderLine","P","Q","R"); //редализирам базата на базата, която не е променлива $index_arr $arr_t=array(); foreach($index_arr като $i=>$v) { foreach($readyForExport като $k=>$b) { if ($k==$v) $arr_t[$k]=$b; } } $readyForExport=$arr_t; //запис в csv ред по ред fputcsv($file, $readyForExport, $ae->delimiter, $ae->separator); } }}


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. ИЗБЕРЕТЕ всички записи, които са на 30 дни

  2. PHP - MYSQL - тестов сървър на база данни

  3. Spring, Hibernate, Blob мързеливо зареждане

  4. Вземете MAX от GROUP BY

  5. Вмъкване само ако стойностите се различават от предишния запис с мулти вмъкване?