Jump to content

[SOLVED] paged query issue


noobstar

Recommended Posts

Hi everyone :)

This is a fairly simple problem but i can't seem to see it anywhere grrr. What this does is display the first 5 results and then has a Next, Last etc link on the bottom so you can view the other results.

However, this displays the 5 results and doesn't show the Next, Last etc links on the bottom. Its probably something i forgot to convert when i copied this from my working example.

If someone could just take a quick look at the code below and maybe work it out please let me know :)

Here is the code:
[code]<?php

include ("/home/btk/public_html/forum/config.php");

$search = $_POST['search'];

// how many rows to show per page
$rowsPerPage = 5;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$query  = "select * from fusion_users where user_name like '%$search%' LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');

echo "<div align=center><span class=style1><u>Search Results</u></span>";

while($row = mysql_fetch_array($result))
{
echo "<br/><br/><form method=post style=display:inline action=#>";
echo "<input type=submit value='".$row['user_name']."' class=link>";
echo "<input type=hidden name=user_name value='".$row['user_name']."'></form>";
}

// how many rows we have in database
$query  = "SELECT COUNT(user_id) as numrows from fusion_users where user_name like '%$search%'";
$result  = mysql_query($query) or die('Error, query failed');
$row    = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];

// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link

// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
    $page = $pageNum - 1;
    $prev = "<a href=\"$self?page=$page\">[Prev]</a>";
   
    $first = "<a href=\"$self?page=1\">[First Page]&nbsp;</a>";
}
else
{
    $prev  = ' [Prev] ';      // we're on page one, don't enable 'previous' link
    $first = ' [First Page] '; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
    $page = $pageNum + 1;
    $next = "<a href=\"$self?page=$page\">[Next]&nbsp;</a>";
   
    $last = "<a href=\"$self?page=$maxPage\">[Last Page]</a>";
}
else
{
    $next = ' [Next] ';      // we're on the last page, don't enable 'next' link
    $last = ' [Last Page] '; // nor 'last page' link
}

echo "<br/><br/><a href=viewpage.php?page_id=2>Back</a></div>";

?>
<style type="text/css">
<!--
.style1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-weight: bold; font-size: 14px;
}
.link { background:none; border:none; cursor:pointer; color:black; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px;}
-->
</style>[/code]


Thank you very much for any replies :)
Link to comment
https://forums.phpfreaks.com/topic/31659-solved-paged-query-issue/
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.