Keep getting Invalid argument supplied for foreach() in
<p>I keep getting this warning:</p>

<p>Warning: Invalid argument supplied for foreach() in
here is my full code which performe function list output from mysql and paging it 10 output per page , the error appear in last page</p>

<pre><code>&lt;?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&gt;$dbadd AND r.ranking&lt;($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&gt;$dbadd AND r.ranking&lt;($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'] &gt; 0 ) {                   
         $col5 = "&lt;span style='color:green; display:inline;'&gt; +" . $tweep['followers_diff'] . "▲ &lt;/span&gt;";
         }
     else if( $tweep['followers_diff'] &lt; 0 ) {
         $col5 = "&lt;span style='color:red; display:inline;'&gt; -" . $tweep['followers_diff'] . "▼ &lt;/span&gt;";
         }

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

      echo $html_table;

?&gt;

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

&lt;span&gt;
&lt;?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&lt;$itl;$it++){
?&gt;
  &lt;span class="&lt;?php echo  $page== $it?'paginate_active':'paginate_button'; ?&gt;"&gt; 
      &lt;a class="pagelinks" href="javascript:void(0);" pageid="&lt;?php echo $it;?&gt;" onClick="changepage(&lt;?php echo $it;?&gt;);" &gt;&lt;?php echo $it; ?&gt;&lt;/a&gt;&lt;/span&gt;
&lt;?php
   }
?&gt;

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

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

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