0

How to export grabbed data to .csv file? I'm using php simple html dom to parse data. Here is the code:

foreach ($linkoviStranica as $stranica)
{
    $podaciStranice = "http://someurl/$stranica";

    $data = file_get_html($podaciStranice);


    $name = $data->find('div[class="price-card-name-header-name"]');
    $onlinePrice = $data->find('div[class="price-box online"]');
    $diffPrice = $data->find('div[class="price-box paper"]');

     echo "<strong>".$name[0]->innertext."<strong>"."<br>";
      if (!empty($onlinePrice[0]->innertext))
      { 
        echo $onlinePrice[0]->innertext."<br>";
      }
     if (!empty($diffPrice[0]->innertext))
     {  
         echo $diffPrice[0]->innertext."<br>";

         echo "---------------------"."<br>";

     }
 }

I want to export, $name, $onlinePrice, $diffPrice to csv file with header in the following format:

name    onlinePrice diffPrice
example    10          44
xxxx       412        461
zzzzz     1414         41

Could you please help me? Thanks!

2 Answers 2

1

Something like this:

// Define a array to hold all the data
$data = [];

// OR use this line if you want the headers with names on the first line of the file
// $data = [['name', 'onlinePrice', 'diffPrice']];

// Loop the raw data
foreach ($linkoviStranica as $stranica) {
    $podaciStranice = "http://someurl/$stranica";
    $data = file_get_html($podaciStranice);

    $name = $data->find('div[class="price-card-name-header-name"]');
    $onlinePrice = $data->find('div[class="price-box online"]');
    $diffPrice = $data->find('div[class="price-box paper"]');

    // Add current row to out array
    $data[] = [
        $name[0]->innertext,
        $onlinePrice[0]->innertext,
        $diffPrice[0]->innertext
    ];
}

// Open a new file. Replace the file name with the name you'd like
$fp = fopen('file.csv', 'w');

// Loop each row in the data array
foreach ($data as $fields) {
    // This method converts a row to a CSV line (http://php.net/manual/en/function.fputcsv.php)
    fputcsv($fp, $fields);
}

// Close the file handler
fclose($fp);
Sign up to request clarification or add additional context in comments.

2 Comments

tnx this is what I was looking for, with small changes, I've added header with this: fputcsv($fp, array('name', 'online price', 'diff price')); because data['name', 'online price', 'diff price'] gives me an error.
@dilesko See the fourth line in my code snippet. It adds this header. Please accept my answer if it solved your problem, so people can easier find it later.
0
$header ="SNo , Order Date , Order Number , Compaign , Amount , Order Status , Used Date , Cancel Reason , Order Commision , Payment Date , Payment Status \n";
            $header1 =$header ;
$result='';
foreach ($data as $fields) {
  $result= 'echo your data with coma seprated '."\n";
}
$all=$header.$result;
$name="report.csv";
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=".$name);
header("Pragma: no-cache");
header("Expires: 0");
print "$header1 $result";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.