Home News Forum Articles
  Welcome back Join CF
You are here You are here: Home | Forum | More PHP help (sorry)


You are currently viewing our boards as a guest which gives you limited access to view most of the discussions, articles and other free features. By joining our Virgin Media community you will have full access to all discussions, be able to view and post threads, communicate privately with other members (PM), respond to polls, upload your own images/photos, and access many other special features. Registration is fast, simple and absolutely free so please join our community today.


Welcome to Cable Forum
Go Back   Cable Forum > Computers & IT > General IT Discussion

More PHP help (sorry)
Reply
 
Thread Tools
Old 29-04-2004, 00:19   #1
Bifta
!
 
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
Bifta has a nice shiny starBifta has a nice shiny star
Bifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny star
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";
?>
Bifta is offline   Reply With Quote
Advertisement
Old 29-04-2004, 07:02   #2
zoombini
cf.mega poster
 
zoombini's Avatar
 
Join Date: Jun 2003
Location: England
Services: I no longer receive cable services, I blame the inept accounts dept for that.
Posts: 3,686
zoombini has reached the bronze age
zoombini has reached the bronze agezoombini has reached the bronze agezoombini has reached the bronze agezoombini has reached the bronze agezoombini has reached the bronze agezoombini has reached the bronze agezoombini has reached the bronze age
Re: More PHP help (sorry)

http://www.phpfreaks.com/
zoombini is offline   Reply With Quote
Old 29-04-2004, 08:30   #3
Bifta
!
 
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
Bifta has a nice shiny starBifta has a nice shiny star
Bifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny star
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.
Bifta is offline   Reply With Quote
Old 29-04-2004, 09:57   #4
punky
Gone
Alpha Bravo Charlie Champion
 
Join Date: Jun 2003
Age: 32
Posts: 14,760
punky has a golden aurapunky has a golden aura
punky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aura
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
punky is offline   Reply With Quote
Old 29-04-2004, 10:48   #5
Bifta
!
 
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
Bifta has a nice shiny starBifta has a nice shiny star
Bifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny star
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.
Bifta is offline   Reply With Quote
Old 29-04-2004, 11:00   #6
zoombini
cf.mega poster
 
zoombini's Avatar
 
Join Date: Jun 2003
Location: England
Services: I no longer receive cable services, I blame the inept accounts dept for that.
Posts: 3,686
zoombini has reached the bronze age
zoombini has reached the bronze agezoombini has reached the bronze agezoombini has reached the bronze agezoombini has reached the bronze agezoombini has reached the bronze agezoombini has reached the bronze agezoombini has reached the bronze age
Re: More PHP help (sorry)

I meant to try the forums there & perhaps the author...
zoombini is offline   Reply With Quote
Old 29-04-2004, 11:04   #7
punky
Gone
Alpha Bravo Charlie Champion
 
Join Date: Jun 2003
Age: 32
Posts: 14,760
punky has a golden aurapunky has a golden aura
punky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aura
Re: More PHP help (sorry)

Woot! I got 4 now, and i've gone up a level Gotta love being a programmer. Thanx guys
punky is offline   Reply With Quote
Old 29-04-2004, 11:06   #8
Stuartbe
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
  Reply With Quote
Old 29-04-2004, 11:11   #9
Bifta
!
 
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
Bifta has a nice shiny starBifta has a nice shiny star
Bifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny star
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
Bifta is offline   Reply With Quote
Old 29-04-2004, 11:33   #10
punky
Gone
Alpha Bravo Charlie Champion
 
Join Date: Jun 2003
Age: 32
Posts: 14,760
punky has a golden aurapunky has a golden aura
punky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aura
Re: More PHP help (sorry)

punky is offline   Reply With Quote
Old 29-04-2004, 12:48   #11
Bifta
!
 
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
Bifta has a nice shiny starBifta has a nice shiny star
Bifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny star
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.
Bifta is offline   Reply With Quote
Old 29-04-2004, 17:24   #12
punky
Gone
Alpha Bravo Charlie Champion
 
Join Date: Jun 2003
Age: 32
Posts: 14,760
punky has a golden aurapunky has a golden aura
punky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aura
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.
punky is offline   Reply With Quote
Old 29-04-2004, 17:39   #13
Richard M
cf.mega poster
 
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 34
Posts: 6,414
Richard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze array
Richard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze array
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.
Richard M is offline   Reply With Quote
Old 29-04-2004, 18:17   #14
Bifta
!
 
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
Bifta has a nice shiny starBifta has a nice shiny star
Bifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny starBifta has a nice shiny star
Re: More PHP help (sorry)

Thankyou gents, I'll just slot some javascript delays in instead.

Cheers!
Bifta is offline   Reply With Quote
Old 29-04-2004, 18:19   #15
punky
Gone
Alpha Bravo Charlie Champion
 
Join Date: Jun 2003
Age: 32
Posts: 14,760
punky has a golden aurapunky has a golden aura
punky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aura
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
punky is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Google Search




All times are GMT. The time now is 19:07.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Copyright © 2003 - 2012, Cable Forum.
(server5.cableforum.co.uk)

SEO by vBSEO 3.3.2