homepage Welcome to WebmasterWorld Guest from 54.234.59.94
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
setting data to variable
how do you echo a list to a $global
wruk999




msg:1290854
 8:52 pm on Jan 7, 2003 (gmt 0)

Hi,
Just wondering whether anyone can help. I have been sat for a while now, pulling my hair out trying to get this to work.

I have a list of data, which each record I want to print to a new line.
I have got the print option working fine, but I now need to register this list into a variable - $output, for later use in the PHP script.

Here is the script I have:

$dbconn = mysql_connect("localhost", "username", "password");
$result = mysql_select_db("database", $dbconn);

$sql = "SELECT field FROM table ORDER BY field ASC";
$result = mysql_query( $sql );
while ( $data = mysql_fetch_assoc( $result ) )
{
$dom = $data['field'];

echo "$dom</br>";

}

I have tried changing the bottom bit to:
{
$dom = $data['field'];

$output = echo "$dom</br>";

}

and also quite a few other variations..but not working.

anyone know how to do this? Any help VERY much appreciated :)

Kindest Regards,
William

(PS: Register_Globals is ON, if that makes any difference)

 

KMxRetro




msg:1290855
 8:56 pm on Jan 7, 2003 (gmt 0)

Try this:-

$sql = "SELECT field FROM table ORDER BY field ASC";
$result = mysql_query( $sql );
while ($data = mysql_fetch_array( $result ) )
{
$dom = $data['field'];

echo $dom."</br>";
}

jatar_k




msg:1290856
 8:58 pm on Jan 7, 2003 (gmt 0)

try building an array

$output = array();
$counter = 0;
while ($data = mysql_fetch_array($result))
{
$dom = $data['field'];

$output[$counter] = $dom;

echo "$dom</br>";
$counter++;
}

<added>changed the array name to output and thought you might need to initialize

dingman




msg:1290857
 9:04 pm on Jan 7, 2003 (gmt 0)

Why the counter? Shouldn't '$output[] = $dom;' do the same thing? Or am I thinking in Perl?

wruk999




msg:1290858
 9:05 pm on Jan 7, 2003 (gmt 0)

hi KMxRetro,

it prints the list out fine in the browser, but i need the ENTIRE list setting in an $output variable, as it is used in this script below, and needs to print every record on a new line.

$ftp_file = fopen("/www/path/to/file.sh", "w+");
fwrite($ftp_file, $output);
fclose($ftp_file);

trying what you suggested, i changed $output above to $dom, and it printed fine in the browser, but only put one record (the very last alphabetical one) into the .sh file.

any ideas?

jatar_k




msg:1290859
 9:06 pm on Jan 7, 2003 (gmt 0)

hmmm, good question, I don't know if the array immediately knows to add the next value, never really thought about it actually.

When I learned C we always used counters and I never even contemplated the possibility that it would be automatic.

dingman




msg:1290860
 9:18 pm on Jan 7, 2003 (gmt 0)

Try putting '$output_for_file = NULL;' before the loop, and inside the loop, '$output_for_file .= "$dom\n";' When you exit the loop, $output_for_file will have each of those records, one per line, in a longish string variable that you can just print to the text file. (If you want a DOS-formatted text file, use \r\n instead of \n, or \r for a Mac-formatted text file.)

In C you deffinitely can't do the kind of assignment I suggested, but I'm pretty sure you can in PHP and certain that you can in either PHP or Perl.

lorax




msg:1290861
 9:19 pm on Jan 7, 2003 (gmt 0)

You might want to take a look at the array push function:

[php.net...]

dingman




msg:1290862
 9:23 pm on Jan 7, 2003 (gmt 0)

[php.net ]

Yup, I was remembering right. It's equivalent to using array_push().

wruk999




msg:1290863
 9:25 pm on Jan 7, 2003 (gmt 0)

hi dingman,

thanks for your info. this is what i have now:

$result = mysql_query( $sql );
$outputfile = NULL;
$output = array();

while ($data = mysql_fetch_array($result))
{
$dom = $data['field'];

echo "$dom</br>";
}
$outputfile .= "$dom\n";

i think this is wrong, as it is still only writing the very last record into the file?

dingman




msg:1290864
 9:47 pm on Jan 7, 2003 (gmt 0)

Yup. Just move the last line inside the while loop.

wruk999




msg:1290865
 10:06 pm on Jan 7, 2003 (gmt 0)

like this:

while ($data = mysql_fetch_array($result))
{
$dom = $data['Zone'];

echo "$dom</br>";
$outputfile = "$dom\n";
}

still no joy :( it writes to the browser fine, just the one last record tp the text file though.

i have the file bit set now to:

$ftp_file = fopen("/www/path/to/file.sh", "w+");
fwrite($ftp_file, $outputfile);
fclose($ftp_file);

(am i missing something completely obvious?)

dingman




msg:1290866
 10:10 pm on Jan 7, 2003 (gmt 0)

'.=', not '='

You want to add to the string, not replace it.

wruk999




msg:1290867
 10:21 pm on Jan 7, 2003 (gmt 0)

TADA!

its working at last - thanks to EVERYONE, especially dingman..(it was that dot!)

Regards,
william

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved