PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > creating an xls file from mysql data
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
creating an xls file from mysql data

Réponse
 
LinkBack Outils de la discussion
Vieux 11/05/2008, 21h57   #1
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut creating an xls file from mysql data


This script will create an xls file from the data that is sent to it
When I run this it only gets one recored and it is supposet to get all
the records that are past by the $_POST[selectedcontactlist]
I think I have a } in the wrong place but I can not figure it out
anybody have a suggestion

$_POST[selectedcontactlist]="3,45,65,23,12,4,56"; //this is a sample of
what is past

$ExplodeIt = explode(",",rtrim($_POST[selectedcontactlist],","));
$Count = count($ExplodeIt);
for ($i=0; $i < $Count; $i++) {
$sql = "SELECT * FROM contacts WHERE id = '$ExplodeIt[$i]'";
$sql_result = query($sql);
$count = mysql_num_fields($sql_result);

for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($sql_result, $i)."\t";
}

while($row = mysql_fetch_row($sql_result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi
line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);


# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";
}

# This line will stream the file to the user rather than spray it across
the screen
header("Content-type: application/octet-stream");

# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo $header."\n".$data;

  Réponse avec citation
Vieux 11/05/2008, 22h06   #2
Andrew Johnstone
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] creating an xls file from mysql data

Hi,
You could always do this within mysql itself. You also have the wrong output
header and what seems to be some quite inefficient code, anyway take a look
at the following.

SELECT order_id,product_name,qty
FROM orders
INTO OUTFILE '/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

Thanks

Andrew

2008/5/11 Richard Kurth <richardkurth@centurytel.net>:

>
> This script will create an xls file from the data that is sent to it
> When I run this it only gets one recored and it is supposet to get all the
> records that are past by the $_POST[selectedcontactlist]
> I think I have a } in the wrong place but I can not figure it out anybody
> have a suggestion
>
> $_POST[selectedcontactlist]="3,45,65,23,12,4,56"; //this is a sample of
> what is past
>
> $ExplodeIt = explode(",",rtrim($_POST[selectedcontactlist],","));
> $Count = count($ExplodeIt);
> for ($i=0; $i < $Count; $i++) {
> $sql = "SELECT * FROM contacts WHERE id = '$ExplodeIt[$i]'";
> $sql_result = query($sql);
> $count = mysql_num_fields($sql_result);
>
> for ($i = 0; $i < $count; $i++){
> $header .= mysql_field_name($sql_result, $i)."\t";
> }
>
> while($row = mysql_fetch_row($sql_result)){
> $line = '';
> foreach($row as $value){
> if(!isset($value) || $value == ""){
> $value = "\t";
> }else{
> # important to escape any quotes to preserve them in the data.
> $value = str_replace('"', '""', $value);
> # needed to encapsulate data in quotes because some data might be multi
> line.
> # the good news is that numbers remain numbers in Excel even though quoted.
> $value = '"' . $value . '"' . "\t";
> }
> $line .= $value;
> }
> $data .= trim($line)."\n";
> }
> }
> # this line is needed because returns embedded in the data have "\r"
> # and this looks like a "box character" in Excel
> $data = str_replace("\r", "", $data);
>
>
> # Nice to let someone know that the search came up empty.
> # Otherwise only the column name headers will be output to Excel.
> if ($data == "") {
> $data = "\nno matching records found\n";
> }
>
> # This line will stream the file to the user rather than spray it across
> the screen
> header("Content-type: application/octet-stream");
>
> # replace excelfile.xls with whatever you want the filename to default to
> header("Content-Disposition: attachment; filename=excelfile.xls");
> header("Pragma: no-cache");
> header("Expires: 0");
>
> echo $header."\n".$data;
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


  Réponse avec citation
Vieux 11/05/2008, 22h27   #3
Hans Wolters
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] creating an xls file from mysql data

In article <d16dfa3f0805111406h7d299cc1q44002e9a82f057b8@mail .gmail.com>,
"Andrew Johnstone" wrote:
>
> SELECT order_id,product_name,qty
> FROM orders
> INTO OUTFILE '/tmp/orders.csv'
> FIELDS TERMINATED BY ','
> ENCLOSED BY '"'
> LINES TERMINATED BY '\n'


Into outfile needs specific user rights. Default it's not available.

Hans
--
IM: hans.wolters@xs4all.nl

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h16.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12924 seconds with 11 queries