3

I am using jPlayer to display some videos and I create the playlist on the fly in a php foreach statement:

var playlistacting = 
            [
        <?php foreach($this->result as $val){?>
                {
                    title:  '<?php echo $val->getTitle();?>',
                    artist: '<?php echo $val->getDes();?>',
                    poster: "<?php echo $val->getVideoId();?>.jpg",
                    thumb:  "<?php echo $val->getVideoId();?>.jpg",
                    flv:    "<?php echo $val->getVideoId();?>.flv",
                },
        <?php }?>
];

and $val->getDes() example would be I have a one-hour (approximately) solo musical revue called "Manhattan With A Twist". The entire...

the error I get is

unterminated string literal
[Break On This Error]   

artist: 'I have a one-hour (approximately)solo musical revue called "Manhattan W...

jquery.js (line 2, col 32)

and is poining to the ' at the beginning of the string.

I can do this: title: "<?php echo htmlspecialchars($val->getTitle());?>",

and I will get the same error but with " instead of '.

I'm not sure what is happening here. Is it complaining about that ' not being escaped or what?

Any ideas?

1

1 Answer 1

4

simply use json_encode:

var playlistacting = <?php echo json_encode($this->result);?>;
Sign up to request clarification or add additional context in comments.

7 Comments

i can see using it on a array, but why is working on this string
It's not working on a string, it's working on an array/object that contains strings.
I don't know why this worked. json_encode() is the right answer, but from what you've shown us of your result object, it won't produce all the right data just with a single call to json_encode(). I would expect you'd need to build a PHP array first from $this->result into the format you want, and json_encode() that.
@Spudley: that's correct, the file-extensions are missing. But they seem to be static, so they also may be appended in javascript later when they are needed.
@Spudley it works perfect, the text comes back with all quotes and everything.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.