Jump to content

Append A Javascript Array


xjx424
 Share

Recommended Posts

Hi, I am trying to append a javascript file "image_list.js" which contains an array like this:

var tinyMCEImageList = new Array(	["Robot", "media/robot.jpg"],	["Logo", "media/logo_over.jpg"]);

I can append the file using this:

$File = "lists/image_list.js";$Handle = fopen($File, 'a');$Data = "New Data Here";fwrite($Handle, $Data);fclose($Handle);

I just cannot figure out how to add another entry into the arraychanging it to something like this:

var tinyMCEImageList = new Array(	["Robot", "media/robot.jpg"],	["Logo", "media/logo_over.jpg"],        ["Animals", "media/animals.jpg"],        ["Cookies", "media/cookies.jpg"]);

any help is appreciated.Thanks

Link to comment
Share on other sites

Append won't work. You have that last line to deal with.Try reading the file into a string. Chop the last two characters off the end of the string. Add your new data to the end of the string. Add ");" to the end of the string. Write the string to the file, overwriting what was already there.

Link to comment
Share on other sites

That's pretty much what I came up with. Works well!

$File = "lists/image_list.js";$content1 =  implode("",file($File));$content2 = str_replace(");","",$content1);$content = str_replace("\r\n","",$content2);$new_content = "".$content.",\n[\"".$_POST['heading']."\", \"".$file_path."\"]);";$fp = fopen($File,"w");fputs($fp,$new_content);fclose($fp);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...