简体   繁体   中英

How to insert a variable from php into p tag (html)?

I am trying to get my twitter follower count to show on my website. I have set up the twitter API to receive the follower count, and that has been tested using echo and that is working perfectly.

The problem is I can't work out how to pass that value into the html.

This is the code that gets the twitter count:

<?php 

/* 
 * Requires the "Twitter API" wrapper by James Mallison 
 * to be found at https://github.com/J7mbo/twitter-api-php
 *
 * The way how to get a follower count was posted by Amal Murali
 * on http://stackoverflow.com/questions/17409227/follower-count-number-in-twitter
 */

require_once('twitterapi/TwitterAPIExchange.php');              // adjust server path accordingly

// GET YOUR TOKENS AND KEYS at https://dev.twitter.com/apps/
$settings = array(
'oauth_access_token' => "SECRET",               // enter your data here
'oauth_access_token_secret' => "SECRET",        // enter your data here
'consumer_key' => "SECRET",                 // enter your data here
'consumer_secret' => "SECRET"               // enter your data here
);

$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=SECRET';                  // enter your twitter name without the "@" here
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($ta_url, $requestMethod)
->performRequest();
$data = json_decode($follow_count, true);
$followers_count = $data[0]['user']['followers_count'];

?>

And this is the code to pass the $followers_count variable into the html.

It's a span tag with the id "twit-follow-count".

<script type="text/javascript">

$( document ).ready(function() {
    $('#twit-follow-count').html($followers_count);
});

</script>

Any help would be greatly appreciated!

<script type="text/javascript">

$( document ).ready(function() {
    $('#twit-follow-count').html('<?php echo $followers_count; ?>');
});

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM