Jump to content

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

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.