-4

Hi i have some data which coming out from database in form of table like thisimage, first i match some data with searching and then display it on page now i need to download it as csv file format please help me check my code and i'm new in php. please check image too for the reference and please please help me

//import.php
// echo "<pre>";
//print_r($_POST);die();
$keyword = $_POST['keyword'];
$csvname = $_POST['csv_file'];

?>

<table border ="1">
    <thead>
        <tr>
            <th>id</th>
            <th>title</th>
            <th>count</th>
        </tr>
    </thead>

<?php

$row = 0;
if (($handle = fopen("idata.csv", "r",)) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {



        $num = count($data);
        // echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            // echo $data[$c] . "<br />\n";
            $query = "SELECT * FROM wm_article WHERE id = '".$data[$c]."'";
            $exec = mysqli_query($conn, $query);
            $details = mysqli_fetch_array($exec);
            $description = $details['title'] ." ".$details['description'];
             //echo $description ;

            //  $regex = "/royal/";

            //  if (preg_match_all($regex, $description, $match))
            //  {

            //      print_r ($matches[0]);
            //  }

             if(preg_match_all("/\b".$keyword."\b/i", $description, $match,PREG_OFFSET_CAPTURE, 3)){
                // echo count(explode('royal', $description));
                // echo "<pre>";print_r($match);



 ?>
 <tr>
 <td><?php echo $details['id'];?></td>
 <td><a href ="<?php echo $details['url'];?>" target ="_blank"><?php echo $details['title'];?></a></td>
 <td><?php echo count($match[0]); ?></td>
</tr>


 <?php



             }
        }
    }

}
?>
<?php



?>
</table>
Dharman
  • 30,962
  • 25
  • 85
  • 135
aman
  • 1
  • 1
    First of all, please go read [ask]. You need to describe to us what you have already tried, and be specific about what your actual issue/problem is. – 04FS Jun 17 '19 at 07:24
  • hey @04fs i will appreciate if you can answer my question rather then giving downvote. i put all my code and i mentioned i'm new so i didn't try and know how to do import – aman Jun 17 '19 at 07:25
  • 2
    You did not ask a proper question to begin with. We are not here to just write your code for you; _you_ are expected to make a reasonable effort to try and solve your problem on your own first of all. Mentioning that you are “new” does not absolve you from any responsibility in that regard. You are likely not the first person ever to try and create a CSV format download with PHP, so you should start by doing some research. Tell us what you found, show us what you tried, describe the actual problem. – 04FS Jun 17 '19 at 07:30
  • This is not the first time I'm seeing this question. Have you reposted ? –  Jun 17 '19 at 07:38
  • no sir my proble is i first match all the data of mysql with my imported csv now i want to download matching data as csv – aman Jun 17 '19 at 07:42
  • Your tags are wrong. Could you pay attention to them next time, please? – Dharman Jun 17 '19 at 08:21
  • i corrected it brother now please give me a perfect solution please? – aman Jun 17 '19 at 08:24
  • I corrected them, you have put them back in. mysqlidb is a wrapper library which you do not use. – Dharman Jun 17 '19 at 14:11

1 Answers1

0

You can use this function to create CSV file & download it,

function array_to_csv($data, $headers, $filename)
{
      header('Content-Type: text/csv; charset=utf-8');
      header('Content-Disposition: attachment; filename='.$filename);
      header('Pragma: no-cache');
      header('Expires: 0');
      $output = fopen('php://output', 'w');
      fputcsv($output, $headers);

        if (count($data) > 0) {
            foreach ($data as $row) {
                fputcsv($output, (array)$row);
            }
        }
  }

What you have to do is to fetch the data you want from the database in array format then define your CVS file headers pass this information to the function as parameter

Ropali Munshi
  • 2,757
  • 4
  • 22
  • 45
  • @aman Please mark it as accepted if this answer helps you. – Ropali Munshi Jun 17 '19 at 07:36
  • it showing error when i put this fileFatal error: Cannot redeclare array_to_csv() (previously declared in C:\xampp\htdocs\match data through csv\import.php:75) in C:\xampp\htdocs\match data through csv\import.php on line 75 – aman Jun 17 '19 at 07:39
  • @aman Do you already have any function named `array_to_csv()` in your code? – Ropali Munshi Jun 17 '19 at 07:40
  • no brother as you can see my code clearly there is no any function even i redeclare this function now Fatal error: Cannot redeclare outputcsv() (previously declared in C:\xampp\htdocs\match data through csv\import.php:75) in C:\xampp\htdocs\match data through csv\import.php on line 75 this is new errror – aman Jun 17 '19 at 07:44
  • This most likely means that you put the function declaration inside a loop in this particular instance. https://stackoverflow.com/questions/1953857/fatal-error-cannot-redeclare-function – 04FS Jun 17 '19 at 07:56
  • now there is no error as i declared function just above $row = 0; but it didn't give me any result how can i do it now or call that fucntion so that it export that data as .csv @04FS – aman Jun 17 '19 at 08:16