Forum Moderators: coopster

Message Too Old, No Replies

PHP and Javascript problem

         

ramoneguru

7:41 am on May 24, 2005 (gmt 0)

10+ Year Member



I have a file with a lot of website addresses in this format:
[google.com...]
[yahoo.com...]
etc...

In my script I extract these lines using the fgets function and insert them into the database like so:

$handle = fopen("/home/mytext.txt", "r");

while (!feof($handle))
{
$buffer = fgets($handle);
$insertData = mysql_query("INSERT INTO Address (page, result) VALUES ('$buffer', '0')") or die(mysql_error());

}//end while

Now I would like to open all these sites in new windows using javascript like so:

$getUrl = mysql_query("SELECT page FROM Address Where pageId BETWEEN '1' and '12' ");

$url = mysql_fetch_array($getUrl);

while ($url)
{
$newPage = $url[page];
echo "<script language = \"javascript\">
<!--

window.open('$newPage');

// -->
echo </script>";
$url = mysql_fetch_array($getUrl);
}//end while

However, the windows do not open and the page source for the javascript keeps coming up funny. It looks like this:

<script language =" javascript">
<!--
window.open('http://www.google.com/
');

// -->
echo </script>

What is the problem here? I'm thinking its some type of newline or something but I'm not sure. I've tried removing the last element in the string (and a few other tricks) but that has done nothing....Is fgets doing something I'm not aware of?
--Nick

mcibor

8:04 am on May 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you can strip the new line with eg:

$newPage = str_replace('\n', '', $url[page]);
$newPage = str_replace('\r', '', $newPage);

This should get rid of the unnecessary new line.
Hope this helps
Michal Cibor

dcrombie

10:50 am on May 24, 2005 (gmt 0)



$newPage = trim($url["page"]);

;)

gliff

1:05 pm on May 24, 2005 (gmt 0)

10+ Year Member



Yes, it's a newline. When fgets grabs the text, it includes the trailing newline. The trim() [php.net] function will remove "whitespace" characters at the begining and end of a string.

Additionally, if you want to create window.open statments for all the URLs in the database, your looping code is wrong. You want

while($url= mysql_fetch_array($getUrl)){...}

Finally, if you download one of the Mozilla browsers (Firefox, etc.), you can use the built in Javascript console to catch errors in your javascript. In the current Firfox release this feature is under Tools -> Javascript Console

ramoneguru

5:44 pm on May 24, 2005 (gmt 0)

10+ Year Member



Thanks everyone, I'll have to try each of those.
--Nick
PS: man, I love this forum :)