继续获取为foreach()提供的无效参数

时间:2012-08-14 11:15:49

标签: php

我一直收到这个警告:

警告:为foreach()提供的参数无效 这是我的完整代码,从mysql输出performe函数列表,并在每页分页10次输出,错误出现在最后一页

<?php
    require_once('dbconnect.php');

    $yesterday = date("Y-m-d", strtotime("yesterday")); 
    $page=intval($_POST['p']);
    if($page=='')
    {
        $page=1;    
    } 
    $dbadd=($page-1)*10;

    $query = "SELECT * FROM ranking";
    $totalposts=mysql_num_rows(mysql_query($query));
    $totalpages=ceil($totalposts/10);
    $query = "SELECT  r.ranking,r.screenname,r.name,r.followers,r.tweets,r.location,r.`join    date`,r.avatar, h.date, r.followers-h.followers followers_diff, r.tweets-h.tweets tweets_diff  FROM ranking r, ranking_hist h WHERE r.screenname=h.screenname and h.date='$yesterday' AND r.ranking>$dbadd AND r.ranking<($dbadd+11) ORDER BY ranking ASC LIMIT 10 ";

    $result = mysql_query($query);

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

    while($row = mysql_fetch_assoc($result) ) {
    $tweep = $row['screenname']; 
    $tweeps[$tweep] = $row; 
    }

    $query = "SELECT r.ranking,r.screenname,r.name,r.followers,r.tweets,r.location,r.`join date`,r.avatar  FROM ranking r  WHERE r.screenname NOT IN ( SELECT DISTINCT screenname from ranking_hist ) AND r.ranking>$dbadd AND r.ranking<($dbadd+11)  ORDER BY ranking ASC LIMIT 10";

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

     while($row = mysql_fetch_assoc($result) ) {
    echo "";
    $tweep = $row['screenname']; 
    $tweeps[$tweep] = $row; 
     }

     mysql_free_result($result);

     $i = 0;
     $total_amount = count($tweeps);

     foreach  ($tweeps as $tweep) {
         $i++;
         if ($total_amount == $i) {
             $class = 'divrow divrowlast';
         } else {
             $class = 'divrow';
         }

         $col5 = "";
     if( $tweep['followers_diff'] > 0 ) {                   
         $col5 = "<span style='color:green; display:inline;'> +" . $tweep['followers_diff'] . "▲ </span>";
         }
     else if( $tweep['followers_diff'] < 0 ) {
         $col5 = "<span style='color:red; display:inline;'> -" . $tweep['followers_diff'] . "▼ </span>";
         }

         $html_table .=   '<div class="'.$class.'"><ul>' .
             "<li class='row100rank'> " . $tweep['ranking'] . "</li>" .      
             "<li class='row100user'>
          <div class='avatar'><img width='32' height='32' src='" . $tweep['avatar'] . "' alt='" . $tweep['screenname'] ."' /></div>
          <div  class='feature-author'><a class='text_bigger' href='http://www.twitter.com/" . $tweep['screenname'] . "/'>@".$tweep['screenname']."</a></div>
        <div class='row100description'>".$tweep['name']."<br />".$tweep['location']."</div></li>" .
              "<li class='pad'><div class='stat'> <span>" . $tweep['followers'] . " $col5</span> followers</div></li>" .
              "<li class='pad rowTwitte'><div class='stat'> <span>" . $tweep['tweets'] . " $col6</span> tweets</div></li>" .
              "<li class='pad rowJoionDate'><div class='stat'> <span>" . date  ( 'd M y', strtotime($tweep['join date'])) . "</span> joindate</div></li>" .
              "</ul></div>";
          }

      echo $html_table;

?>

<div class="dataTables_paginate paging_full_numbers" ><span   class="first paginate_button" ><a class="pagelinks" href="javascript:void(0);" pageid="1" onClick="changepage(1);">First</a></span>
              <span class="previous paginate_button" ><a class="pagelinks" href="javascript:void(0);" pageid="<?php echo $page>1?($page-1):1;?>" onClick="changepage(<?php echo $page>1?($page-1):1;?>);" >Previous</a></span>

<span>
<?php
    switch($page){
        case 1:
             $it=1;
             $itl=6;
             break;
        case 2:
             $it=1;
             $itl=6;
         break;
        case $totalpages:
         $it=$totalpages-4;
         $itl=$totalpages+1;
            break;
        case $totalpages-1:
         $it=$totalpages-4;
         $itl=$totalpages+1;
         break;
        default:
         $it=$page-2;
         $itl=$page+3;
    }

    for(;$it<$itl;$it++){
?>
  <span class="<?php echo  $page== $it?'paginate_active':'paginate_button'; ?>"> 
      <a class="pagelinks" href="javascript:void(0);" pageid="<?php echo $it;?>" onClick="changepage(<?php echo $it;?>);" ><?php echo $it; ?></a></span>
<?php
   }
?>

  </span>
  <span class="next paginate_button" ><a class="pagelinks" href="javascript:void(0);" pageid="<?php echo $page<$totalpages?($page+1):$page;?>" onClick="changepage(<?php echo $page<$totalpages?($page+1):$page;?>);">Next</a>
  </span>

   <span class="last paginate_button" >
       <a class="pagelinks" href="javascript:void(0);" pageid="<?php echo $totalpages;?>" onClick="changepage(<?php echo $totalpages;?>);">Last</a>
   </span></div>

   <div class="dataTables_info"> 
        Showing <?php echo ($page-1)*10+1;?> to <?php echo ($page-1)*10+10;?> of <?php echo $totalposts;?> 
    </div>

2 个答案:

答案 0 :(得分:1)

尝试代码打击。您的$ tweep可能在某些情况下不是数组或对象,请检查数组是否为空并且is_array($tweeps)

$i = 0;
$total_amount = count($tweeps);
if(is_array($total_amount) && !empty($tweeps))
{
    foreach  ($tweeps as $tweep)
    {
        $i++;
        if ($total_amount == $i)
        {
           $class = 'divrow divrowlast';
        }
        else
        {
           $class = 'divrow';
        }
    }
}

答案 1 :(得分:0)

您的问题很可能是查询没有重新调整任何数据。

您正在将新条目填充到循环内的$tweeps数组中,但如果没有数据,它将不会进入该循环。

你没有在其他任何地方初始化$tweeps变量,所以如果它不进入循环,$tweeps将不是一个数组,它将是未定义的。如果您尝试在foreach()中使用它,则会出现错误。

在开始填充变量之前,您需要添加一行来初始化变量:

$tweeps = array();

这会在首次开始填充$tweeps的循环之上。

希望有所帮助。