Rizvi
Rizvi

Reputation: 283

how to limit the page number?

My code show all number of page on one page.I want to limit the number of page?

My code is

$start_date = $_REQUEST['date1'];
$end_date = $_REQUEST['date2'];

$condition="1=1";
if ($start_date!="")
    $condition.=" AND event_date>='".date( "Y-m-d", strtotime($start_date))."'";
if ($end_date!="")
    $condition.=" AND event_date<='".date( "Y-m-d", strtotime($end_date))."'";
$start_date = date( "Y-m-d", strtotime($start_date));
$end_date = date( "Y-m-d", strtotime($end_date));

$link = mysql_connect('localhost', 'username', 'password');
if (!$link) 
{
    die('Could not connect: ' . mysql_error());
}
if ($_REQUEST["orderby"]!="") $ord = " ORDER BY ".$_REQUEST["orderby"];
if ($_REQUEST["dir"]!="") $ord .= " ".$_REQUEST["dir"];

mysql_select_db("intern_db", $link);
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;

$recordPerPage= '30';
$startPoint = ($page - 1)*$recordPerPage;
$result = mysql_query("SELECT count(*) cnt FROM ` admin_crmf_poc_event_history` where   $condition");
$row = mysql_fetch_array($result);
$num_rows = $row["cnt"];
$result = mysql_query("SELECT * FROM ` admin_crmf_poc_event_history` where $condition   $ord LIMIT $startPoint,$recordPerPage");
$totalPages=ceil($num_rows/$recordPerPage);

if ($page > 1) {
    echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.($page-   1).'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">Previous</a>';
}
for ($i = 1; $i < $totalPages; $i++) {
    echo '&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page= '. $i  .'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">' . $i . '</a>&nbsp;&nbsp;';
}
if ($page < $totalPages ) {
    echo '<a href="'.$_SERVER['PHP_SELF'].'?page='. ($page+1).'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">Next</a>';
}
echo "<br>";
echo "you are in $page page";
mysql_close($link);

I want to show like previous 1 2 3 4 5 6 7 8 9 10 next. When I click next it will show the other 10 pages from 11 to 20 and so on

Upvotes: 0

Views: 89

Answers (2)

Svetoslav
Svetoslav

Reputation: 4686

As I comment your question, there are A LOT of problems inside your code so better will be to use some ready function/class for it :) just search for it ..

How ever I can give you easy way to display what you want..

Replace your FOR loop with that code and it will be fine :)

$start = ( floor($page/10) * 10 ) + 1;
for( $i = $start; $i < $totalPages; $i++){
    if( $i >= ($start + 10)){
        break;
    }
    echo '&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page= '. $i  .'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">' . $i . '</a>&nbsp;&nbsp;';
}

But really SECURE YOUR CODE !!!

1st - MYSQL_ functions are deprecated use mysqli or PDO instead..

2nd - ESCAPE all variables in your queries..

3rd - Think for using more flexible methods of work (function/objects ..)

Upvotes: 1

Related Questions