-1

我的 sql 表在行中包含脱机访问令牌包含 id 列和访问令牌我想编辑代码以运行 $output 为从 1 到 50 的 ids 然后下一个按钮运行下一个 50 直到全部完成而不是一次全部运行

   <?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>
4

1 回答 1

0
<?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>

奖励:使用PDO而不是 mysql_* 函数。

于 2012-11-14T12:07:56.030 回答