Member Avatar for Member #710047

How to print more than one results in a page??
I want to print 10 results per page and show next button if it have more than 10 results..
In the next button it will show another 10 results..
How to code it..??
And please explain the code if you can...
It will my skills to generate my code..

I have a table and three fields..
id, name and info..
name is the one from witch i want to show results..
because it repeats in the data.. It repeats so many times..

Dani AI

Generated

The goal is pagination — show 10 rows, then a Next (and Prev) control to fetch the next block. showed the basic fetch/loop; that works for dumping rows but it does not implement pagination or modern safety. Use SQL LIMIT/OFFSET plus a COUNT to know whether a Next link is needed, and avoid the deprecated mysql_* functions — use PDO or MySQLi with prepared statements.

A minimal, safe pagination flow (using PDO) — validate page, compute offset, get total rows, then fetch the page rows:

$perPage = 10;
$page = max(1, (int)($_GET['page'] ?? 1));
$offset = ($page - 1) * $perPage;

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

$total = (int)$pdo->query('SELECT COUNT(*) FROM your_table')->fetchColumn();

$stmt = $pdo->prepare('SELECT id,name,info FROM your_table ORDER BY id LIMIT :limit OFFSET :offset');
$stmt->bindValue(':limit', $perPage, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();

foreach ($stmt as $row) {
    // render table row
}

if ($page > 1) echo '<a href="?page='.($page-1).'">Prev</a>';
if ($offset + $perPage < $total) echo '<a href="?page='.($page+1).'">Next</a>';

If you only want unique name values (because they repeat), paginate the unique set: use SELECT DISTINCT name ... LIMIT ... or SELECT name, MIN(id) FROM table GROUP BY name ... and COUNT(DISTINCT name) for the total. Always include an ORDER BY so page results are stable. For very large tables, OFFSET becomes slow — consider keyset (cursor) pagination: WHERE id > :last_id ORDER BY id LIMIT :perPage.

Security/UX notes: cast and clamp page to an integer, bind numeric limits as integers (PDO::PARAM_INT), and show clear disabled/hidden Prev/Next when at bounds. For reference: see the PDO prepare docs (PDO::prepare) and MySQL SELECT/LIMIT behavior (MySQL SELECT).

Recommended Answers

All 5 Replies

Member Avatar for Member #334542
$query = "SELECT * FROM symbols";

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
    // yes 
    // print them one after another 
    echo "<table cellpadding=10 border=1>"; 
    while($row = mysql_fetch_row($result)) { 
        echo "<tr>"; 
        echo "<td>".$row[0]."</td>"; 
        echo "<td>" . $row[1]."</td>"; 
        echo "<td>".$row[2]."</td>"; 
        echo "</tr>"; 
    } 
    echo "</table>"; 
} 
else { 
    // no 
    // print status message 
    echo "No rows found!"; 
}

This is the logic to show all records, split up to 10 by implementing the logic

Member Avatar for Member #710047

I know how to put 10 results but have to print the next button for the next 10 results..

Member Avatar for Member #334542

Add a button at the 11th row.

Member Avatar for Member #710047

in the 11th row what to link to show the next results..

Member Avatar for Member #334542

Again generate the loop for next 10 records

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.