-3

I have a PHP code that draws my subscriber count from my YouTube channel and sets it as an integer variable, $subs. I need to use the variable $subs in this piece of HTML code.

HTML Code:

<div class="col-md-6 bottommargin-sm">
    <div class="counter counter-small"><span data-from="100" data-to="$subs" data-refresh-interval="50" data-speed="2000" data-comma="true"></span></div>
        <h5 class="nobottommargin">Subs</h5>
</div>

PHP Code:

<?php
    set_time_limit(0);
    function retrieveContent($url){
        $file = fopen($url,"rb");

        if (!$file) return "";
        while (feof ($file)===false) {
            $line = fgets ($file, 1024);
            $salida .= $line;
        }

        fclose($file);
        return $salida;
    }
        {
            $content = retrieveContent("youtube.com/user/geekawhat/about"); $start = strpos($content,'<span class="about-stat"><b>');
            $end = strpos($content,'</b>',$start+1);
            $output = substr($content,$start,$end-$start);
            $subs = (int)$output;
            echo "$output";
         }
?>

It is a counter, and I want it to count to whatever that variable equals. I have placed the variable in the data-to field, which informs the counter what to count upto - how do I get this to work? (This does work btw with a number inserted into the data-to field)

Panda
  • 6,955
  • 6
  • 40
  • 55

3 Answers3

0

Simply echo it out:

data-to="<?php echo $subs; ?>"

or if you have PHP short tags enabled:

data-to="<?= $subs ?>"

http://php.net/manual/en/function.echo.php

Amous
  • 534
  • 5
  • 18
rjdown
  • 9,162
  • 3
  • 32
  • 45
  • It just shows NaN, I presume I would also lose the animated number count up if it worked anyway? – James Cousins Apr 16 '16 at 22:30
  • 1
    `NaN` is not something PHP would output. That sounds like JavaScript is interfering. – rjdown Apr 16 '16 at 22:33
  • yes NaN it is like JavaScript or Jquery. – Ajay Kumar Apr 16 '16 at 22:39
  • oh right, can javascript or jquery not read a php integer variable? – James Cousins Apr 16 '16 at 22:41
  • Sure it can. How are you reading the value back? jquery's `.data('to')` would automatically convert it to an integer when it's read. On the other hand, if you're using `.attr('data-to')`, it would be read as a string, and you may need to use `parseInt()` on the value first if you want to use it as an integer. Suggest looking at the source code first to see if PHP has inserted the intended value. – rjdown Apr 16 '16 at 22:46
0

You can just echo() it:

Output one or more strings

data-to="<?php echo $subs ?>"

Alternatively, you can also use print():

Output a string

data-to="<?php print $subs ?>"
Panda
  • 6,955
  • 6
  • 40
  • 55
  • It just shows the counter count from 50 to 0 (is it set to start at 0) - showing it cant read the variable value, I presume I would also lose the animated number count up if it worked anyway? – James Cousins Apr 16 '16 at 22:34
  • @JamesCousins Can you upload more code, problem doesn't seem to be in by your current code – Panda Apr 16 '16 at 22:35
  • This is the PHP retrieving the sub count: '); $end = strpos($content,'',$start+1); $output = substr($content,$start,$end-$start); $subs = (int)$output; echo "$output"; } ?> – James Cousins Apr 16 '16 at 22:40
0

use php tag and echo where you want

<div class="col-md-6 bottommargin-sm">
    <div class="counter counter-small">
    <span data-from="100" data-to="<?php echo $subs; ?>" data-refresh-interval="50" data-speed="2000" data-comma="true"></span>
</div>
<h5 class="nobottommargin">Subs</h5>
</div>
Ajay Kumar
  • 1,314
  • 12
  • 22