PDA

View Full Version : Help with writing an array to a .html using fwrite


Jinovich
12-25-2008, 05:11 PM
I have no trouble writing to a file my problem is I can not think of a way to print the array into a variable so I can write it to the file. I can only seem to write one row. Because obviously the variable gets rewrtten in the while loop.

Can someone help me with this I have been struggling with it for sometime.

<?php
include("../config.php");
$get = mysql_query("SELECT * FROM users ORDER BY username ASC");
while($slot = mysql_fetch_array($get))
{
$content = "".$slot[username]." - ".$slot[slots]."<br>";
}
$filename = 'test.html';


// Let's make sure the file exists and is writable first.
//if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $content) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($content) to file ($filename)";

fclose($handle);

//} else {
//echo "The file $filename is not writable";
//}
?>

Oh and merry christmas :)

Dismounted
12-26-2008, 04:51 AM
Find:
$content = "".$slot[username]." - ".$slot[slots]."<br>";
Replace With:
$content .= "".$slot[username]." - ".$slot[slots]."<br>";
It's called concatenation, by the way (and it's a lot to say!).

Jinovich
12-26-2008, 01:10 PM
Find:
$content = "".$slot[username]." - ".$slot[slots]."<br>";
Replace With:
$content .= "".$slot[username]." - ".$slot[slots]."<br>";
It's called concatenation, by the way (and it's a lot to say!).

Cheers! That worked like a charm.