Question

Here I am getting some result in form of array from mysql db. I want to write entire result into excel file.

Result is in loop, so result should be appended till end of loop.

    for($i = 5; $i > 0 ; $i--)
    {
        $select_words = mysqli_query($con,"SELECT * from review_details where rate = $i and category = 'italian' and isApi = 0");
        while ($row1 = @mysqli_fetch_array($select_words))
        {
            //echo $row1[review]."<br>";
//          echo $row1[rate]."<br>";
            $word.=$row1[adjective].",";        
        }
        //echo "Words are : $word";
        echo "<br/><br/><strong> $i star word count </strong>"; // want to write this in excel
        $word_count = array_count_values(str_word_count($word,1));
        arsort($word_count);
        echo "<br>";
        echo "<br>";
        //var_dump($word_count);
        $word_count2 = array_slice($word_count,0,20);
        print_r($word_count2); //Writing this to excel

        unset($word);
        unset($word_count);
        unset($select_words);
    }

result looks like this:

Category : Italian

5 star word count

Array ( [Italian] => 1754 [good] => 1600 [great] => 1514 [delicious] => 923 [amazing] => 740 [fresh] => 596 [nice] => 486 [perfect] => 478 [little] => 467 [favorite] => 463 [other] => 451 [excellent] => 437 [special] => 395 [small] => 388 [wonderful] => 380 [much] => 342 [Great] => 314 [sure] => 301 [last] => 300 [fantastic] => 292 )

4 star word count

Array ( [good] => 2215 [Italian] => 1388 [great] => 1261 [delicious] => 791 [little] => 706 [nice] => 668 [fresh] => 465 [small] => 447 [other] => 427 [much] => 412 [tasty] => 357 [excellent] => 352 [amazing] => 312 [perfect] => 301 [special] => 297 [next] => 290 [few] => 248 [Great] => 243 [sure] => 241 [attentive] => 233 )

3 star word count

Array ( [good] => 1017 [Italian] => 470 [great] => 386 [nice] => 288 [little] => 271 [other] => 264 [much] => 231 [small] => 198 [special] => 161 [delicious] => 145 [tasty] => 130 [bad] => 127 [sure] => 118 [fresh] => 115 [decent] => 114 [next] => 109 [italian] => 109 [few] => 109 [average] => 95 [many] => 94 )

2 star word count

Array ( [good] => 342 [Italian] => 222 [great] => 144 [other] => 122 [much] => 114 [nice] => 112 [little] => 104 [bad] => 101 [small] => 93 [special] => 84 [bland] => 67 [long] => 60 [many] => 53 [fresh] => 53 [next] => 46 [mediocre] => 45 [few] => 45 [tasty] => 45 [high] => 44 [sure] => 43 )

1 star word count
Array ( [good] => 150 [Italian] => 104 [bad] => 87 [other] => 66 [great] => 55 [much] => 50 [few] => 48 [next] => 39 [many] => 36 [special] => 35 [little] => 33 [small] => 32 [last] => 32 [same] => 30 [nice] => 30 [long] => 30 [rude] => 29 [sure] => 29 [bland] => 29 [such] => 26 ) 

Here is one way which can give excel result after each test:

$filename ="data.xls";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);

But I dont know how it will write above result into this file.

UPDATE

I updated the code as below as per answer:

        $fp = fopen('file.xlsx', 'w');

        foreach ($word_count2 as $fields)
        {
            fputcsv($fp, $fields);
        }
        fclose($fp);

file.xlsx contains no data. I also used file.csv, that also contains 0 bytes of data.

Was it helpful?

Solution 2

Here I could do it:

        $file = fopen("words.xls","w+"); //clear the content every time before start writing. To see new content everytime
        fclose($file);
        for($i = 5; $i > 0 ; $i--)
        {
            $select_words = mysqli_query($con,"SELECT * from review_details where rate = $i and category = 'italian' and isApi = 0");
            while ($row1 = @mysqli_fetch_array($select_words))
            {
                //echo $row1[review]."<br>";
    //          echo $row1[rate]."<br>";
                $word.=$row1[adjective].",";        
            }
            //echo "Words are : $word";
            echo "<br/><br/><strong> $i star word count </strong>";
            $word_count = array_count_values(str_word_count($word,1));
            arsort($word_count);
            echo "<br>";
            echo "<br>";
            //var_dump($word_count);
            $word_count2 = array_slice($word_count,0,20);
            print_r($word_count2);

            unset($word);
            unset($word_count);
            unset($select_words);
            file_put_contents('words.xls', print_r($word_count2, true), FILE_APPEND | LOCK_EX);
        }

OTHER TIPS

The PHP Function fputcsv (PHP Docs) writes an array as CSV to a opened file handler.

Example use:

<?php

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>

(taken from the PHP Doc)

So basicly you iterate over your array and write each line as array to the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top