dtla92562 2012-11-14 11:48
浏览 12
已采纳

too long

my sql table contain in row offline access token contain id column and access token i want to edit code to run $output for ids from 1 to 50 then a next button to run next 50 until all finished instead of run all in one time

   <?php

require './config.php';
require './facebook.php';

//connect to mysql database
mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_name);
mysql_query("SET NAMES utf8");

//Create facebook application instance.
$facebook = new Facebook(array(
  'appId'  => $fb_app_id,
  'secret' => $fb_secret
));

$output = '';

//if form below is posted- 
//lets try to send wallposts to users walls, 
//which have given us a access_token for that
if(isset($_POST['send_messages'])){

    //default message/post
    $msg =  array(
        'message' => 'date: ' . date('Y-m-d') . ' time: ' . date('H:i')
    );

    //construct the message/post by posted data
    if(isset($_POST['message'])){
        $msg['message'] = $_POST['message'];
    }
    if(isset($_POST['url']) && $_POST['url'] != 'http://'){
        $msg['link'] = $_POST['url'];
    }
    if(isset($_POST['picture_url']) && $_POST['picture_url'] != ''){
        $msg['picture'] = $_POST['picture_url'];
    }

    //get users and try posting our message/post
    $result = mysql_query("
        SELECT
            *
        FROM
            offline_access_users
    ");

    if($result){
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
            $msg['access_token'] = $row['access_token'];
            try {
                $facebook->api('/me/feed', 'POST', $msg);
                $output .= "<p>Posting message on '". $row['name'] . "' wall success</p>";
            } catch (FacebookApiException $e) {
                $output .= "<p>Posting message on '". $row['name'] . "' wall failed</p>";
            }
        }
    }
}


?><!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="et" lang="en">
    <head>
        <title>Batch posting</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style type="text/css">
            body { font-family:Verdana,"Lucida Grande",Lucida,sans-serif; font-size: 12px}
        </style>
    </head>
    <body>
        <h1>Batch posting</h1>
        <form method="post" action="">
            <p>Link url: <br /><input type="text" value="http://" size="60" name="url" /></p>
            <p>Picture url: <br /><input type="text" value="" size="60" name="picture_url" /></p>
            <p>Message: <br /><textarea type="text" value="" cols="160" rows="6" name="message" />Message here</textarea></p>
            <p><input type="submit" value="Send message to users walls" name="send_messages" /></p>
        </form>
        <?php echo $output; ?>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • dongzhent208577 2012-11-14 12:07
    关注
    <?php
    
    require './config.php';
    require './facebook.php';
    
    //connect to mysql database
    mysql_connect($db_host, $db_username, $db_password);
    mysql_select_db($db_name);
    mysql_query("SET NAMES utf8");
    
    //Create facebook application instance.
    $facebook = new Facebook(array(
      'appId'  => $fb_app_id,
      'secret' => $fb_secret
    ));
    
    $output     = '';
    $isHaveNext = false;
    
    if (isset($_POST['send_messages'])) {
    
        //default message/post
        $msg = array(
            'message' => 'date: ' . date('Y-m-d') . ' time: ' . date('H:i')
        );
    
        //construct the message/post by posted data
        if (isset($_POST['message'])) {
            $msg['message'] = $_POST['message'];
        }
        if (isset($_POST['url']) && $_POST['url'] != 'http://') {
            $msg['link'] = $_POST['url'];
        }
        if (isset($_POST['picture_url']) && $_POST['picture_url'] != '') {
            $msg['picture'] = $_POST['picture_url'];
        }
    
        // get offset
        $offset = isset($_POST['offset']) ? intval($_POST['offset']) : 0;
    
        // get total rows count
        $query = mysql_query("SELECT COUNT(1) FROM offline_access_users LIMIT 1");
        $rows  = mysql_fetch_array($query);
        $total = $rows[0];
    
        //get users and try posting our message/post
        $result = mysql_query("SELECT * FROM offline_access_users LIMIT 50 OFFSET $offset");
    
        if ($result) {
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $msg['access_token'] = $row['access_token'];
                try {
                    $facebook->api('/me/feed', 'POST', $msg);
                    $output .= "<p>Posting message on '" . $row['name'] . "' wall success</p>";
                } catch (FacebookApiException $e) {
                    $output .= "<p>Posting message on '" . $row['name'] . "' wall failed</p>";
                }
            }
        }
    
        // next
        if ( $total > ($offset + 50) ) {
            $isHaveNext = true;
            $offset += 50;
            ?>
            <form action="" method="post">
                <input type="hidden" name="message" value="<?php echo $_POST['message'] ?>">
                <input type="hidden" name="url" value="<?php echo $_POST['url'] ?>">
                <input type="hidden" name="picture_url" value="<?php echo $_POST['picture_url'] ?>">
                <input type="hidden" name="offset" value="<?php echo $offset ?>">
                <input type="submit" name="send_messages" value="Next">
            </form>
            <?php
            echo $output;
        }
    }
    
    if ($isHaveNext) exit(1);
    
    ?>
    <!DOCTYPE html 
        PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="et" lang="en">
        <head>
            <title>Batch posting</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <style type="text/css">
                body { font-family:Verdana,"Lucida Grande",Lucida,sans-serif; font-size: 12px}
            </style>
        </head>
        <body>
            <h1>Batch posting</h1>
            <form method="post" action="">
                <p>Link url: <br /><input type="text" value="http://" size="60" name="url" /></p>
                <p>Picture url: <br /><input type="text" value="" size="60" name="picture_url" /></p>
                <p>Message: <br /><textarea type="text" value="" cols="160" rows="6" name="message" />Message here</textarea></p>
                <p><input type="submit" value="Send message to users walls" name="send_messages" /></p>
            </form>
    <?php echo $output; ?>
        </body>
    </html>
    

    Bonus: Use PDO instead of mysql_* functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题