0

I need help with putting this $id value into the javascript below

PHP:

<?php

$id = NULL;
$username = 'YouTube';

$xml = simplexml_load_file(sprintf('http://gdata.youtube.com/feeds/base/users/%s/uploads?alt=rss&v=2&orderby=published', $username));

if ( ! empty($xml->channel->item[0]->link) )
{
  parse_str(parse_url($xml->channel->item[0]->link, PHP_URL_QUERY), $url_query);

  if ( ! empty($url_query['v']) )
    $id = $url_query['v'];
}

echo $id; // Outputs the video ID.
    ?>

JS: Need $id value ---> 'I need the value to go right here'

<script type="text/javascript">
   $('document').ready(function() {
    var options = { videoId: 'I need the value to go right here', start: 3 };
    $('#video1').tubular(options);
});
</script>
5
  • 4
    whats the problem? why not <?php echo $id; ?> in the javascript? Commented Oct 10, 2012 at 9:32
  • IT WORKED! Sorry i'm a noob and new to all of this. THANK YOU!!! EaterOfCorpses Commented Oct 10, 2012 at 9:35
  • it's okay :) everbody needs to learn it some time :) Commented Oct 10, 2012 at 9:37
  • Is it ok to do a follow up question lol.. going to do it anyway, the $id retrieval works perfectly, but is there a way to ensure that the latest value it gets from YouTube (source) is the latest? Cause it doesn't seem to be retrieving the latest video, it shows the 2nd to last which is nice too - but 1st would be good. - Thanks in advance again! Commented Oct 10, 2012 at 10:03
  • I think its better to make a new question for that and accept kalpesh's answer :) Commented Oct 10, 2012 at 10:46

3 Answers 3

2

Do something like this....

<script type="text/javascript">
   $('document').ready(function() {
    var options = { videoId: '<?php echo $id?>', start: 3 };
    $('#video1').tubular(options);
});
</script>

But make sure, you are including this script in php file.

Other solution could be to use html hidden variable, and access that value using js.

Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm! Basically its retrieving the latest video from a youtube channel, only pain is that some videos have embedding disabled = video doesn't show :( not sure there's a way around that!
1

by the use of

<script type="text/javascript">
  ....
</script>

I understand you're using javascript inside of your view! then why not just do this

var options = { videoId: '<?php echo $id; ?>', start: 3 };

Comments

0

Use

<script type="text/javascript">
 var id= '<?= $id; ?>';
</script>

now you can use the id variable in your javascript

<script type="text/javascript">
   $('document').ready(function() {
    var options = { videoId: id, start: 3 }; // id variable which is filled by $ib variable of php
    $('#video1').tubular(options);
});
</script>

2 Comments

uhm why create a new javascript block :( will slowdown slow pc's
its better to get all the php variable you want in one place and also the script in which OP require the variable can be a javascript file so there no php will be available so we can use the block on the page we are including the external javascript file

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.