Page 1 of 1

Create JS Array from Text File

Posted: Wed Dec 06, 2006 9:25 am
by bobvaz
Burrito | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello - a newbie--
I am at a loss as to why the following code will not work...
Commenting out the php results in the coded displays.  uncommenting the the php results in a blank page only.....Help, Please.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>aaaaaa</title>
</head>
<body>
<script type="text/javascript">
var jsarr= new Array();
<?php 
$filename="linksfile.txt"; 
$lines = file("linksfile.txt"); 

foreach ($lines as $xxx) {
?>
jsarr[jsarr.length]='<?php echo $xxx?>';
<?php	
}
?>
document.write("This is just a check")
document.write(jsarr[0])
</script>
</body>
</html>

Burrito | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Dec 06, 2006 9:45 am
by Burrito
does your linksfile.txt file contain any single quotes?

Posted: Wed Dec 06, 2006 9:54 am
by Burrito
also, file() sometimes adds a line break at the end of each element of the array. my bet is that your output looks something like this:

Code: Select all

jsarr[jsarr.length] = 'whatever your line is here
';
if that's the case, js will choke and die on it. that can easily be fixed with an str_replace().

check your source to find out.

Posted: Wed Dec 06, 2006 12:30 pm
by feyd
an array_map() + implode() could probably do it pretty easily after file().