0

我不断收到此警告:

警告:这里为 foreach() 提供的参数无效是我的完整代码,它执行 mysql 的函数列表输出并将其分页每页 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>
4

2 回答 2

1

尝试代码打击。您的 $tweeps 在某些情况下可能不是数组或对象,请检查数组是否不为空并且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';
        }
    }
}
于 2012-08-14T11:17:34.217 回答
0

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

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

您没有$tweeps在其他任何地方初始化变量,因此如果它不进入循环,$tweeps则不会是数组,它将是未定义的。如果您尝试在foreach().

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

$tweeps = array();

这将位于您第一次开始填充的循环上方的某个位置$tweeps

希望有帮助。

于 2012-08-14T11:35:51.547 回答