FAUSTINE MWOYA November 18, 2025

Jinsi ya Kutengeneza PHP CSV Export Function | Rahisi & Haraka

Pata mifumo zaidi kupitia faulink.com
Jifunze kutengeneza CSV Export Function kwa PHP ili kusave data za report, mauzo, wanafunzi, matokeo au bidhaa kwa urahisi. Inafanya download ya faili moja kwa moja bila library yoyote.

✅ 1. Function ya Ku-Export Data kuwa CSV
function export_to_csv($filename, $columns, $data) {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'"');

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

// Header row (column names)
fputcsv($output, $columns);

// Data rows
foreach ($data as $row) {
fputcsv($output, $row);
}

fclose($output);
exit;
}

✅ 2. Kutumia Export Function
Mfano wa data ya ku-export:
$columns = ["ID", "Name", "Amount", "Date"];

$data = [
[1, "John", 12000, "2025-01-01"],
[2, "Asha", 9000, "2025-01-02"],
[3, "Peter", 6000, "2025-01-03"]
];

Kuitumia kwa button:
if(isset($_GET['export'])) {
export_to_csv("sales_report.csv", $columns, $data);
}

Button ya Download
<a href="?export=1">Download CSV</a>

✅ 3. Kutumia Export kwa Database Results
$result = $conn->query("SELECT id, name, amount, date FROM sales");
$data = [];

while($row = $result->fetch_assoc()) {
$data[] = [$row['id'], $row['name'], $row['amount'], $row['date']];
}

export_to_csv("db_sales.csv", ["ID", "Name", "Amount", "Date"], $data);

🔥 Faida za CSV Export

Inafanya kazi kwenye Excel, Google Sheets, LibreOffice

Rahisi sana kutengeneza reports

Haina library — ni native PHP

Inafaa kwa:
✔️ Sales reports
✔️ Student lists
✔️ Inventory data
✔️ Financial statements

🔗 Links Za Kujifunza Zaidi

🌐 Faulink Official Website:
https://www.faulink.com/

📘 Jifunze Web Design & Programming (Tutorials / Mifumo):
https://www.faulink.com/excel_mifumo.php

📲 Piga / WhatsApp kwa msaada wa haraka:
https://wa.me/255693118509
Share this post
Previous Next

Comments

0
No comments yet. Be the first to comment.

Continue Reading

Subscribe

Get new updates

Jiunge upokee posts mpya, tutorials, na updates za mifumo moja kwa moja kwenye email yako.

Chat na Faulink