Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Trouble converting php array to javascript
Old 12-13-2008, 08:43 AM Trouble converting php array to javascript
Sleeping Troll's Avatar
Ultra Talker

Posts: 351
Name: Butch Begy
Trades: 0
I am using the following to convert a php array to javascript.
PHP Code:
var TextsArray = new Array(<?php
              
foreach($TextsArray as $Text)
                  {
                   echo 
"'$Text',";
                  }
              
?>);
The output of this statement is:

Code:
 
var TextsArray = new Array('Your Text Here',);
This generated line creates a syntax error, is it because of the single quotes???
Sleeping Troll is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-13-2008, 09:49 AM Re: Trouble converting php array to javascript
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
No, it's because of the comma at the end. Place the comma at the beginning of the string in your PHP logic, but don't do it on the first iteration.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 12-13-2008, 09:52 AM Re: Trouble converting php array to javascript
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You need to check if it is at the upper bound of the array and only insert the comma if it is not.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-14-2008, 03:49 AM Re: Trouble converting php array to javascript
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
PHP Code:
var TextsArray = new Array(<?php
              $comma 
false;
              foreach(
$TextsArray as $Text)
                  {
                   echo ((
$comma)?",":"")."'$Text'";
                   
$comma true;
                  }
              
?>);
Pretty obvious and maybe an unnessecary addition to this topic.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Trouble converting php array to javascript
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13646 seconds with 12 queries