hello sir,

i have a php code like

<?php
$count = 0;
    foreach($_POST as $item)
    {
    $count += (is_array($item)) ? count($item) : 1;
    }
   // echo "Items = $count";

    $db=$_POST["db"];
$skills = explode(",", $_POST["skills"]);
$table = explode(",", $_POST["table"]);
$city = explode(",", $_POST["city"]);
$region = explode(",", $_POST["region"]);
$country = explode(",", $_POST["country"]);

$latitude=$_POST["lat"];
$longitude=$_POST["long"];

$distance=$_POST["dist"];

$latitude*=0.01745;
$longitude=0.01745;
//print_r($skills);

/* @var $table type */
$s="select * from $table[0] where skills regexp \"$skills[0]\"";
for($i=1;$i<count($skills);$i++)
{
    $s.=" and skills regexp \"$skills[$i]\"";

    }

    for($i=0;$i<count($region);$i++)
if(!$region[$i]=="")
    {
    $s.=" and region regexp \"$region[$i]\"";

    }

    for($i=0;$i<count($city);$i++)
if(!$city[$i]=="")
    {
    $s.=" and city regexp \"$city[$i]\"";

    }


    for($i=0;$i<count($country);$i++)
if(!$country[$i]=="")
    {
    $s.=" and country regexp \"$country[$i]\"";

    }
    if(!$latitude=="")
   $s.=" and acos(sin($latitude) * sin(Lati) + cos($latitude) * cos(Lati) * cos(Longi - ($longitude))) * 6371 <= $distance";

    $s.=";";

    $con=mysqli_connect("localhost","root","cutiepie_100",$db);
    if (mysqli_connect_errno()) 
        {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$abc=mysqli_query($con, $s) or die('cannot show tables');

echo "<table border='1'>
<tr>
<th>sno</th>
<th>email</th>
<th>skills</th>
<th>location</th>
</tr>";

while($row = mysqli_fetch_array($abc)) {
  echo "<tr>";
    echo "<td>" . $row['sno'] . "</td>";
      echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['skills'] . "</td>";
  echo "<td>" . $row['location'] . "</td>";
  echo "</tr>";
}
echo "</table>";

$filename = strtotime("now")."csv";

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv");

$display = fopen("php://output", 'w');

$flag = false;
while($row = mysql_fetch_assoc($abc)) {
    if(!$flag) {
      // display field/column names as first row
      fputcsv($display, array_keys($row), ",", '"');
      $flag = true;
    }
    fputcsv($display, array_values($row), ",", '"');

<table border='1'>       
<tr>     
<th>sno</th>      
<th>email</th>        
<th>skills</th>       
<th>location</th>     
</tr><tr><td>1</td><td>forankit.j@gmail.com</td><td>cpppl  javapl  matlabpl</td><td>new delhi</td></tr></table>select * from e where skills regexp "javapl"




fclose($display);
 print_r($s);
?>

it provides a downloadable file but the csv i open is not in proper format like i show.

<table border='1'>
<tr>
<th>sno</th>
<th>email</th>
<th>skills</th>
<th>location</th>
</tr><tr><td>1</td><td>forankit.j@gmail.com</td><td>cpppl javapl matlabpl</td><td>new delhi</td></tr></table>select * from e where skills regexp "javapl"

this is the output csv file i get..please help.. i want a proper csv file with metadata and output rows..

Member Avatar for diafol

Why are you placing html into it?

+1 Diafol's most pertinent question,
Are you tryng to output
a csv file for a particular device or service,
a generic .csv, or
create a html table

the html in the middle of your php code without an echo or heredoc, just breaks the php script

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.