简体   繁体   中英

How to add the results of a sql oracle query in a csv file in php?

I have a php code that I connect to an oracle database and with a request to retrieve the information I want, here is my code :

$query = "SELECT ACTIVE_SIZES FROM ADA_ACTIVE_SIZE2_VIEW WHERE ADA_STYLE = 'SCPCL4'";  
$result = odbc_exec($connect, $query);
  
   while($final = odbc_fetch_array($result)) {
        
         print_r($final); //Array ( [ACTIVE_SIZES] => XS-S-M-L-XL-2XL )
        
       } 

Now I'm reading a csv file and I would like to adapt this code to add the results of my queries in a column at the end of the file. I already add two columns at the end of this one, but my query doesn't return anything in the csv file, how can I do please?

<?php 
//Modifications on csv file
$delimiter = ";"; 
$csv_data = array();
if (($handle = fopen($nomcsv, 'r')) !== FALSE) {
    while (($data = fgetcsv($handle, 10000, $delimiter)) !== FALSE) {
        //Add two columns at the end
        $data['Pictures Names'] = (!empty($data[4]) ? ($data[7] ?: '') . "_" . $data[4] . '.jpg' : ''); 
        $data['Color-Description'] = (!empty($data[3]) ? (ltrim($data[4], '0') ?: '') . "-" . $data[3] : '');  
        
        //Query
        $query = "SELECT ACTIVE_SIZES FROM ADA_ACTIVE_SIZE2_VIEW WHERE ADA_STYLE = '".$data[4]."'";
        $result = odbc_exec($connect, $query);
        
         while($final = odbc_fetch_array($result)) {
        
                $data['Sizes'] = $final;
               var_dump($final); //array(1) { ["ACTIVE_SIZES"]=> string(8) "XS-S-M-L" }array(1) { ["ACTIVE_SIZES"]=> string(8) "XS-S-M-L" }...
            
        }  
        
            $csv_data[] = $data; 
            var_dump($csv_data); //["Pictures Names"]=> string(15) "SCJEG4_1041.jpg" ["Color-Description"]=> string(12) "1041-MUSTARD" ["Sizes"]=> array(1) { ["ACTIVE_SIZES"]=> string(15) "XS-S-M-L-XL-2XL" } } }
             
    }
    fclose($handle);
}

if (($handle = fopen($nomcsv, 'w')) !== FALSE) {
    foreach ($csv_data as $data) {
        fputcsv($handle, $data, $delimiter);
    }
    fclose($handle);
}

?>

At the end I have this in my csv file:

在此处输入图片说明

Try this, only thing is you need to define your ActiveSizes index and push it to the sizes index

  $delimiter = ";"; 
  $csv_data = array();
  if (($handle = fopen($nomcsv, 'r')) !== FALSE) {
      while (($data = fgetcsv($handle, 10000, $delimiter)) !== FALSE) {
          //Add two columns at the end
          $data['Pictures Names'] = (!empty($data[4]) ? ($data[7] ?: '') . "_" . $data[4] . '.jpg' : ''); 
          $data['Color-Description'] = (!empty($data[3]) ? (ltrim($data[4], '0') ?: '') . "-" . $data[3] : '');  
          
          //Query
          $query = "SELECT ACTIVE_SIZES FROM ADA_ACTIVE_SIZE2_VIEW WHERE ADA_STYLE = '".$data[4]."'";

          $result = odbc_exec($connect, $query);
          
          while($row = odbc_fetch_array($result)) {
          
              $data['Sizes'][] = $row['ACTIVE_SIZES'];
              
          }  
          
          $csv_data[] = $data; 
              
      }
      
      fclose($handle);
  }

  //var_dump($csv_data);

  if (($handle = fopen($nomcsv, 'w')) !== FALSE) {
      foreach ($csv_data as $data) {
          fputcsv($handle, $data, $delimiter);
      }

      fclose($handle);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM