Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP create a complex CSV file
    primarykey
    data
    text
    <p>I am in need to create a CSV file getting the data from a mySQL DB.</p> <p>The fact is that I want the CSV tp be corrected labeled and not just writing the data like this:</p> <pre><code>id,name,url 1,thisismyname,thisismyurl </code></pre> <p>I need the CSV file to look well ordered and each data inserted in the relative column.</p> <p>Also with the function I am going to add below I can only grab the data from the DB and write it to the CSV file as it is. But I need to work with the data and have the CSV labeled in this way:</p> <pre><code>Campaign Name: Name of the campaign Campaign Url: Url of the campaign Tot visits: Tot of visits Tot unique visits: Tot of unique visits id name url 1 thisname this url 2 thisname this url 3 thisname this url 4 thisname this url 5 thisname this url </code></pre> <p>This is the PHP code I have so far..I need to understand how to achieve a correct structure of the CSV with PHP and adding the lines in it the exact way I want..</p> <p>Thanks for your help!</p> <pre><code>function genCSV($filename, $attachment = true, $headers = true) { // send response headers to the browser header('Content-Type: text/csv'); header('Content-Disposition: attachment;filename=' . $filename); $fp = fopen('php://output', 'w'); $query = "SELECT * FROM campaigns"; $result = mysql_query($query) or die(mysql_error()); if ($headers) { // output header row (if at least one row exists) $row = mysql_fetch_assoc($result); if ($row) { fputcsv($fp, array_keys($row)); // reset pointer back to beginning mysql_data_seek($result, 0); } } while ($row = mysql_fetch_assoc($result)) { fputcsv($fp, $row); } fclose($fp); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload