Now code looks like this... No links(page navigaton links) working...

i think(not sure) problem @ line no 55, 66 & 77...(query part of URL).

<?php 
//CONNECTION TO DATABASE!

$host="localhost";      // Host name 
$username="root";       // Mysql username 
$password="";           // Mysql password 
$db_name="accounts";    // Database name
$tbl_name="login";      // Table name
$tbl1_name="details";   // Table1 name

// Connect to server & select databse.
$con=mysql_connect("$host","$username","$password");
mysql_select_db("$db_name",$con);

$show_all=$_POST['rg01'];                     // Show All

//**EDIT TO YOUR TABLE NAME, ECT. 

$t = mysql_query(" SELECT username FROM $tbl1_name WHERE sex = '$show_all' "); 
if(!$t) die(mysql_error()); 
    
$a                = mysql_fetch_object($t); 
$total_items      = mysql_num_rows($t); 
$limit            = 3;                       //default 
$page             = 1;                       //default 
//$sex              = $show_all; 

//calcuate total pages 
$total_pages     = ceil($total_items / $limit); 
$set_limit       = $page * $limit - ($limit); 

//query: **EDIT TO YOUR TABLE NAME, ECT. 

$q = mysql_query("SELECT username FROM $tbl1_name WHERE sex = '$show_all' LIMIT $set_limit, $limit"); 
  if(!$q) die(mysql_error()); 
     $err = mysql_num_rows($q); 
       if($err == 0) die("No matches met your criteria."); 

echo "<br>";

//show data matching query: 
while($code = mysql_fetch_object($q)) { 
     echo("item: ".$code->username."<BR>"); 
} 

$sex = urlencode($show_all); //makes browser friendly 
//echo $show_all;
//echo $sex;
echo "<br>";

//prev. page: **EDIT LINK PATH** 
$prev_page = $page - 1; 

if($prev_page >= 1) { 
  echo("<b>&lt;&lt;</b> <a href=http://localhost/wamp/www/28_09_pm_new/new.php?sex=$sex&amp;limit=$limit&amp;page=$prev_page><b>Prev.</b></a>"); 
} 
//Display middle pages: **EDIT LINK PATH** 
for($a = 1; $a <= $total_pages; $a++) 
{ 
   if($a == $page) 
   { 
      echo("<b> $a</b> | "); //no link 
   }
   else 
   { 
    echo("  <a href=http://localhost/wamp/www/28_09_pm_new/new.php?sex=$sex&amp;limit=$limit&amp;page=$a> $a </a> | "); 
   }
} 


//next page: **EDIT THIS LINK PATH** 
if($page!=$total_pages)
{
  $next_page = $page + 1; 
  if($next_page <= $total_pages) 
  { 
   echo("<a href=http://localhost/wamp/www/28_09_pm_new/new.php?sex=$sex&amp;limit=$limit&amp;page=$next_page><b>Next</b></a> &gt; &gt;"); 
  } 
}
//all done 
?>

Recommended Answers

All 6 Replies

its bit complecating when you use database pagination in many pages
why dont you use function for pagination,here is the function i use for pagination

function Navigation($start,$total,$link){
global $len;$en = $start +$len;if($start==0){if($total>0){$start1=1;}else {$start1=0;}}else{$start1=$start+1;}if($en>$total)$en = $total;if($total!=0)$pagecount=($total % $len)?(intval($total / $len)+1):($total / $len); else{$pagecount=0;return;}print "<table  cellpading=0 cellspacing=0  width=100% nowrap><tr>";print "<td width=40% class='dlinks' valign=bottom height=25  align='left'>Showing $start1 to $en of $total</td>";print "<td width=54% align=right valign=bottom>";if($en>$len){$en1=$start-$len; print "<a href='$link&start=$en1' class='slinks'> Previous </a><span class='slinks'> | </span>" ; }else print "<span class='slinks'> Previous </span><span class='slinks'> | </span>" ;$numstr="";$curpage=intval(($start+1)/$len)+1;if($pagecount > 10){$istart=(intval($curpage/10) * 10)+1;if($istart + 10 > $pagecount)$istart=$pagecount - 9;$pagecount=10;}else $istart=1;for($i=$istart;$i<$pagecount+$istart;$i++){$ed=($i-1)*$len;if($start!=$ed){$numstr.= " <a href='$link&start=$ed' class='slinks'> $i </a><span class='slinks'> | </span>";}else {$numstr.= "<span class='slinks'><strong> $i </strong></span><span class='slinks'> | </span>";}}print $numstr;if($en<$total){$en2=$start+$len;print "<a href='$link&start=$en2' class='slinks'> Next </a>" ;}else print "<span class='slinks'> Next </span>" ;print "</td></tr></table>";
}

in page you pass link="Your page.php?a=a";
$len=25;
and in query

mysql_query("select * from name limit $start,$len");

in page bottom you can call function

if you have any doubts post it here

Try removing the "(" at the beginning and end of echo !! just go on typing the one you wanted and close with "

PravinBhat is correct, try removing the parenthesis.

Other issues you may want to address when you get this working:
When using a variable in a function you don't need to include the "". See below for changes

$con=mysql_connect($host, $username, $password);
mysql_select_db($db_name, $con);

Use single quotes over double unless you need them. If you are going to escape then don't bother with double quotes...

On a side note, don't use deprecated code; change <b> to <strong>.

If you still have problems, let us know what error are you getting, if any.

Instead of $show_all=$_POST;

Use below one

$show_all=$_REQUEST;

use below code change the query use whtever u want for that and retrieve those columns related to that table, also changes locvalhost path for previous and next page:

<?
$host="localhost";      // Host name 
$username="root";       // Mysql username 
$password="";           // Mysql password 
$db_name="accounts";    // Database name


$tbl_name="login";      // Table name
$tbl1_name="details";   // Table1 name

$con=mysql_connect("$host","$username","$password");
mysql_select_db("$db_name",$con);


$show_all=$_REQUEST['rg01'];    


        $items_per_row = 5;
        $items_per_page =5;
        $chunk=5;
        $page = empty($_REQUEST['page']) ? 0 : $_REQUEST['page'];

        if ($page==-1)
        {
         $items_per_page=9999999;
         $xpage=$page;
         $page=0;
        }


        $query6="select SQL_CALC_FOUND_ROWS * from $tbl1_name WHERE sex = '$show_all'";
        $result6=mysql_query($query6);
        $nr = mysql_num_rows($result6);
        $z = ceil($nr / $items_per_page);
        if($nr!=0)
        {
            ?>
            <table border="1" align="center">
            <tr><th>Username</th><th>Sex</th></tr>
            <?   
                $lmt=$page * $chunk;
                $query="select SQL_CALC_FOUND_ROWS * from $tbl1_name WHERE sex = '$show_all' LIMIT " . $lmt . ", $chunk";
                $result=mysql_query($query);

                $maxlevel = ($page*$items_per_page)+$chunk;
                if ($maxlevel > $nr) $maxlevel=$nr; 

                while($row = mysql_fetch_array($result))
                        {   
            ?>
            <tr>
            <td><? echo $row['username']; ?></td>
            <td><? echo $row['sex']; ?></td>
            </tr>
            <?

        }

            ?>
    </table>
    <table cellpadding="0" cellspacing="0" align="center" border="0">
    <tr><td><br /></td></tr>
    <tr>
        <td>
            <?php if ($page > 1) { ?><a href="http://localhost/pooja/pooja_practice3/index.php?rg01=<?=$show_all?>&lmt=<?echo $lmt?>&amp;page=<?=$page-1?>">Previous<?php }  ?>
                <?php if ($page == 1) { ?><a href="http://localhost/pooja/pooja_practice3/index.php?rg01=<?=$show_all?>&lmt=<?echo $lmt?>">Previous</a><?php }  ?>

                 <?php  for($i = 0; $i < $z; $i++) { ?>
                  <?php if ($page == $i) { ?>
                   <span class="color-new"><?=$i+1?></span>
                  <?php } else if ($i==0) { ?>
                   <a href="http://localhost/pooja/pooja_practice3/index.php?rg01=<?=$show_all?>"><?=$i+1?></a>
                  <?php } else  { ?>
                   <a href="http://localhost/pooja/pooja_practice3/index.php?rg01=<?=$show_all?>&lmt=<?echo $lmt?>&amp;page=<?=$i?>"><?=$i+1?></a>
                  <?php } ?>
                 <?php } ?>

                <?php if ($page < ($z-1)) {?><a href="http://localhost/pooja/pooja_practice3/index.php?rg01=<?=$show_all?>&lmt=<?echo $lmt?>&amp;page=<?=$page+1?>">Next</a><?php }  ?>

                <p class="displaying"><span class="color"><?=($page*$items_per_page)+1?>-<?=$maxlevel?></span> of <?=$nr?> items</p>
        </td>
    </tr>
    </table>
<?
        }
        else
        {
            ?>
            <h3 align="center">There are no records.</h3>
            <?
        }
mysql_close($con);

?>

thanks to all those who replied...

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.