29-04-2004, 00:19
|
#1
|
|
!
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
More PHP help (sorry)
I know I'm whoringly annoying with all the PHP/SQL/ASP questions but I have one final thing to do before i can shut up about it, I have this PHP script which writes a table into excel CSV format, works perfectly, trouble is, it writes it to the screen and I need it to write it to a file on the server instead, anyone have any ideas?
Code:
<?php
define(db_host, "localhost");
define(db_user, "dbuser");
define(db_pass, "dbpassword");
define(db_link, mysql_connect(db_host,db_user,db_pass));
define(db_name, "dbpassword");
mysql_select_db(db_name);
$select = "SELECT * FROM homes";
$export = mysql_query($select);
$count = mysql_num_fields($export);
for ($i = 0; $i < $count; $i++) {
$header .= mysql_field_name($export, $i)."\t";
}
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r", "", $data);
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=spreadsheet.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
|
|
|
29-04-2004, 07:02
|
#2
|
|
cf.mega poster
Join Date: Jun 2003
Location: England
Services: I no longer receive cable services, I blame the inept accounts dept for that.
Posts: 3,686
|
Re: More PHP help (sorry)
|
|
|
29-04-2004, 08:30
|
#3
|
|
!
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: More PHP help (sorry)
Quote:
|
Originally Posted by zoombini
|
That's where the above script came from, giving me URL's is no good, I spent at least 4 hours yesterday trawling through hundreds of site's to find an answer.
|
|
|
29-04-2004, 09:57
|
#4
|
|
Gone
Join Date: Jun 2003
Age: 32
Posts: 14,760
|
Re: More PHP help (sorry)
Quote:
|
Originally Posted by Bifta
I know I'm whoringly annoying with all the PHP/SQL/ASP questions but I have one final thing to do before i can shut up about it, I have this PHP script which writes a table into excel CSV format, works perfectly, trouble is, it writes it to the screen and I need it to write it to a file on the server instead, anyone have any ideas?
Code:
<?php
define(db_host, "localhost");
define(db_user, "dbuser");
define(db_pass, "dbpassword");
define(db_link, mysql_connect(db_host,db_user,db_pass));
define(db_name, "dbpassword");
mysql_select_db(db_name);
$select = "SELECT * FROM homes";
$export = mysql_query($select);
$count = mysql_num_fields($export);
for ($i = 0; $i < $count; $i++) {
$header .= mysql_field_name($export, $i)."\t";
}
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r", "", $data);
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=spreadsheet.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
|
You see the last line where it says print? That outputs it to the screen. If you want to to write to a file, change the line to...
Code:
$filename = "/absolute/path/to/file.csv"; // define file name to write to
$handle = fopen($filename, "wb"); // open the file for write binary safe mode
fwrite($handle, $header . "\n" . $data); // write data to file
fclose($handle); // close the open file.
HTH
|
|
|
29-04-2004, 10:48
|
#5
|
|
!
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: More PHP help (sorry)
Quote:
|
Originally Posted by punky
You see the last line where it says print? That outputs it to the screen. If you want to to write to a file, change the line to...
Code:
$filename = "/absolute/path/to/file.csv"; // define file name to write to
$handle = fopen($filename, "wb"); // open the file for write binary safe mode
fwrite($handle, $header . "\n" . $data); // write data to file
fclose($handle); // close the open file.
HTH
|
That more than helped! Fantastic! I'd rep you more than once if it were possible.
|
|
|
29-04-2004, 11:00
|
#6
|
|
cf.mega poster
Join Date: Jun 2003
Location: England
Services: I no longer receive cable services, I blame the inept accounts dept for that.
Posts: 3,686
|
Re: More PHP help (sorry)
I meant to try the forums there & perhaps the author...
|
|
|
29-04-2004, 11:04
|
#7
|
|
Gone
Join Date: Jun 2003
Age: 32
Posts: 14,760
|
Re: More PHP help (sorry)
Woot! I got 4 now, and i've gone up a level  Gotta love being a programmer. Thanx guys
|
|
|
29-04-2004, 11:06
|
#8
|
|
Guest
Location: Luton
Services: NTL Nafband
Posts: n/a
|
Re: More PHP help (sorry)
Quote:
|
Originally Posted by punky
Woot! I got 4 now, and i've gone up a level  Gotta love being a programmer. Thanx guys 
|
You deserve it m8... Keep helping like this and you will soon be limping allong with 12 of them
 Nice one
|
|
|
|
29-04-2004, 11:11
|
#9
|
|
!
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: More PHP help (sorry)
Quote:
|
Originally Posted by zoombini
I meant to try the forums there & perhaps the author...
|
Ahh but see, I know there are very talented programmers who frequent this site, as you can see
|
|
|
29-04-2004, 11:33
|
#10
|
|
Gone
Join Date: Jun 2003
Age: 32
Posts: 14,760
|
Re: More PHP help (sorry)
|
|
|
29-04-2004, 12:48
|
#11
|
|
!
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: More PHP help (sorry)
Ah now I've inflated your head, here's a very simple one
I want to print some text, pause, print some more test pause etc
print "whatever";
sleep(5);
print "whatever";
doesn't work, it just pauses for 5 seconds then prints everything at once.
|
|
|
29-04-2004, 17:24
|
#12
|
|
Gone
Join Date: Jun 2003
Age: 32
Posts: 14,760
|
Re: More PHP help (sorry)
Quote:
|
Originally Posted by Bifta
Ah now I've inflated your head, here's a very simple one
I want to print some text, pause, print some more test pause etc
print "whatever";
sleep(5);
print "whatever";
doesn't work, it just pauses for 5 seconds then prints everything at once.
|
It isn't as simple as you think. Because of the way that PHP works, it's a server-side scripting language, which means it processes everything on the server, then returns the result. I don't think you can do it solely with PHP. You could do some re-iterating post-submission, but i'm pretty sure you need some local scripter, like Javascrip, to keep submitting it. It's dead easy with Javascript though. But PHP reacting dynamically with a web page isn't something you can do I don't think.
|
|
|
29-04-2004, 17:39
|
#13
|
|
cf.mega poster
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 34
Posts: 6,414
|
Re: More PHP help (sorry)
Quote:
|
Originally Posted by Bifta
Ah now I've inflated your head, here's a very simple one
I want to print some text, pause, print some more test pause etc
print "whatever";
sleep(5);
print "whatever";
doesn't work, it just pauses for 5 seconds then prints everything at once.
|
Only Perl could do that server-side, you could use Javascript though.
What is it your trying to do exactly?
You could also use a META refresh like the old VB 2 did inbetween posting and going to the thread.
|
|
|
29-04-2004, 18:17
|
#14
|
|
!
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: More PHP help (sorry)
Thankyou gents, I'll just slot some javascript delays in instead.
Cheers!
|
|
|
29-04-2004, 18:19
|
#15
|
|
Gone
Join Date: Jun 2003
Age: 32
Posts: 14,760
|
Re: More PHP help (sorry)
I've had a bash using pure Javascript :
Code:
<html>
<head>
<script language="JavaScript">
var numberoftimes = 3;
var count = 0;
function repeattext() {
document.body.innerHTML = document.body.innerHTML + "Whatever" + "<br>";
count++;
if (count < numberoftimes)
{
setTimeout("repeattext()", 1000);
}
}
</script>
</head>
<body onLoad="repeattext()">
</body>
</html>
If you wanted to use some feature of PHP though (like its MySql functions), its easy.. you can just put <?php somecommands ?> and echo what you want to be displayed. If you want it inside HTML, just do something like...
Code:
<input type="text" value="<?php echo($_SERVER['PHP_SELF']); ?>">
But if you want it in Javascript, you need something like:
Code:
document.body.innerHTML = document.body.innerHTML + "<?php echo($_SERVER['PHP_SELF']); ?>" + "<br>";
HTH
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 19:07.
|