【问题标题】:How to insert a variable from php into p tag (html)?如何将 php 中的变量插入 p 标签(html)?
【发布时间】:2017-10-09 10:02:46
【问题描述】:

我正在尝试让我的 twitter 关注者数量显示在我的网站上。我已经设置了 twitter API 来接收关注者数量,并且已经使用 echo 进行了测试,并且运行良好。

问题是我不知道如何将该值传递给 html。

这是获取推特计数的代码:

<?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'];

?>

这是将 $followers_count 变量传递到 html 的代码。

这是一个 ID 为“twit-follow-count”的跨度标签。

<script type="text/javascript">

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

</script>

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript php jquery html twitter


    【解决方案1】:
    <script type="text/javascript">
    
    $( document ).ready(function() {
        $('#twit-follow-count').html('<?php echo $followers_count; ?>');
    });
    
    </script>
    

    【讨论】:

    • 是的。这就是我会做的!
    • ?我投了赞成票。这就是我能做的。我没有问这个问题。
    • 抱歉,没看昵称。
    • 抱歉没用。 echo 会弹出正确的数字,但不是 p 标签。
    • 你可以使用 PHP 插入这个值,不需要真正使用 javascript。
    猜你喜欢
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 2023-03-28
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多