I'm trying to generate a csv-file from a database in wordpress.
The generated CSV-file contains the generated database array AND the HTML-sourcecode of the page.
Any idea what a solution could be to get rid of the HTML-Code?
The strategy with ob_start() / ob_end_clean(); seems not to work.
Thanks for your help.
<?php
ob_start();
$filename = 'provider.csv';
$headers = array('ID', 'Name', 'Location');
$handle = fopen('php://memory', 'w');
fputcsv($handle, $headers, ',', '"');
$results = $wpdb->get_results("SELECT * FROM provider");
foreach($results as $results1)
{
$row = array(
$results1->provider_id,
$results1->provider_name,
$results1->provider_location
);
fputcsv($handle, $row, ',', '"');
}
ob_end_clean();
fseek($handle, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
fpassthru($handle);
fclose($handle);
?>
edited: This is how the csv-file looks like
edited: Screenshot of the solution from aniket patel
Please use below code I think it will work for you.
<?php
global $wpdb;
$filename = 'provider.csv';
$headers = array('ID', 'Name', 'Location');
$handle = fopen('php://output', 'w');
fputcsv($handle, $headers, ',', '"');
$results = $wpdb->get_results("SELECT * FROM provider");
foreach($results as $results1)
{
$row = array(
$results1->provider_id,
$results1->provider_name,
$results1->provider_location
);
fputcsv($handle, $row, ',', '"');
}
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
exit;
?>
Related
everything seems to work in the following code as in it does download data from the table but the column names are not showing in the csv what is wrong?
$stmt = $db->prepare("select * from interviews");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$filename = "export.csv";
$f = fopen('php://output', 'w');
header('Content-Type: text/csv');
header('Content-Disposition: attachement; filename="export.csv"');
header("Pragma: no-cache");
header("Expires: 0");
foreach ($rows as $line) {
fputcsv($f, $line);
}
fclose($f);
Thanks
<?php
$conn = mysql_connect("localhost","root","");
if(!$conn){
echo mysql_error();
}
$db = mysql_select_db("imagestore",$conn);
if(!$db ){
echo mysql_error();
}
$q = "SELECT * FROM imagetable";
$r = mysql_query("$q",$conn);
if($r)
{
while($row=mysql_fetch_array($r) )
{
header("Content-type: text/html");
echo "</br>";
echo $row['photoname'];
echo "</br>";
$type = "Content-type: ".$row['phototype'];
header($type);
echo "<img src=image.php?fotoid=". $row['fotoid']."width =300 height = 35. 300/>";
}
}
else{
echo mysql_error();
}
?>
I know ORDER by but is not working to display photos as i want in my page .
I am a beginner .
I used $q = "SELECT * FROM imagetable ORDER BY fotoid DESC";
Cakephp3, I am passing data to another page using Json, my issue seems to be on my new page where my created date is passed as a sting , and displays like '2016-05-16T17:21:10+0000' . How do i format that string to be left with '2016-05-16' ? I've tried setting it as a date, i tried using the i18nFormat with no success.
$http = new Client();
$response = $http->get('http://localhost:8383/mb-be/apis/getmaritimemessagelist.json');
$maritimes = $response->json['content'];
$cnt = count($maritimes);
for ($i=0; $i<$cnt; $i++) {
//var_dump($maritimes[$i]);
echo '<p class="brdr-bttm mrgn-lft-md ">';
echo $this->Html->link($maritimes[$i]['title'], array(
'controller' => 'messages',
'action' => 'view/'. $maritimes[$i]['id']
));
echo '<br />';
$date = $maritimes[$i]['created'];
echo $date;
echo '</p>';
}
I found a solution of this problem.
Here you can use DateTime::createFromFormat to format your created date. but it still has a problem with T inside the date '2016-05-16T17:21:10+0000', just because of timezone(I guess).
you can replace this T with space and got formatted created date as you want.
$http = new Client();
$response = $http->get('http://localhost:8383/mb-be/apis/getmaritimemessagelist.json');
$maritimes = $response->json['content'];
$cnt = count($maritimes);
for ($i=0; $i<$cnt; $i++) {
//var_dump($maritimes[$i]);
echo '<p class="brdr-bttm mrgn-lft-md ">';
echo $this->Html->link($maritimes[$i]['title'], array(
'controller' => 'messages',
'action' => 'view/'. $maritimes[$i]['id']
));
echo '<br />';
$date = $maritimes[$i]['created'];
//--------- Format created date --------
$string = str_replace("T"," ",$date);
$created_date = DateTime::createFromFormat('Y-m-d H:i:sO',$string)->format('Y-m-d');
echo $created_date;
echo '</p>';
}
I log stats for my Minecraft server and display player's in-game stats on my site. I log kills and deaths already, but now I'm trying to get a functioning kill/death ratio.
I am trying to display the kills/deaths in a decimal ratio format (Example: 3789 kills - 5711 deaths would give you a K/DR of 0.663)
elseif ($_GET['task'] == 'stats') {
$get_player = $_GET['player'];
$get_db = 'engine';
$result = mysql_query("SELECT * FROM $get_db WHERE name = '" . mysql_real_escape_string($get_player) . "'", $link);
while($data = mysql_fetch_array($result)) {
echo '{"task":"viewstats","kills":"'; echo $data['kills'];
echo '","deaths":"'; echo $data['deaths'];
echo '","joins":"'; echo $data['joins'];
echo '","quits":"'; echo $data['quits'];
echo '","kicked":"'; echo $data['kicked'];
echo '"}';
}
}
I call upon them in a table like this:
<td><?php echo empty($stats) ? "--" : substr($stats->kills, 0, 50); ?></td>
<td><?php echo empty($stats) ? "--" : substr($stats->deaths, 0, 50); ?></td>
The above PHP code is an API file and the MySQL is already enabled in it - I only posted a snippet of the API though.
You can do this:
echo json_encode(array(
'task' => 'viewstats',
'kills' => $data['kills'],
'deaths' => $data['deaths'],
'joins'=> $data['joins'],
'quits' => $data['quits'],
'kicked' => $data['kicked'],
// then ratio
'ratio' => $data['kills'] / $data['deaths'],
));
//**Make sure this Function is declared at the top of your script.**
function MySQLi_quickConnect()
{
$host = 'somewebsite.db.120327161.hostedresource.com'; //or 'http://localhost'
$username = '<YOUR USERNAME>';
$password = '<YOUR PASSWORD>';
$database = '<YOUR DATABASES NAME>';
$db = new MySQLi($host,$username,$password,$database);
$error_message = $db->connect_error;
if($error_message != NULL){die("Error:" . $error_message . "<br>" . "Occured in function
MySQLi_quickConnect");}
return $db;
}
//Replace your code with this:
elseif($_GET['task'] == 'stats') {
$get_player = $_GET['player'];
$get_db = 'engine';
$mysqli = MySQLi_quickConnect();
$query = ('SELECT kills, deaths, FROM ? WHERE name = ? ');
if ($stmt = $mysqli->prepare($query)) {
$stmt->bind_param("ss", $get_db, $get_player);
$stmt->execute();
$stmt->bind_result($kills, $deaths);
}
while ($stmt->fetch()) {
$kdr = $kills/$deaths;
echo "You have a K/DR of " . $kdr . "<br>";
}
$stmt->close();
}
Note: Verify your Database connection, table names, and $_Get variables.
I want to export mysql table into pdf format. Please help me
I have tried this code. Here Is my code
My.php
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("jass12", $con);
$result = mysql_query("SHOW COLUMNS FROM ja");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result))
{
#$csv_output .='"'.$row['Field'].'","';
$i++;}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ja");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].",";
}
$csv_output .= "\n";
}
#$filename = $file."_".date("d-m-Y_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: xls" . date("Y-m-d") . ".xls");
header( "Content-disposition: filename=".$filename.".xls");
print $csv_output;
exit;
?>
It produces error & also give as output as xls file.
I want to get the exact coding which convert the database file into pdf.
Please help me
try using FPDF http://www.fpdf.org/.