2012-08-14 46 views
0

我不斷收到這樣的警告:不斷得到爲的foreach()提供了無效的論點

警告:這裏 爲的foreach()提供了無效的參數是我的全部代碼performe從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> 
+0

$ tweeps一定不能是數組或對象。嘗試var_dumping它或類似的東西,看看它到底是什麼。 – Telgin 2012-08-14 11:17:39

+0

你能告訴我們什麼$ tweeps包含? – DonSeba 2012-08-14 11:17:44

+1

'$ tweeps'不是數組或對象。你能告訴我們在哪裏定義? – Leri 2012-08-14 11:18:34

回答

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'; 
     } 
    } 
} 
+1

'count()'不起作用。例如。對於字符串,它返回1. – PeeHaa 2012-08-14 11:23:26

+0

我修改了我的帖子再次檢查它,當我用你的代碼修改時,分頁不工作 – LeoSam 2012-08-14 11:31:03

+0

我更新了代碼 – 2012-08-14 11:36:49

0

您的問題很可能是查詢不重新調用任何數據。

您正在將新條目填充到循環內的$tweeps數組中,但如果沒有數據,它將不會進入該循環。

您沒有在其他地方初始化$tweeps變量,所以如果它沒有進入循環,$tweeps將不會是一個數組,它將是未定義的。如果你嘗試在foreach()中使用它,這會給你一個錯誤。

您需要添加一行開始填充它之前初始化變量:

$tweeps = array(); 

這地方去的循環,你先開始填充$tweeps以上。

希望有所幫助。

+0

當我添加數組,在我的循環之前,分頁不能正常工作,它只是拉第一個10 $ tweeps下一頁顯示爲空! – LeoSam 2012-08-14 11:41:17

+0

我想在代碼中還有其他的錯誤。我沒有看那麼遠。上面的答案試圖解決原始問題,這是foreach()中的錯誤。 – SDC 2012-08-14 12:20:04

相關問題