用$ userslist数组替换$ _GET

时间:2012-08-13 22:14:01

标签: php

我通过$ _GET使用这个脚本将用户名添加到我旁边的数据库到我的twitter列表脚本功能很好
我在url domain.com/add.php?user=username上调用它时的检查 添加执行100%罚款的功能
我的疑问是调整代码以通过数组一次添加多个用户或从文件中获取用户列表 有关调整代码或修改代码的提示吗?

 <?php
session_start();

 require_once('dbconnect.php');
  require_once('twitteroauth/twitteroauth.php');
   require_once('config.php');

   $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,TOKEN_KEY, TOKEN_SECRET);

   $user = $_GET['user'];

 if( !isset( $_GET['user'] ) )
die('You must enter a username');


   $info = $connection->get( 'users/show', array("screen_name"=> $user
                                  ));   
$var = $_GET['user'];



 $individual = 1;

 $protected = ($info->protected == "true")?1:0;

$query = sprintf("INSERT INTO tweeps VALUES('%s', '%s', %s, %s, '%s', '%s', '%s', %s ) ",
mysql_real_escape_string($info->screen_name),
mysql_real_escape_string($info->name),
$info->followers_count,
$info->statuses_count,
mysql_real_escape_string($info->location),
$info->created_at,
$info->profile_image_url,
$protected)    ;


  echo $query;      

$result = mysql_query($query); 
if(false) 
 echo "s";

 else {

$list_id = get_latest_list();
$list = "Tweeps-" . $list_id;

    echo "list $list";

mysql_free_result($result);
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, TOKEN_KEY, TOKEN_SECRET);

$result = $connection->post( 'friendships/create/' .  $info->screen_name    );  
//var_dump($result); die();
    $result = $connection->post( 'lists/members/create', array("screen_name"=>   $info->screen_name,
                                   "slug"=> $list,
                                       "owner_screen_name"=>  "Tweeps")  ); 

if( !isset($result->error) ) {

    $userid = $info->id;
    $result = mysql_query("INSERT INTO followed_tweeps(screenname, userid, individual, list_id) VALUES('" . $info->screen_name . "', '" . $userid . "', $individual, $list_id)");       

}

 }
    echo "SUCCESS";





  function get_latest_list() {

$query = "SELECT count(*) cnt, list_id FROM followed_tweeps group by list_id order by  list_id desc";

$result = mysql_query($query);

if(! $result ) {
    die("Error: query is $query error is: " . mysql_error() );
}

$row = mysql_fetch_row($result);

    $cnt = $row[0];
    $free_slots = 500 - $cnt;   
    $list_id = ($freeslots > 0) ? $row[1]++ : $row[1];
    return $list_id;

  }


  ?>

1 个答案:

答案 0 :(得分:6)

PHP支持多个同名查询参数的数组表示法:

<input type="text" name="name[]" />
                             ^^--- tells PHP to treat 'name' as an array

然后您可以将其处理为:

if (isset($_GET['name']) && isarray($_GET['name'])) {
   foreach ($_GET['name'] as $name) {
      ...
   }
}

无论POST和GET如何,这都有效 - 您只需在字段名称中包含[]