Bandingkan baris CSV dalam php
P粉649990273
P粉649990273 2024-04-04 13:57:04
0
1
516

Saya mempunyai kod berikut yang membolehkan saya menjana fail csv daripada pangkalan data.

<?php
require_once 'dbconfig.php';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

    $sql = 'SELECT o.id_order ,c.id_customer, c.firstname, c.lastname, a.address1, a.address2, a.postcode, a.city,a.phone,c.email,a.phone_mobile, od.product_id, od.product_name,od.product_quantity, od.product_price,o.total_paid_tax_incl, c.id_customer, op.date_add, op.amount, op.payment_method 
    FROM mod582_orders o 
    INNER JOIN mod582_customer c ON o.id_customer = c.id_customer 
    INNER JOIN mod582_address a ON o.id_address_delivery = a.id_address 
    INNER JOIN mod582_order_detail od ON o.id_order = od.id_order 
    INNER JOIN mod582_order_payment op ON o.reference = op.order_reference 
    WHERE CAST(o.date_add AS DATE) LIKE "2023-01%" /*CAST( curdate() AS DATE)*/; 
    ';
    
    $r = $pdo->query($sql);

$tab = \[\];
$tab\[\] = \['ORDNOORDER', 'ORDREFCUSORDER', 'ORDNOCOSTNUMBER','ORDNOCUSTOMER','ORDCUSTOMERCODE','ORDCUSCAT','ORDTYPE','ORDCURRENCY','ORDCURRENCYRATE','ORDDESIGNATION','ORDREPCODE','ORDPORT','ORDPORTTYPE','ORDPORTRATE','DEONOORDER','DEOCOMMENT','DEOCOUNTRY','DEONAME','DEOFIRSTNAME','DEOADDRESS1','DEOADDRESS2','DEOZIPCODE','DEOCITY','DEOPHONE','DEOMAIL','DEOPHONEPORTABLE','ODLNOORDER','ODLNOORDERLINE','ODLNOARTICLE','ODLARTDESIGN','ODLQUANTITYORDER','ODLTTCCURUPRICE','ODLCODEPARCELLEFLEU','PAYNUMPAYMENT','PAYNOCUSTOMER','PAYNOORDER','PAYNOCURRENCY','PAYDATEPAYMENT','PAYPAYMENTTTCCUR','PAYCURRENCYRATE','PAYCONTREPARTIE'\];
$odrline = 1;
while($rs = $r-\>fetch(PDO::FETCH_ASSOC)){
$tab\[\] = \[$rs\['id_order'\], $rs\['id_order'\], '17', '', 'AAA'.$rs\['id_customer'\],'DET','O','EUR','1','','115','D','P','17', $rs\['id_order'\],'','FRA', $rs\['firstname'\], $rs\['lastname'\], $rs\['adress1'\], $rs\['adress2'\], $rs\['postcode'\], $rs\['city'\], $rs\['phone'\], $rs\['email'\], $rs\['phone_modile'\], $rs\['id_order'\],$odrline, $rs\['product_id'\], $rs\['product_name'\], $rs\['product_quantity'\], $rs\['product_price'\],'','','', $rs\['id_order'\],'EUR', $rs\['date_add'\], $rs\['amount'\],'1','VIR'\];
}

$fichier_csv = new SplFileObject('vinistoria/commandes.csv', 'w');
foreach($tab as $ligne){
$fichier_csv-\>fputcsv($ligne, ';');
}
$date = date('d-m-y h:i:s');
echo "nouveau fichier commandes.csv créé à ". $date;
} catch (PDOException $e) {
die("Could not connect to the database $dbname :" . $e-\>getMessage());
}

?\>

Walau bagaimanapun, saya mempunyai beberapa medan yang perlu diubah berdasarkan nilai medan dalam baris sebelumnya csv. Sebagai contoh : Untuk semua baris yang $rs['id_order'] mempunyai nilai 486, nilai odrline saya ialah 1, tetapi jika dalam baris seterusnya $rs['id_order'] ialah 487 odrline sepatutnya 2 etc..

Bagaimana saya boleh menyemak imbas fail saya dan menukar nilai berdasarkan medan dalam baris sebelumnya?

Bagaimana saya boleh menyemak imbas fail saya dan menukar nilai berdasarkan medan dalam baris sebelumnya?

P粉649990273
P粉649990273

membalas semua(1)
P粉207969787

Jangan tukar data melalui fail selepas menciptanya, sebaliknya proses data sebelum mencipta fail.

Simpan id_order dalam pembolehubah dan bandingkannya pada permulaan setiap lelaran gelung.

Serupa dengan:

$odrline = 0;   // start with zero, as incremented before first written out
$prevordernumber = 0;   // pick a number that is not a valid order number
                        // or empty string if not numeric
while($rs = $r->fetch(PDO::FETCH_ASSOC)){
  // if order number has changed then increment order line
  if($prevordernumber != $rs['id_order']) $odrline++;
  $tab [] = ........

  // update stored previous order number
  $prevordernumber = $rs['id_order'];  
}
// write $tab to file
$fichier_csv = new SplFileObject('vinistoria/commandes.csv', 'w');
foreach($tab as $ligne){
$fichier_csv-\>fputcsv($ligne, ';');
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan