looping and writing to a file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a function here that will (with some other functions not shown
here) display some results from a database.  Here is the code:

function display_song_list($fd, $e) {
    global $sys_install_base;
    global $sys_relative_uploaddir;

    echo ("<songs>\n");

    foreach ($e as $error) {
        echo ("$error\n");

    $c = pg_numrows($fd['result']);
    for ($i = 0; $i < $c; $i++) {
        $r = pg_fetch_assoc($fd['result'], $i);

        echo ("<song title=\"\" artist=\"\"


    echo ("</songs>");


I need some help writing the info to a text file.  I know the 'fopen'
and 'fwrite' stuff.  But I don't how to write <songs>, and then write
out all of the rows from the database, and then write </songs>.

Thank you for your help.


Re: looping and writing to a file

Aaron Reimann wrote (in part):
Quoted text here. Click to load it

You know you can write those two lines on one line.

       global $sys_install_base, $sys_relative_uploaddir;

Quoted text here. Click to load it

You don't need parenthesis on an echo statement.

Quoted text here. Click to load it

If you know about fopen and fwrite, what's your problem?

$fp = fopen('your.file','w');
fwrite($fp, 'this is a line'."\n");

Just replace your echo statements with fwrite statements and make sure
you end each line with a carriage return ("\n").

BTW, this echo statement:

 echo ("<song title=\"\" artist=\"\"

can be written much more cleanly, something like:

echo '<song title="' . $r['song_title'] .'" artist="' . $r['artist'] .
'" path="' . $sys_install_base . $sys_relative_uploaddir .
$r['audio_file'] . '"/>' . "\n";


Re: looping and writing to a file

Thank you very much.  I didn't know that you could "write" that way.  I
thought I had to get all of the info from the database, put in in a
variable, then write it.

Thank you.

thanks for the other tip too.

Site Timeline