Jump to content

pagination help if page number dont exist


lovephp
Go to solution Solved by QuickOldCar,

Recommended Posts

friends if a page number does not exist how do i get the user back to page=1?

 

here is my following code

 

<table class="table" width="100%">
            <tbody>
            <tr class="top nodrop nodrag">
              <th class="checkbox" style="width: 12px;"></th>
              <th>Image/Video</th>
              <th>Title/Description</th>
              <th class="action">Action</th>
            </tr>
            <?php
            
            $allRecords = mysql_query('select * from article');
            $total_rows = mysql_num_rows($allRecords);
            $base_url = 'https://localhost/pagi/';   
              $per_page = 1;                          
              $num_links = 4;                         
              $total_rows = $total_rows;
              $cur_page = 1;         
 
    if(isset($_GET['page']))
    {
      $cur_page = $_GET['page'];
      $cur_page = ($cur_page < 1)? 1 : $cur_page;            
    }
    
     $offset = ($cur_page-1)*$per_page;                
   
    $pages = ceil($total_rows/$per_page);
    $start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
    $end   = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;
    
      $res = mysql_query("SELECT * FROM article LIMIT ".$per_page." OFFSET ".$offset);
            if(is_resource($allRecords))
            {
                while($row = mysql_fetch_assoc($res))
                {
                $img = $row['image'];
                $yt = $row['youtube'];
                if(!empty($img)){
                $img = '<img src="../uploads/images/'.$row['image'].'" height="120" width="120" />';
                }elseif(!empty($yt)){
                $img = '';
                }else{
                $img = '<img src="../uploads/images/noimg.png" height="120" width="120" />';
                }                
                if(!empty($yt)){
                $yt = '<img src="../uploads/images/youtube.png" height="120" width="120" />';
                }else{
                $yt = '';
                }
                    ?>
                    <tr>
                        <td class="checkbox"><input type="checkbox"  value="<?php echo $row['id'];?>" name="ids[]" class="case"></td>
                        <td><?php echo $img.$yt; ?></td>
                        <td><a href="<?php echo $site_path.$row['url']; ?>" target="_blank"><?php echo ucfirst($row['title']); ?></a><br/><?php echo shortenString($row['article']); ?></td>                                            
                        <td class="action"><a href="post-article.php?id=<?php echo $row['id']; ?>"><img src="media/layout/edit.png" alt="Edit" /></a><br/><a href="javascript:delete_id(<?php echo $row['id']; ?>)"><img src="media/layout/x.png" alt="Delete" /></a></td>
                    </tr>
                    <?php
                }
            }
            ?>
            <tr>
              <td> </td>
              <td class="checkbox"  colspan="4"><br/><br/>
              <select name="action" class="select" style="width:143px;">
              <option selected="selected" disabled="disabled">Choose an action</option>
              <option value="delete">Delete</option>
              </select>
              
              <input type="submit" name="Go" class="submit" value="Apply action">
              </td>
            </tr>
          </tbody>
</table>

 <div id="pagination">
        <div id="pagiCount">
            <?php
                if(isset($pages))
                {  
                    if($pages > 1)        
                    {    if($cur_page > $num_links)    
                        {   $dir = "first";
                            echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.(1).'">'.$dir.'</a> </span>';
                        }
                       if($cur_page > 1)
                        {
                            $dir = "prev";
                            echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page-1).'">'.$dir.'</a> </span>';
                        }                 
                        
                        for($x=$start ; $x<=$end ;$x++)
                        {
                            
                            echo ($x == $cur_page) ? '<strong>'.$x.'</strong> ':'<a href="'.$_SERVER['PHP_SELF'].'?page='.$x.'">'.$x.'</a> ';
                        }
                        if($cur_page < $pages )
                        {   $dir = "next";
                            echo '<span id="next"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page+1).'">'.$dir.'</a> </span>';
                        }
                        if($cur_page < ($pages-$num_links) )
                        {   $dir = "last";
                       
                            echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$pages.'">'.$dir.'</a> ';
                        }   
                    }
                }
            ?>

 

thanks in advance appreciate your time

Link to comment
Share on other sites

is this the right way?

 

 $allRecords = mysql_query('select * from article');

         $total_rows = mysql_num_rows($allRecords);
         $base_url = 'https://localhost/pagi/';   
         $per_page = 1;                          
         $num_links = 4;                         
         $total_rows = $total_rows;
         if($total_rows<1){
         header("Location:article.php?page=1");
         }
         $cur_page = 1;

Link to comment
Share on other sites

nope

$total_rows = mysql_num_rows($allRecords);
            //$base_url = 'https://localhost/pagi/';    //Provide location of you index file  
  $per_page = 1;                           //number of results to shown per page
  $num_links = 4;                           // how many links you want to show
  $total_rows = $total_rows;
  if(empty($total_rows)){
    header("Location:articles.php?page=1");
    exit();
   }
  $cur_page = 1;     

 

this did not work :)

Edited by lovephp
Link to comment
Share on other sites

  • Solution

You would want to do the row count for this query

 

$res = mysql_query("SELECT * FROM article LIMIT ".$per_page." OFFSET ".$offset);

 

The $allrecords is being used to create pagination links all records with no offset or limit.

 

Also check row like this...a number.

if(mysql_num_rows($res) < 1){
header("Location: https://localhost/pagi/articles.php");
exit;
}

Can link back to the main script since you already set page to 1 if not set

  • Like 1
Link to comment
Share on other sites

You would want to do the row count for this query

 

$res = mysql_query("SELECT * FROM article LIMIT ".$per_page." OFFSET ".$offset);

 

The $allrecords is being used to create pagination links all records with no offset or limit.

 

Also check row like this...a number.

if(mysql_num_rows($res) < 1){
header("Location: https://localhost/pagi/articles.php");
exit;
}

Can link back to the main script since you already set page to 1 if not set

thanks alot bro works like a charm :)

Link to comment
Share on other sites

You are using Mysql code that has been removed from Php. You need to use PDO with prepared statements. Here is a really good tutorial (Posted by @Jaques1) https://phpdelusions.net/pdo

but me do not like PDO i still use php, will this matter in near future? no still not familiar with PDO though i understand how its done still php is far better for me

Link to comment
Share on other sites

PHP is totally different different. PDO is a database wrapper allowing you to connect and manipulate a database. Similar to mysql or mysqli.

 

Mysql functions are deprecated and removed in new versions of php.

Edited by QuickOldCar
  • Like 1
Link to comment
Share on other sites

The old MySQL extension is dead and has been removed from the current PHP version. You had 12(!) years to switch to one of the modern extensions, so, really, it's about time.

 

And it's not only about keeping your code up-to-date. The old extension is notorious for causing SQL injection vulnerabilities, because most programmers simply don't understand the concept the SQL-escaping. PDO fixed that by introducing prepared statements.

 

I understand that PDO looks a bit different, but it's very easy to learn with the above tutorial, and it actually makes a lot of sense.

  • Prepared statements are a major improvement with regards to security.
  • Instead of having to constantly write down the clunky while ($row = mysql_fetch_(...)) pattern, you now now use a simple foreach loop to iterate over rows.
  • PDO works with all mainstream SQL systems, not just MySQL. That means you won't have to re-learn everything if you switch to a different systems (yes, this can happen).
  • Like 1
Link to comment
Share on other sites

PHP is totally different different. PDO is a database wrapper allowing you to connect and manipulate a database. Similar to mysql or mysqli.

 

Mysql functions are deprecated and removed in new versions of php.

guess ill have to give a serious thought on ;earning PDO

Link to comment
Share on other sites

 

The old MySQL extension is dead and has been removed from the current PHP version. You had 12(!) years to switch to one of the modern extensions, so, really, it's about time.

 

And it's not only about keeping your code up-to-date. The old extension is notorious for causing SQL injection vulnerabilities, because most programmers simply don't understand the concept the SQL-escaping. PDO fixed that by introducing prepared statements.

 

I understand that PDO looks a bit different, but it's very easy to learn with the above tutorial, and it actually makes a lot of sense.

  • Prepared statements are a major improvement with regards to security.
  • Instead of having to constantly write down the clunky while ($row = mysql_fetch_(...)) pattern, you now now use a simple foreach loop to iterate over rows.
  • PDO works with all mainstream SQL systems, not just MySQL. That means you won't have to re-learn everything if you switch to a different systems (yes, this can happen).

 

will soon get me a book on PDO :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.