Jump to content

Pagination help


MDanz

Recommended Posts

Hello, i have a pagination problem.

 

                 //echo out $construct
      $construct = "SELECT * FROM Stacks WHERE $construct LIMIT $start, $limit";
}              $run = mysql_query($construct);
                $foundnum = mysql_num_rows($run);

 

I'm trying to apply pagination to this. But i keep getting this error message

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ustackc1/public_html/search.php on line 112 .. line 112 is this ... $foundnum = mysql_num_rows($run);

 

 

if  ($foundnum==0)
echo "<br><br><font color=white>No Stacks Found</font>";
else
{
     echo "<font color=white>$foundnum <img src='http://www.u-stack.com/mini%20stack.jpg'> Found!</font><hr size='1'><p>";

echo '<table '.$margin.'>';
//here you do your loop like

       while ($runrows = mysql_fetch_assoc($run))
     {
                      //get data
              $id = $runrows['id'];
        $name = $runrows['name'];
        $image = $runrows['image'];
        $hyperlink = $runrows['hyperlink'];
        $currency = $runrows['currency'];
        $info = $runrows['info'];
        $type = $runrows['type'];
        $rating = $runrows['rating'];

        echo '<td><tr>';
       echo "<a href='$hyperlink'><img src='http://www.u-stack.com/Stack.jpg'></a>";
       echo "</tr><font color=white><strong>  $name - $info - $rating <a href='rate.php?id=$id'><img src='http://www.u-stack.com/rate.jpg'></a></center><br><br><hr size='1'></strong></font></td>";

            }

       echo '</table>';

}   


  // How many adjacent pages should be shown on each side?
$adjacents = 3;
   

// Your file name  (the name of this file)
$targetpage = "search.php";

// How many items to show per page
$limit = 5;

$page = $_GET['page'];

if($page)
$start = ($page - 1) * $limit;


else
$start = 0;
   


if ($page == 0) $page = 1;   


$prev = $page - 1;


$next = $page + 1;


$lastpage = ceil($foundnum/$limit);


$lpm1 = $lastpage - 1;
   

$pagination = "";
if($lastpage > 1)
{   
$pagination .= "<div class=\"pagination\">";
// Previous
if ($page > 1)
$pagination .= "<a href=\"$targetpage?page=$prev\">« previous</a>";

else
$pagination .= "<span class=\"disabled\">« previous</span>";   
       
// Pages

if ($lastpage < 7 + ($adjacents * 2))
{   
for ($counter = 1; $counter <= $lastpage; $counter++)
{

if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";

else
$pagination .= "<a href=\"$targetpage?page=$counter\">$counter</a>";                   
}
}


elseif($lastpage > 5 + ($adjacents * 2))
{

if($page < 1 + ($adjacents * 2))       
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{

if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";

else
$pagination .= "<a href=\"$targetpage?page=$counter\">$counter</a>";                   
}

$pagination .= "...";
$pagination .= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination .= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
}


elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination .= "<a href=\"$targetpage?page=1\">1</a>";
$pagination .= "<a href=\"$targetpage?page=2\">2</a>";
$pagination .= "...";

for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";

else
$pagination .= "<a href=\"$targetpage?page=$counter\">$counter</a>";                   
}
$pagination .= "...";
$pagination .= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination .= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
}

else
{
$pagination .= "<a href=\"$targetpage?page=1\">1</a>";
$pagination .= "<a href=\"$targetpage?page=2\">2</a>";
$pagination .= "...";

for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{

if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";

else
$pagination .= "<a href=\"$targetpage?page=$counter\">$counter</a>";                   
}
}
}
       
// Next
if ($page < $counter - 1)
$pagination .= "<a href=\"$targetpage?page=$next\">next »</a>";

else
$pagination .= "<span class=\"disabled\">next »</span>";
$pagination .= "</div>\n";       
}


   



?>

 

If you recommend an easier pagination technique to implement than i one i'm using now, i'll be grateful.. i've been stuck on this for days..

Link to comment
https://forums.phpfreaks.com/topic/170924-pagination-help/
Share on other sites

ok i've managed to get some progress.. now it displays 5 results but no links to the next page of results

 

 <?php

mysql_connect("localhost", "Master", "password");
mysql_select_db("Ustack");

$button = $_GET['submit'];
$search = $_GET['search'];

if(!$button){
   echo "<br>You didn't submit a keyword";
}else{
   if(!isset($search) ||  strlen($search)<=2){
      echo "<br><font color=white><br>search term too short</font>";
   }else{
      echo "<br><br><font color=white><br>you searched for <b>$search</b></font><hr size='1'>";
   }

   //explode our search term
   $search_exploded = explode(" ",$search);
   foreach($search_exploded as $search_each){
      //construct query
      $x++;
      if($x==1){
         $construct .= " `keywords` LIKE '%$search_each%'";
      }else{                                          // these both need spaces.
         $construct .= " OR `keywords` LIKE '%$search_each%'";
      }

         $construct = "SELECT * FROM `Stacks` WHERE $construct";
   }
   



   // How many items to show per page
   $limit = 5;

   $page = $_GET['page'];

   if($page){
      $start = ($page - 1) * $limit;
   }else{
      $start = 0;
   }

   $construct = $construct.' LIMIT '.$start.' , '.$limit;
   

   $run = mysql_query($construct);
   $foundnum = mysql_num_rows($run);
   
   echo mysql_error();

    if ($foundnum==0){
      echo "<br><br><font color=white>No Stacks Found</font>";
   }else{
      echo "<font color=white>$foundnum <img src='http://www.u-stack.com/mini%20stack.jpg'> Found!</font><hr size='1'><p>";
      echo '<table '.$margin.'>';
      
      //here you do your loop like
      while ($runrows = mysql_fetch_assoc($run)){
         //get data <== all thi is very not needed.
         $id = $runrows['id'];
         $name = $runrows['name'];
         $image = $runrows['image'];
         $hyperlink = $runrows['hyperlink'];
         $currency = $runrows['currency'];
         $info = $runrows['info'];
         $type = $runrows['type'];
         $rating = $runrows['rating'];

         echo '<td><tr>';
         echo "<a href='$hyperlink'><img src='http://www.u-stack.com/Stack.jpg'></a>";
         echo "</tr><font color=white><strong>  $name - $info - $rating <a href='rate.php?id=$id'><img src='http://www.u-stack.com/rate.jpg'></a></center><br><br><hr size='1'></strong></font></td>";

      }
      echo '</table>';
    }   

   // How many adjacent pages should be shown on each side?
   $adjacents = 3;
       
    // Your file name  (the name of this file)
   $targetpage = "search.php";

   if ($page == 0){
      $page = 1;
   }

   $prev = $page - 1;

   $next = $page + 1;

   $lastpage = ceil($foundnum/$limit);

   $lpm1 = $lastpage - 1;

   $pagination = "";
   if($lastpage > 1){   
      $pagination .= "<div class=\"pagination\">";
      // Previous
      if ($page > 1){
         $pagination .= "<a href=\"$targetpage?page=$prev\">« previous</a>";
      }else{
         $pagination .= "<span class=\"disabled\">« previous</span>";
      }

      // Pages

      if ($lastpage < 7 + ($adjacents * 2)){   
         for ($counter = 1; $counter <= $lastpage; $counter++){
            if ($counter == $page){
               $pagination .= "<span class=\"current\">$counter</span>";
            }else{
               $pagination .= "<a href=\"$targetpage?page=$counter\">$counter</a>";                   
            }
         }
      }elseif($lastpage > 5 + ($adjacents * 2)){
         if($page < 1 + ($adjacents * 2)){
            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
               if ($counter == $page){
                  $pagination .= "<span class=\"current\">$counter</span>";
               }else{
                  $pagination .= "<a href=\"$targetpage?page=$counter\">$counter</a>";
               }
            }
         }
      }            
      $pagination .= "...";
      $pagination .= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
      $pagination .= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
   }elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)){
      $pagination .= "<a href=\"$targetpage?page=1\">1</a>";
      $pagination .= "<a href=\"$targetpage?page=2\">2</a>";
      $pagination .= "...";

      for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++){
         if ($counter == $page){
            $pagination .= "<span class=\"current\">$counter</span>";
         }else{
            $pagination .= "<a href=\"$targetpage?page=$counter\">$counter</a>";                   
         }
      }
      $pagination .= "...";
      $pagination .= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
      $pagination .= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
    }else{
      $pagination .= "<a href=\"$targetpage?page=1\">1</a>";
      $pagination .= "<a href=\"$targetpage?page=2\">2</a>";
      $pagination .= "...";

      for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++){
         if ($counter == $page){
            $pagination .= "<span class=\"current\">$counter</span>";
         }else{
            $pagination .= "<a href=\"$targetpage?page=$counter\">$counter</a>";
         }
      }
   }
           
   // Next
   if ($page < $counter - 1){
       $pagination .= "<a href=\"$targetpage?page=$next\">next »</a>";
   }else{
      $pagination .= "<span class=\"disabled\">next »</span>";
      $pagination .= "</div>\n";       
    }
}
    



?>

Link to comment
https://forums.phpfreaks.com/topic/170924-pagination-help/#findComment-901924
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.