Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Put pagination recordset inside a function

Community Beginner ,
Feb 25, 2013 Feb 25, 2013

Hi, I need some help if its possible.

I'm using Dreamweaver's PHP code for selecting data from database and outputing them in pages ( Pagination ). I thought to create a very general record set and then put it in a function so I can re-use it,  and change only things like the query and the $maxRows_. So it looks like this:

<?php

$maxRows_sql = 10; // per page

$pageNum_sql = 0;

if (isset($_GET['pageNum_sql'])) {

  $pageNum_sql = $_GET['pageNum_sql'];

}

$startRow_sql = $pageNum_sql * $maxRows_sql;

$query_sql = "SELECT * FROM `database_table` ";

$query_limit_sql = sprintf("%s LIMIT %d, %d", $query_sql, $startRow_sql, $maxRows_sql);

$sql = mysqli_query($connection, $query_limit_sql) or die(mysqli_error());

$row_sql = mysqli_fetch_assoc($sql);

if (isset($_GET['totalRows_sql'])) {

  $totalRows_sql = $_GET['totalRows_sql'];

} else {

  $all_sql = mysqli_query($connection, $query_sql);

  $totalRows_sql = mysqli_num_rows($all_sql);

}

$totalPages_sql = ceil($totalRows_sql/$maxRows_sql)-1;

$queryString_sql = "";

if (!empty($_SERVER['QUERY_STRING'])) {

  $params = explode("&", $_SERVER['QUERY_STRING']);

  $newParams = array();

  foreach ($params as $param) {

    if (stristr($param, "pageNum_sql") == false &&

        stristr($param, "totalRows_sql") == false) {

      array_push($newParams, $param);

    }

  }

  if (count($newParams) != 0) {

    $queryString_sql = "&" . htmlentities(implode("&", $newParams));

  }

}

$queryString_sql = sprintf("&totalRows_sql=%d%s", $totalRows_sql, $queryString_sql);

?>

<html>

.....

<?php do { ?>

                        <tr>

                            <td><?php echo $row_sql['id']; ?></td>

                              <td><?php echo $row_sql['name']; ?></td>

                        </tr>

<?php } while ($row_sql = mysqli_fetch_assoc($sql)); ?>

.....

<a href="<?php printf("%s?pageNum_sql=%d%s", $currentPage, max(0, $pageNum_sql - 1), $queryString_sql); ?>" title="previous page">Previous</a>

<a href="<?php printf("%s?pageNum_sql=%d%s", $currentPage, min($totalPages_sql, $pageNum_sql + 1), $queryString_sql); ?>" title="next page">Next</a>

.....

</html>

How can I make this??? Imean if I put all the php code (above the html tags) inside a function, what should my function return back, how will the output data inside the while loop will appear properly and also the pagin links???

TOPICS
Server side applications
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 25, 2013 Feb 25, 2013

There are many scripts that are pre-made, tried & tested that you could use.

There's a beautiful step-by-step tutorial along with source files here that you can easily follow: http://net.tutsplus.com/tutorials/php/how-to-paginate-data-with-php/

-ST

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 26, 2013 Feb 26, 2013
LATEST

Well this doesn't make any big difference, does it? Dreamweaver already generates the code for you so you can save some time from creating something from scratch. With few changes can get the result you want. Anyway thanks for the suggestion

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines