php mysql javascript, json create javascript aray object for dynamic page

I am converting a static site to a dynamice site and have an issue with a javascript array. I using php mysql to get the content from the database and all is well there but I need to write a javascript array data object, data=[[...................]] , and Iam having trouble. I query the database and fetch the php mysql array and use json to encode the array and write the object out to the page. The data does not format properly and of course the images and links I am reading from the database are not found. FYI the JS code is a scroller that works fine in static mode where the paths and images are define in the page code.
Here below is the code being used to create the JS object...

Get query with success.......
$image_name=array();

while( $r = mysql_fetch_assoc($result) ) {
      $image_name[] = $imageurl.$r['image_name'].'",'.'"Click to view",'.'"http://url/main.html"'.']';             }
           echo json_encode( $image_name );         ?>;

the static code looks as shown below, the code the above script is writing is backslashed and has many formatting problems such as missing "[" and to many double quoates in the wrong places.
data=[
["http://url/images/bannerimages/image.jpg","Click to view","http://url/main.html"],


If anyone has a clean fix for me please pass it on, Im in a hurry on this one and do not want to re invent the wheel, please stick with a solution that fits where we are unless you have an easy and elegant alternative.
Thanks
Ron McCainAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcnuteCommented:
$image_name[] = $imageurl.$r['image_name'].'",'.'"Click to view",'.'"http://url/main.html"'.']';  

as long as the part "Click to view" is no php you don't need to add the full stops here. Just leave it as a string like this:

$image_name[] = $imageurl.$r['image_name'].'","Click to view","http://url/main.html";  

Not sure even if you need to quote out the first variable, too.
Try to clean up a little bit and make a 4,5 times trial and error testing until you get the formatting you want. Nobody can provide you clean code without having your testing environment available for testing.

0
Ron McCainAuthor Commented:
understood, what I am trying to do is to duplicate a working JS array formated as shown but writ it out dynamically. I know I am not explaining this very well?
I have have tried every trick I can think of. thanks for the response
0
leakim971PluritechnicianCommented:
could you show us  the working JS array formated ? Or provide a link to see your page(s) ?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ron McCainAuthor Commented:
thanks for the response , as far as a link, the source is on our local dev servers as we are mid project on this so I will try again to show what we have below.
I did manage to write out the JS data array ()  without json by doing the following not so elegant script but it works, the last issue I need to fix as usual is the last comma in the array when all we need is the closing brackets .....i.e. WE END UP WITH  ],]  INSTEAD OF ]]  at the close of the array()..............
NOTE: I am sure I waas the problem with the jsan_encode() trouble but I had to move on because of time issues and although the code below works I would like to change it to use json as soon as I have a bit more time to work on it or someone can help.

using this code to write the JS data[] array into the page just before the JS code uses it.
NOTE: the JS array I needed to duplicate is shown after this script snippet but has the urls obscured.

data=[<?php
//===get database stuff======
$imagename=array();
while( $r = mysql_fetch_assoc($result) ) {
      echo $imagename[] = '['.'"'.$imageurl.$r['image_name'].'"'.",".'"Click to view"'.','.'"'.$brandurl ;        
    }
                echo ']'   ;
?>;

=======================================

//JS array()
//array content from database

data=[["http://www.URL/images/bannerimages/image1.jpg","Click to view","http://www.URL/library/main.html"],
["http://www.URL/images/bannerimages/image2.jpg","Click to view","http://www.URL/library/main.html"]]
of course the array is much larger and has acctual path names etc.

Thanks again for the efforts


0
leakim971PluritechnicianCommented:
in your previous code you just create yourself the sub set of arrays instead create true array. so the array is not array but string and json_encode encode square brack as string

$image_name = array();

while( $r = mysql_fetch_assoc($result) ) {
      $subarray = array($imageurl.$r['image_name'], "Click to view", "http://url/main.html");      
      $image_name[] = $subarray;
}

echo json_encode( $image_name );         

?>; 

Open in new window

0
leakim971PluritechnicianCommented:
$image_name = array();

while( $r = mysql_fetch_assoc($result) ) {
      $subarray = array($imageurl.$r['image_name'], "Click to view", $brandurl);      
      $image_name[] = $subarray;
}

// header('Content-type: application/json');
echo json_encode( $image_name );         

?>; 

Open in new window

0
Ron McCainAuthor Commented:
Thanks I tried your code and get only half of the data set I need as image_name is only part, the  manual write effort works but I need to remove the comma .  Thanks for the assist, with my manual code what is the best method you seee to  trim the last comma that must be removed at the array close, currently the array ends with ......data ],]; and it needs to be ......data ]]; (ie no last comma.
0
leakim971PluritechnicianCommented:
try this :
$imagename=array();
$str = "";
while( $r = mysql_fetch_assoc($result) ) {
      $str .=  '["' . $imageurl . $r['image_name'] . '"," . '"Click to view"' . ',"' . $brandurl . '"],';         
}
echo substr($str,0,-1) . ']';

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ron McCainAuthor Commented:
good job
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.