Hi all,

I am new to PHP.. I want to paginate the result of a search query...
can any one give me an example of the concern codes...
u can post it on daniweb or u can send it on

Please help me..
Jino..

Dani AI

Generated

For : the replies already on this thread show the basic idea, but a few important clarifications are missing. ’s script demonstrates paging logic, yet it loads the entire result set into PHP and uses the old mysql_ API (no longer supported). pointed to a tutorial — good reading — but the recommended, production-safe pattern is different: run a COUNT() for the search, then fetch only the current page with LIMIT/OFFSET using prepared statements (PDO or mysqli). Preserve search parameters when building links and validate page/limit inputs. For very large tables, prefer keyset (seek) pagination instead of large OFFSETs.

Example (modern, secure pattern):

$pdo = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8mb4', 'user', 'pass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => false,
]);

$q = trim($_GET['q'] ?? '');
$page = max(1, (int)($_GET['page'] ?? 1));
$perPage = 10;
$search = "%{$q}%";

$count = $pdo->prepare('SELECT COUNT(*) FROM items WHERE title LIKE :q');
$count->execute([':q' => $search]);
$total = (int)$count->fetchColumn();

$offset = ($page - 1) * $perPage;
$stmt = $pdo->prepare('SELECT id,title FROM items WHERE title LIKE :q ORDER BY id DESC LIMIT :limit OFFSET :offset');
$stmt->bindValue(':q', $search, PDO::PARAM_STR);
$stmt->bindValue(':limit', $perPage, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

Notes and cautions: always use prepared statements to avoid SQL injection; bind LIMIT/OFFSET as integers (PDO::PARAM_INT) and disable emulated prepares when possible. Escape output with htmlspecialchars() to prevent XSS. Keep page bounds (no negative or out-of-range pages). COUNT(*) can be slow on very large tables — for high-scale search consider full-text indexes, external search engines, or keyset pagination (e.g., WHERE id < last_id ORDER BY id DESC LIMIT N) to avoid expensive offsets. Use http_build_query(array_merge($_GET, ['page'=>$n])) when rendering links so search terms stay in the query string.

Recommended Answers

All 2 Replies

<?php
/* paging_dinamis.php */

require_once "connection.inc.php";

echo "<form action=\"$PHP_SELF\" method=\"GET\">";
echo "<b>Number of Paging :</b>
      <select name='batas'>
        <option value='3'>3
        <option value='5'>5
        <option value='10'>10
        <option value='10'>15
      </select>&nbsp;";
echo "<input type=submit value='submit'>";
echo "</form>";

$flname=basename($PHP_SELF);

$res = mysql_query("SELECT * FROM table_name ORDER BY id");

$jml = @mysql_num_rows($res);
if ($jml == 0) {
    echo "<font color=red>
          <b>Ooops.... Data not found</b></font>";
    exit;
}

// Initialization default value for paging
if (isset($_GET["batas"])) {
    $batas  = $_GET["batas"];
} else {
    $batas = 3;  
}

if (($jml % $batas) == 0) {
    $jmlpage=(int)($jml/$batas);
} else {
    $jmlpage=((int)$jml/$batas)+1;
}

// Inisialisasi variabel page 
if (isset($_GET["page"])) {
    $page  = $_GET["page"];
} else {
   $page = 1;  
}

if ($page>$jmlpage) {
    $page = $jmlpage;
}

while ($rows = mysql_fetch_array($res)) {
    $arrdata[] = $rows;
}

$end  = ($page*$batas)-1;
$start= $end-($batas-1);
if ($end > $jml) {
    $end = $jml-1;
}

for ($i=$start; $i<=$end; $i++) {
     $arr[] = $arrdata[$i];
}
echo "<table width=450 style='border:1pt solid #666666;'>";
foreach ($arr as $row) {
    echo "<tr><td width=100>Nama</td>
              <td width=10>:</td><td>$row[1]</td></tr>"; 
    echo "<tr><td>Email</td><td>:</td><td>
          <a href='mailto:$row[2]'>$row[2]</a></td></tr>"; 
    echo "<tr><td>Komentar</td>
              <td>:</td><td>$row[3]</td></tr>"; 
    echo "<tr><td>&nbsp;</td></tr>"; 
}

echo "</table> <br>";

// Manage paging navigation
for ($n=1; $n<=$jmlpage; $n++) {
  $b = $page + 1;
  if ($n != $page) {
      echo "&nbsp;<a href='$flname?page=$n&batas=$batas'>
            Hal $n</a>&nbsp;";
  } else {
      echo "<font color='#999999'><b>Hal $n </b></font>"; 
  }   
}

// Next navigation paging
if (($n != $page) && ($n > $b)) {
    echo "&nbsp;<a href='$flname?page=$b&batas=$batas'>
          Next</a>";
}
?>
</body></html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.