Issues with Pagination in PHP - php

Currently I've 2 tables and have the datas are dynamic.
I need to add pagination for both tables seperately. I had added paginator using paginator class and it is working fine. But the problem is when I click on the next button of the first table then the contents of both tables are changed in to the next page.
Paginator.php
<?php
// Paginator Class
// error_reporting(E_ALL);
define("QS_VAR", "page"); // the variable name inside the query string (don't use this name inside other links)
define("STR_FWD", "Next>>"); // the string is used for a link (step forward)
define("STR_BWD", "<<Prev"); // the string is used for a link (step backward)
$scriptname = (isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : '');
define("SCRIPT_NAME", $scriptname);
$v = (isset($_REQUEST['page_num_rows']) ? (is_numeric($_REQUEST['page_num_rows']) ? $_REQUEST['page_num_rows'] : 5) : 5);
define("NUM_ROWS", $v); // the number of records on each page
class Paginator {
var $sql;
var $result;
var $get_var = QS_VAR;
var $rows_on_page = NUM_ROWS;
var $str_forward = STR_FWD;
var $str_backward = STR_BWD;
var $all_rows;
var $num_rows;
var $page;
var $number_pages;
var $url_name = SCRIPT_NAME;
// constructor
function Paginator() {
}
// sets the current page number
function set_page() {
$this->page = (isset($_REQUEST[$this->get_var]) && $_REQUEST[$this->get_var] != "") ? $_REQUEST[$this->get_var] : 0;
return $this->page;
}
// gets the total number of records
function get_total_rows() {
$tmp_result = mysql_query($this->sql);
$this->all_rows = mysql_num_rows($tmp_result);
mysql_free_result($tmp_result);
return $this->all_rows;
}
// get the totale number of result pages
function get_num_pages() {
$this->number_pages = ceil($this->get_total_rows() / $this->rows_on_page);
return $this->number_pages;
}
// returns the records for the current page
function get_page_result() {
$start = $this->set_page() * $this->rows_on_page;
$page_sql = sprintf("%s LIMIT %s, %s", $this->sql, $start, $this->rows_on_page);
$this->result = mysql_query($page_sql);
return $this->result;
}
// get the number of rows on the current page
function get_page_num_rows() {
$this->num_rows = #mysql_num_rows($this->result);
return $this->num_rows;
}
// free the database result
function free_page_result() {
#mysql_free_result($this->result);
}
function display_row_count() {
$var = $this->get_var;
$url_part1 = $this->url_name . "?";
$url_part2 = "&" . $var . "=0" . $this->rebuild_qs($var);
$select = " Show ";
$select.="<form method=get name=page_num_rows_form action=\"$this->url_name\" >"; // [form used for javascript disabled case] -not working
$select.="<select name=page_num_rows id=page_num_rows onChange=\"window.location='$url_part1'+'page_num_rows='+this.value+'$url_part2'\" >";
$select.="<option value=50 " . (isset($_REQUEST['page_num_rows']) ? ($_REQUEST['page_num_rows'] == 50 ? ' selected ' : '') : '') . " >50</option>";
$select.="<option value=100 " . (isset($_REQUEST['page_num_rows']) ? ($_REQUEST['page_num_rows'] == 100 ? ' selected ' : '') : '') . " >100</option>";
$select.="<option value=150 " . (isset($_REQUEST['page_num_rows']) ? ($_REQUEST['page_num_rows'] == 150 ? ' selected ' : '') : '') . " >150</option>";
$select.="<option value=200 " . (isset($_REQUEST['page_num_rows']) ? ($_REQUEST['page_num_rows'] == 200 ? ' selected ' : '') : '') . " >200</option>";
$select.="<option value=500 " . (isset($_REQUEST['page_num_rows']) ? ($_REQUEST['page_num_rows'] == 500 ? ' selected ' : '') : '') . " >500</option>";
$select.="</select>";
$select.="<noscript> <input type=submit value=Go /></noscript>";
$select.="</form>"; // form used for javascript disabled case -- not working
$select.=" per page";
return $select;
}
// function to handle other querystring than the page variable
function rebuild_qs($curr_var) {
if (!empty($_SERVER['QUERY_STRING'])) {
$parts = explode("&", $_SERVER['QUERY_STRING']);
$newParts = array();
foreach ($parts as $val) {
if (stristr($val, $curr_var) == false) {
array_push($newParts, $val);
}
}
if (count($newParts) != 0) {
$qs = "&" . implode("&", $newParts);
} else {
return false;
}
return $qs; // this is your new created query string
} else {
return false;
}
}
// this method will return the navigation links for the conplete recordset
function navigation($separator = " | ", $css_current = "", $back_forward = false) {
$max_links = NUM_LINKS;
$curr_pages = $this->set_page();
$all_pages = $this->get_num_pages() - 1;
$var = $this->get_var;
$navi_string = "";
if (!$back_forward) {
$max_links = ($max_links < 2) ? 2 : $max_links;
}
if ($curr_pages <= $all_pages && $curr_pages >= 0) {
if ($curr_pages > ceil($max_links / 2)) {
$start = ($curr_pages - ceil($max_links / 2) > 0) ? $curr_pages - ceil($max_links / 2) : 1;
$end = $curr_pages + ceil($max_links / 2);
if ($end >= $all_pages) {
$end = $all_pages + 1;
$start = ($all_pages - ($max_links - 1) > 0) ? $all_pages - ($max_links - 1) : 1;
}
} else {
$start = 0;
$end = ($all_pages >= $max_links) ? $max_links : $all_pages + 1;
}
if ($all_pages >= 1) {
$forward = $curr_pages + 1;
$backward = $curr_pages - 1;
$navi_string = ($curr_pages > 0) ? "" . $this->str_first . " " . $this->str_backward . " " : $this->str_first . " " . $this->str_backward . " ";
$navi_string .= ($curr_pages < $all_pages) ? " " . $this->str_forward . "" . " " : " " . $this->str_forward . " ";
}
}
return "<span style='font-size:.7em; padding:3px 3px 4px 3px;'>" . $this->current_page_info() . $navi_string . "</span>";
}
function current_page_info() {
$cur_page = $this->set_page() + 1;
$total_pages = $this->get_num_pages();
// $page_info = " Page " . $cur_page . " of " . $total_pages . " ";
// return $page_info;
}
function show_go_to_page() {
$cur_page = $this->set_page() + 1;
$total_pages = $this->get_num_pages();
$options = "";
for ($i = 1; $i <= $total_pages; $i++) {
$options.="<option value=$i " . (($i == $cur_page) ? ' selected ' : '') . ">$i</option>";
}
$page_info = " Go to page <input type=text name=paginator_go_to_page id=paginator_go_to_page size=1 value=$cur_page />";
// $page_info.= "<select name=paginator_go_to_page2 >$options</select>";
return $page_info;
}
}
?>
tables.php
<?php
include("library/paginator.php");
?>
<div style="text-align:right;">
<?
$query = "select * from haves_settings";
$tab = mysql_query($query);
$row = mysql_fetch_array($tab);
$item_no = $row['items_to_show'];
$scroll = $row['scroll_interval'];
$online_paginate = new Paginator;
$online_paginate->sql = "select * from placing_item_bid where status='Active' and picture1!='' and selling_method!='want_it_now' and selling_method!='ads' and bid_starting_date <= now() and expire_date>=now() order by item_id desc"; // sql statement
$online_paginate->rows_on_page = $item_no;
$results = $online_paginate->get_page_result(); // result set
$num_rows = $online_paginate->get_page_num_rows(); // number of records in result set
$nav_links = $online_paginate->navigation(" | "); // the navigation links (define a CSS class
?>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr bgcolor="#d79196" class="detail9txt">
<input type="hidden" value="2" name="len">
<td align="center" width="20%"><b>Picture</b> </td>
<td width="30%" align="center"><b>Name / Responses</b> </td>
<td width="50%" align="center"><b>Description</b> </td>
</tr><tr style="height:10px;"><td></td></tr>
<?
if ($num_rows > 0) {
while ($bestsellers_fetch = mysql_fetch_array($results)) {
$temp = $bestsellers_fetch['item_id'];
$sql = "SELECT count(`user_id`) as response FROM `watch_list` where `item_id`=$temp group by `item_id`";
$res = mysql_query($sql);
$response = mysql_fetch_row($res);
$counttop = $counttop + 1;
if (!empty($bestsellers_fetch['sub_title']))
$item_subtitle1 = $bestsellers_fetch['sub_title'];
else
$item_subtitle1 = substr($bestsellers_fetch['item_title'], 0, 20);
$item_title1 = substr($bestsellers_fetch['item_title'], 0, 40)
?>
<tr>
<td class="tr_botborder" style="vertical-align:middle;" width="20%" align="center"><div align="center"><img src="thumbnail/<?= $bestsellers_fetch['picture1']; ?>" alt="" width="79" height="70" border="0" /></div></td>
<td class="tr_botborder" style="vertical-align:middle;" width="30%" align="center"><div align="center"><span class="bestsellerstxt"><?= $item_subtitle1; ?> <?= $item_title1; ?><br/><?php if ($response[0] != '') { ?><a style="text-decoration:none;color:#336666;" href="detail.php?item_id=<?= $bestsellers_fetch['item_id']; ?>"> <?php echo $response[0] . ' responses'; ?></a> <?php } else { ?><span style="color:#666666;"><?php
echo '0 responses';
}
?></span></span></td>
<td class="tr_botborder" style="vertical-align:middle;" width="50%" align="center"><div align="center"><span class="bestsellerstxt"><?= html_entity_decode($bestsellers_fetch['detailed_descrip']); ?></span></td>
</tr>
<?
if ($counttop != 2) {
}
}
} else {
?>
<tr><td height="148" align="center" class="featxt">No Items Available</td></tr>
<?
}
?>
</table>
<div style="text-align: right;"><?php echo $nav_links; ?></div>
//wants content
$online_paginate1 = new Paginator;
$online_paginate1->sql = "select * from placing_item_bid where status='Active' and selling_method='want_it_now' order by item_id desc";
$online_paginate1->rows_on_page = $item_no1;
$result1 = $online_paginate1->get_page_result(); // result set
$want_total_records = $online_paginate1->get_page_num_rows(); // number of records in result set
$nav_links1 = $online_paginate1->navigation(" | "); // the navigation links (define a CSS class
?>
<div class="superbg">
<table cellspacing="0" cellpadding="5" width=100%>
<form name="want_form" action="myauction.php" method=post>
<tr bgcolor="#d79196" class="detail9txt">
<input type="hidden" name="len" value="<?= $want_total_records ?>">
<td align="center" width="30%"><b>Name / Responses</b> </td>
<td align="center" width="20%"><b>Picture</b> </td>
<td width="50%" align="center"><b>Description</b> </td>
</tr>
<?
if ($want_total_records > 0) {
while ($want_row = mysql_fetch_array($result1)) {
$tot_bid_sql = "select count(*) from want_it_now where wanted_itemid=" . $want_row[item_id];
$tot_bid_res = mysql_query($tot_bid_sql);
$tot_bids = mysql_fetch_array($tot_bid_res);
?>
<tr class="detail9txt">
<td class="tr_botborder" align="center" style="vertical-align:middle;" width="30%">
<a href="wantitnowdes.php?item_id=<?= $want_row['item_id'] ?>" class="header_text">
<? echo $want_row['item_title']; ?></a> <br/> <?
if ($tot_bids[0] != 0) {
?>
<a style="font-weight:normal;" href="wantitnowdes.php?item_id=<?= $want_row['item_id'] ?> " class="header_text"><? echo $tot_bids[0] . ' responses'; ?></a>
<?
} else {
echo $tot_bids[0] . ' responses';
}
?></td>
<td class="tr_botborder" style="vertical-align:middle;" width="20%" align="center">
<?
if (!empty($want_row['picture1'])) {
$img = $want_row['picture1'];
list($width, $height, $type, $attr) = getimagesize("images/$img");
$h = $height;
$w = $width;
if ($h > 50) {
$nh = 50;
$nw = ($w / $h) * $nh;
$h = $nh;
$w = $nw;
}
if ($w > 50) {
$nw = 50;
$nh = ($h / $w) * $nw;
$h = $nh;
$w = $nw;
}
?>
<!-- <img name="runimg" src="images/<? //echo $want_row['picture1']; ?>" border=1 width=<? //= $w; ?> height=<? //=$h ?> >-->
<img name="runimg" src="images/<? echo $want_row['picture1']; ?>" border=1 width="79" height="70" >
<?
} else {
?>
<img src="images/no_image.gif" border=1 name="runimg" >
<? } ?>
</td>
<td class="tr_botborder" style="vertical-align:middle;" width="50%" align="center"><div align="center"><span class="bestsellerstxt"><?= html_entity_decode($want_row['detailed_descrip']); ?></span></td>
</tr>
<?
} // while
} else {
?>
<tr>
<td width="3%"> </td>
<td width="97%" class="myauction3txt">There are no items in this section</td>
</tr>
<? } ?>
</table>
<div style="text-align: right;"><?php echo $nav_links1; ?></div>
</div>

Where is the problem ?
Paginator class uses the same query string "page" parameter to calculate the current page. If you add 2 or more Pagination in the same request, "page" will be shared by all instances, leading to this mess you described.
How to fix it ?
Tell the Paginator class which parameter to use in query string... follow this 2-step patch below :
Step 1 : replace constructor in Paginator class
// constructor
function Paginator($get_var=null) {
if ($get_var!=null) $this->get_var = $get_var;
}
Step 2 : update Paginator object creation (twice)
$online_paginate = new Paginator('page_table1');
and later :
$online_paginate1 = new Paginator('page_table2');
Hope this helps !

Related

Print the HTML and PHP Code in Double or single quotation marks

I am Displaying HTML Table which fetch data from database in PHP, and Exporting this in PDF Page... Everything is fine, but when PDF file download, except of values from Mysql it display the php code in table, and the issue is with inverted commas, so how to write the code in inverted commas, that it display values exactly in table except of php code?
WHEN CLICK ON IT
<button type="button" class="btn btn-success">PDF FORMAT</button>
Exporting Table in PDF Code "FILE.PHP"
<?php require_once 'components/app.php'; ?>
<?php
include('mpdf/mpdf.php');
$mpdf=new mPDF();
$mpdf->WriteHTML("
<table id='fixed_table' class='table table-bordered table-hover'>
<thead>
<tr>
<th>District</th>
<th>Union Council</th>
<th>Village</th>
<th>Recreational</th>
<th>Awareness Raising</th>
<th>Training</th>
<th>Social Mobilization</th>
<th>Total Activities</th>
</tr>
</thead>
<tbody>
<tr>
<td style='color:red;'><b><?php echo getTotalDistrictUnique();?></b></td>
<td style='color:red;'><b><?php echo getTotalUnionCouncilUnique();?></b></td>
<td style='color:red;'><b><?php echo getTotalVillages(); ?></b></td>
<td style='color:red;'><b><?php echo getTotalRecreational();?></b></td>
<td style='color:red;'><b><?php echo getTotalAwareness();?></b></td>
<td style='color:red;'><b><?php echo getTotalTraining();?></b></td>
<td style='color:red;'><b><?php echo getTotalSocial();?></b></td>
<td style='color:red;'><b><?php echo getTotalRecreational() + getTotalAwareness() + getTotalTraining() + getTotalSocial(); ?></td>
</tr>
<?php
include('connection.php');
$query ='select * from general';
$run =mysqli_query($con,$query);
while ($row=mysqli_fetch_array($run)) {
$id=$row[0];
$createdate=$row[1];
$createday=$row[2];
$partnername=$row[3];
$district=$row[4];
$unioncouncil=$row[5];
$village=$row[6];
$vannumber=$row[7];
$facilitator=$row[8];
$beneficiarytype=$row[9];
$rmultimedia=$row[10];
$rgame=$row[11];
$rsprort=$row[12];
$rart=$row[13];
$rgroupreading=$row[14];
$rother=$row[15];
$alandminer=$row[16];
$apersonalsafety=$row[17];
$abirthregister=$row[18];
$aother=$row[19];
$tstickstone=$row[20];
$tohthers=$row[21];
$sbirthregister=$row[22];
$sother=$row[23];
$formnumber=$row[24];
$submitdatatime=$row[25];
?>
<tr>
<td><?php echo $district?></td>
<td><?php echo $unioncouncil?></td>
<td><?php echo $village?></td>
<td>
<?php
if($rmultimedia=='true')
{
$rmultimedia_value = 1;
}
else{
$rmultimedia_value = 0;
}
if($rgame=='true')
{
$rgame_value = 1;
}
else{
$rgame_value = 0;
}
if($rsprort=='true')
{
$rsprort_value = 1;
}
else{
$rsprort_value = 0;
}
if($rart=='true')
{
$rart_value = 1;
}
else{
$rart_value = 0;
}
if($rgroupreading=='true')
{
$rgroupreading_value = 1;
}
else{
$rgroupreading_value = 0;
}
if($rother=='true')
{
$rother_value = 1;
}
else{
$rother_value = 0;
}
$recreational_sum = $rmultimedia_value + $rgame_value + $rsprort_value + $rart_value + $rgroupreading_value + $rother_value;
echo $recreational_sum;?></td>
<td>
<?php
if($alandminer=='true')
{
$alandminer_value = 1;
}
else{
$alandminer_value = 0;
}
if($apersonalsafety=='true')
{
$apersonalsafety_value = 1;
}
else{
$apersonalsafety_value = 0;
}
if($abirthregister=='true')
{
$abirthregister_value = 1;
}
else{
$abirthregister_value = 0;
}
if($aother=='true')
{
$aother_value = 1;
}
else{
$aother_value = 0;
}
$awareness_raising_sum = $alandminer_value + $apersonalsafety_value + $abirthregister_value + $aother_value;
echo $awareness_raising_sum;?>
</td>
<td>
<?php
if($tstickstone=='true')
{
$tstickstone_value = 1;
}
else{
$tstickstone_value = 0;
}
if($tohthers=='true')
{
$tohthers_value = 1;
}
else{
$tohthers_value = 0;
}
$training_sum = $tstickstone_value + $tohthers_value;
echo $training_sum;?>
</td>
<td>
<?php
if($sbirthregister=='true')
{
$sbirthregister_value = 1;
}
else{
$sbirthregister_value = 0;
}
if($sother=='true')
{
$sother_value = 1;
}
else{
$sother_value = 0;
}
$social_mobilization_sum = $sbirthregister_value + $sother_value;
echo $social_mobilization_sum;?>
</td>
<td style='color:red;'><?php echo $recreational_sum + $awareness_raising_sum + $training_sum + $social_mobilization_sum;?></td>
</tr>
<?php } ?>
</tbody>
</table>
");
$mpdf->Output(); exit;
?>
Your code is very messy. The PHP code in the string is just a string not code anymore. You should write your code properly.
<?php
require_once 'components/app.php';
include('connection.php');
include('mpdf/mpdf.php');
$content = '
<table id="fixed_table" class="table table-bordered table-hover">
<thead>
<tr>
<th>District</th>
<th>Union Council</th>
<th>Village</th>
<th>Recreational</th>
<th>Awareness Raising</th>
<th>Training</th>
<th>Social Mobilization</th>
<th>Total Activities</th>
</tr>
</thead>
<tbody>
<tr>
<td style="color:red;"><b>' . getTotalDistrictUnique() . '</b></td>
<td style="color:red;"><b>' . getTotalUnionCouncilUnique() . '</b></td>
<td style="color:red;"><b>' . getTotalVillages() . '</b></td>
<td style="color:red;"><b>' . getTotalRecreational() . '</b></td>
<td style="color:red;"><b>' . getTotalAwareness() . '</b></td>
<td style="color:red;"><b>' . getTotalTraining() . '</b></td>
<td style="color:red;"><b>' . getTotalSocial() . '</b></td>
<td style="color:red;"><b>' . (getTotalRecreational() + getTotalAwareness() + getTotalTraining() + getTotalSocial()) . '</td>
</tr>
';
$query ='select * from general';
$run =mysqli_query($con,$query);
while ($row=mysqli_fetch_array($run)) {
$id=$row[0];
$createdate=$row[1];
$createday=$row[2];
$partnername=$row[3];
$district=$row[4];
$unioncouncil=$row[5];
$village=$row[6];
$vannumber=$row[7];
$facilitator=$row[8];
$beneficiarytype=$row[9];
$rmultimedia=$row[10];
$rgame=$row[11];
$rsprort=$row[12];
$rart=$row[13];
$rgroupreading=$row[14];
$rother=$row[15];
$alandminer=$row[16];
$apersonalsafety=$row[17];
$abirthregister=$row[18];
$aother=$row[19];
$tstickstone=$row[20];
$tohthers=$row[21];
$sbirthregister=$row[22];
$sother=$row[23];
$formnumber=$row[24];
$submitdatatime=$row[25];
$rmultimedia_value = $rmultimedia == 'true' ? 1 : 0;
$rgame_value = $rgame == 'true' ? 1 : 0;
$rsprort_value = $rsprort == 'true' ? 1 : 0;
$rart_value = $rart == 'true' ? 1 : 0;
$rgroupreading_value = $rgroupreading == 'true' ? 1 : 0;
$rother_value = $rother == 'true' ? 1 : 0;
$recreational_sum = $rmultimedia_value + $rgame_value + $rsprort_value + $rart_value + $rgroupreading_value + $rother_value;
$alandminer_value = $alandminer == 'true' ? 1 : 0;
$apersonalsafety_value = $apersonalsafety == 'true' ? 1 : 0;
$abirthregister_value = $abirthregister == 'true' ? 1 : 0;
$aother_value = $aother == 'true' ? 1 : 0;
$awareness_raising_sum = $alandminer_value + $apersonalsafety_value + $abirthregister_value + $aother_value;
$tstickstone_value = $tstickstone == 'true' ? 1 : 0;
$tohthers_value = $tohthers == 'true' ? 1 : 0;
$training_sum = $tstickstone_value + $tohthers_value;
$sbirthregister_value = $sbirthregister == 'true' ? 1 : 0;
$sother_value = $sother == 'true'? 1 : 0;
$social_mobilization_sum = $sbirthregister_value + $sother_value;
$content .= '
<tr>
<td>' . $district . '</td>
<td>' . $unioncouncil . '</td>
<td>' . $village . '</td>
<td>' . $recreational_sum . '</td>
<td>' . $awareness_raising_sum . '</td>
<td>' . $training_sum . '</td>
<td>' . $social_mobilization_sum . '</td>
<td style="color:red;">' . ($recreational_sum + $awareness_raising_sum + $training_sum + $social_mobilization_sum) . '</td>
</tr>';
}
$content .= '</tbody></table>';
$mpdf=new mPDF();
$mpdf->WriteHTML($content);
$mpdf->Output();
exit;
?>
Write these lines:
<td style='color:red;'><b><?php echo getTotalDistrictUnique();?></b></td>
like this:
<td style='color:red;'><b>".getTotalDistrictUnique()."</b></td>
Your code strucktur is build up as this one:
<?php
...
<?php // this is wrong, the opening PHP tag is still alive
... getTotalDistrictUnique()...
?>
?>
Your concatenation with php is not proper.
Please check below code and let me know if having any issue in comment box.
<?php
require_once 'components/app.php';
include('mpdf/mpdf.php');
$mpdf=new mPDF();
$html = "";
$html .= "
<table id='fixed_table' class='table table-bordered table-hover'>
<thead>
<tr>
<th>District</th>
<th>Union Council</th>
<th>Village</th>
<th>Recreational</th>
<th>Awareness Raising</th>
<th>Training</th>
<th>Social Mobilization</th>
<th>Total Activities</th>
</tr>
</thead>
<tbody>
<tr>
<td style='color:red;'><b>".getTotalDistrictUnique()."</b></td>
<td style='color:red;'><b>".getTotalUnionCouncilUnique()."</b></td>
<td style='color:red;'><b>".getTotalVillages()."</b></td>
<td style='color:red;'><b>".getTotalRecreational()."</b></td>
<td style='color:red;'><b>".getTotalAwareness()."</b></td>
<td style='color:red;'><b>".getTotalTraining()."</b></td>
<td style='color:red;'><b>".getTotalSocial()."</b></td>
<td style='color:red;'><b>".(getTotalRecreational() + getTotalAwareness() + getTotalTraining() + getTotalSocial())."</td>
</tr>";
include('connection.php');
$query ='select * from general';
$run =mysqli_query($con,$query);
while ($row=mysqli_fetch_array($run)) {
$id=$row[0];
$createdate=$row[1];
$createday=$row[2];
$partnername=$row[3];
$district=$row[4];
$unioncouncil=$row[5];
$village=$row[6];
$vannumber=$row[7];
$facilitator=$row[8];
$beneficiarytype=$row[9];
$rmultimedia=$row[10];
$rgame=$row[11];
$rsprort=$row[12];
$rart=$row[13];
$rgroupreading=$row[14];
$rother=$row[15];
$alandminer=$row[16];
$apersonalsafety=$row[17];
$abirthregister=$row[18];
$aother=$row[19];
$tstickstone=$row[20];
$tohthers=$row[21];
$sbirthregister=$row[22];
$sother=$row[23];
$formnumber=$row[24];
$submitdatatime=$row[25];
$html .= '<tr>';
$html .= '<td>'.$district;
$html .= '</td>';
$html .= '<td>'.$unioncouncil;
$html .= '</td>';
$html .= '<td>'.$village;
$html .= '</td>';
if($rmultimedia=='true')
{
$rmultimedia_value = 1;
}
else{
$rmultimedia_value = 0;
}
if($rgame=='true')
{
$rgame_value = 1;
}
else{
$rgame_value = 0;
}
if($rsprort=='true')
{
$rsprort_value = 1;
}
else{
$rsprort_value = 0;
}
if($rart=='true')
{
$rart_value = 1;
}
else{
$rart_value = 0;
}
if($rgroupreading=='true')
{
$rgroupreading_value = 1;
}
else{
$rgroupreading_value = 0;
}
if($rother=='true')
{
$rother_value = 1;
}
else{
$rother_value = 0;
}
$recreational_sum = $rmultimedia_value + $rgame_value + $rsprort_value + $rart_value + $rgroupreading_value + $rother_value;
$html .= '<td>'.$recreational_sum;
$html .= '</td>';
if($alandminer=='true')
{
$alandminer_value = 1;
}
else{
$alandminer_value = 0;
}
if($apersonalsafety=='true')
{
$apersonalsafety_value = 1;
}
else{
$apersonalsafety_value = 0;
}
if($abirthregister=='true')
{
$abirthregister_value = 1;
}
else{
$abirthregister_value = 0;
}
if($aother=='true')
{
$aother_value = 1;
}
else{
$aother_value = 0;
}
$awareness_raising_sum = $alandminer_value + $apersonalsafety_value + $abirthregister_value + $aother_value;
$html .= '<td>'.$awareness_raising_sum;
$html .= '</td>';
if($tstickstone=='true')
{
$tstickstone_value = 1;
}
else{
$tstickstone_value = 0;
}
if($tohthers=='true')
{
$tohthers_value = 1;
}
else{
$tohthers_value = 0;
}
$training_sum = $tstickstone_value + $tohthers_value;
$html .= '<td>'.$training_sum;
$html .= '</td>';
if($sbirthregister=='true')
{
$sbirthregister_value = 1;
}
else{
$sbirthregister_value = 0;
}
if($sother=='true')
{
$sother_value = 1;
}
else{
$sother_value = 0;
}
$social_mobilization_sum = $sbirthregister_value + $sother_value;
$html .= '<td>'.$social_mobilization_sum;
$html .= '</td>';
$html .= '<td style="color:red;">'.($recreational_sum + $awareness_raising_sum + $training_sum + $social_mobilization_sum);
$html .= '</td>';
$html .= '</tr>';
$html .= '</tbody>';
$html .= '</table>';
$mpdf->WriteHTML($html);
$mpdf->Output(); exit;
?>

print data with auto increment in paging

i know it's a silly question, but it is creating a problem for me....
I want to print data from mysql with auto increment, but when i use the below code, it auto increments the value in the same page but i want it to be continued to the next pages also, here is my code
<?php
$perpage = 10;
$start = (isset($_GET['id'])) ? $_GET['id'] : 0;
$TotalRec = mysql_result(mysql_query("SELECT COUNT(*) FROM register where
r_bid='".$_SESSION["id"]."'"), 0);
$select = "SELECT * FROM register where r_bid='".$_SESSION["id"]."' LIMIT
$start,$perpage";
$result = mysql_query($select) or die(mysql_error());
$res = mysql_query("select * from regi_balic where b_id='".$_SESSION["id"]."'");
$row1=mysql_fetch_array($res);
$i=1;
while($row = mysql_fetch_array($result))
{
echo '<tr>
<td align="center" width=5%><font size=3>'.$i.'</font></td>
<td width=12%><font size=3>'.$row1['name'].'</font></td>
<td align="center" width=5%><font size=3><a href="edit_detail.phpid='.$row["r_id"].'
&cand_id='.$_SESSION["id"].'&email='.$row["email"].'">'.$row['name'].'</a></font></td>
<td align="center" width=5%><font size=3>'.$row['reference'].'</font></td>
<td align="right" style="padding-right:8px" width=12%>
<fontsize=3>'.$row['age'].'</font></td>
<td align="right" style="padding-right:8px" width=12%>
<fontsize=3>'.$row['occupation'].'</font></td>
<td width=12%><font size=3>'.$row['mob_no'].'</font></td>
<td width=2%><a href="process_del_client.php?id='.$row['r_id'].'"
onClick="returnConfirmSubmit(\'Are You sure ?\')"><img src = "images/delete.png">
</a></td>
</tr>';
}
$i++;
echo '</table>';
if($start == 0)
{
echo "<br>Previous Page ";
}
else
{
echo '<br><a href='."./view.php?id=" . ($start - $perpage) . '>'.
"PreviousPage".'</a> ';
}
if($start + $perpage >= $TotalRec)
{
echo " Next Page<br>";
}
else
{
echo ' <a href='."./view.php?id=" . ($start + $perpage) . '>'."Next Page".'
</a><br>';
}
?>
update value in a session variable and use that onto another page for updation .
Ex..
$_SESSION['value']=0
$_SESSION['value']=$_SESSION['value']++;
try changing
$i=1;
to
$i=1+$start;

fail to manage checkbox using Pagination php using cookies

I want to manage pagination by using cookies.
I need to get values from selected checkboxe's and pass that values through pagination witch is done by php/mysql. And if on another page user select other checkboxes add their values to array of earlier seleced. For that i wrote one code but it did not work..What wrong with my code????
Following file is the manual.php file.
<script type="text/javascript" src="paginate.js"> </script>
<tr>
<td><strong>Select</strong></td>
<td width="59"><strong>Sr No.</strong></td>
<td width="300"><strong>Question </strong></td>
<td width="30"><strong>CorrectAnswer </strong></td>
<td width="50"><strong>Catagory </strong></td>
<td><strong>View Details</strong></td>
</tr>
<?php
include("config.php");
$start = 0;
$per_page = 10;
//$tablename = "testlist";
$targetpage = "manual.php?id=42";
if(!isset($_GET['page'])){
$page = 1;
} else{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
$sql="select * from math order by id";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = $num_rows / $per_page;
$sql .= " LIMIT $start, $per_page";
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result))
{ ?>
<tr>
<td><input name="<?php echo $row['id'];?>" type="checkbox" class="remember_cb"></td>
<td><center><?php echo htmlspecialchars_decode($row['id'], ENT_QUOTES); ?></center>
</td>
<td style="padding-left:25px;margin:15px"><?php echo $_SESSION['question']=
htmlspecialchars_decode($row['question'], ENT_QUOTES); ?></center>
</td>
<td><center><?php echo htmlspecialchars_decode($row['correctAnswer'], ENT_QUOTES); ?>
</center></td>
<td><center><?php echo htmlspecialchars_decode($row['category'], ENT_QUOTES); ?>
</center></td>
<td><a href="<?php echo "manual_update.php?id=".$row['id']."&mode=update";?
>"id="dialog_10">View</a></td>
</tr>
<?php } ?>
<tr>
<td><strong>Select </strong></td>
<td><strong>Sr No </strong></td>
<td><strong>Question </strong></td>
<td><strong>CorrectAnswer </strong></td>
<td><strong>Category </strong></td>
<td><strong>View Details</strong></td>
</tr>
<?php
if($page > 1){
$prev = $page - 1;
$prev = " <a href='$targetpage&page=$prev'>prev</a> ";
} else {
$prev = "";
}
if($page < $num_pages){
$next = $page + 1;
$next = " <a href='$targetpage&page=$next'>next</a> ";
}
else
{
$next = "";
}
echo $prev;
echo $next;
?>
paginate.js
var aa_checkbox;
function init_checkbox(){
//setup blank cb cookie
if(!Cookie.read('checkbox'))
{
Cookie.write('checkbox', JSON.encode({}));
}
//setup "associative array" to match what is currently in the cookie
aa_checkbox = JSON.decode(Cookie.read('checkbox'));
//set up each checkbox with class="remember_cb"
$$('input.remember_cb').each(function(el){
//mark checked if it is in the cookie
if(aa_checkbox[el.name]){
el.checked = 'checked'
}
//setup onclick event to put checkbox status in the
if(el.checked){
aa_checkbox[el.name] = 1;
}else{
delete(aa_checkbox[el.name]);
}
})
})
//save aa_checkbox back into cookie upon leaving a page
window.onbeforeunload = function(){Cookie.write('cb',
JSON.encode(aa_checkbox));};
setup_form();
return true;
}
function setup_form(){
//set up form so that it adds the inputs upon submit.
$$('form.remember_cb_form').each(function(form){
form.addEvent('submit', function(ev){
//clean up previously inserted inputs
var aa_hidden_insert = $$('input.hidden_insert');
$each(aa_hidden_insert, function(el){
el.parentNode.removeChild(el);
})
var el_form = this;
//insert hidden elements representing the values stored in aa_checkbox
$each(aa_checkbox, function(i_value, s_name){
if(i_value){
var el_input = document.createElement('input');
el_input.type = 'hidden';
el_input.value = i_value;
el_input.name = s_name;
el_input.setAttribute('class', 'hidden_insert');
el_form.appendChild(el_input);
}
});
});
});
}
window.addEvent('domready', init_checkbox);
function selectall()
{
var selectAll = document.forms[0].length;
if(document.forms[0].topcheckbox.checked == true)
{
for(i=1;i<selectAll;i++)
{
document.forms[0].elements[i].checked = true;
}
}
else
{
for(i=1;i<selectAll;i++)
document.forms[0].elements[i].checked = false;
}
}
function goSelect()
{
var select = document.forms[0].length;
var checkboxes=""
for(i=1;i<select;i++)
{
if(document.forms[0].elements[i].checked==true)
checkboxes+= " " + document.forms[0].elements[i].name
}
if(checkboxes.length>0)
{
var con=confirm("You have selected ...........");
if(con)
{
window.location.assign("submit.php?recsno="+checkboxes)
}
}
else
{
alert("No record is selected.")
}
}

pagination $_GET['page'] in php

I have a pagination question. When I load this page in the browser and click on the category link to show my products the first page shows with the products limited and the links for pagination are in place but it keeps sending me to the first page which is zero for whatever pagination link I click on. But the odd thing is is when I change
$offset=($pageNum - 1) * $perPage;
to
$offset=($pageNum)= $perPage;
if shows the rest of the products I'm trying to show after clicking on the category. So the problem might be in the page or somewhere around there.
Here is my code.
<?php
$productUlList="";
error_reporting (E_ALL ^ E_NOTICE);
include_once "convenientglobal2localhost.php";
$result = mysql_query("SELECT * FROM category WHERE 1")or die(mysql_error());
while($rowp=mysql_fetch_array($result)){
$categoryId=$rowp['catId'];
$categoryName=$rowp['catName'];
$productUlList.='
<ul id="ul" >
<li id="lists"> '.$categoryName.' </li>
</ul>';
}
?>
<?php
$msg_to_user3='';
$productList.='';
$categoryList='';
include_once "convenientglobal2localhost.php";
$perPage= 3;
if(isset($_GET['category']))
$categoryNames=$_GET['category'];
$pageNum=(isset($_GET['page']))? (int)$_GET['page']: 1;
$pages_query= mysql_query("SELECT * FROM products INNER JOIN category ON categoryName=catName WHERE categoryName='$categoryNames'");
$numrows= mysql_num_rows($pages_query);
$maxpages=ceil($numrows / $perPage);
$offset=($pageNum-1) * $perPage;
if ($offset < 0)
{
$offset = 0 ;
}
include_once "convenientglobal2localhost.php";
$results = mysql_query("SELECT * FROM products WHERE categoryName='$categoryNames' LIMIT $offset, $perPage")or die(mysql_error());
$num=mysql_num_rows($results);
if($num > 0){
while($row=mysql_fetch_array($results)){
$productId=$row['productId'];
$productName=$row['name'];
$productDescription=$row['description'];
$productPrice=$row['price'];
$productDiscountedPrice=$row['discountedPrice'];
$productStock=$row['stock'];
$productCategory=$row['categoryName'];
$categoryId=$row['catId'];
$catName=$row['catName'];
$categoryList='<table><th id="toptable"></th></table>
<table id="categorytable">
<th><img src="inventory_category_images/' . $categoryId . '.jpg" width="498px"; height="125px";/></th>
</table>';
$productList.='<table id="productoutputtable">
<tr>
<td rowspan="7" valign="top"><img style="border-style=solid; border-color:#767475; padding=; "src="inventory_images/' . $productId . '.jpg" width="150" height="135"/>
</td>
</tr>
<tr>
<td id="tablecolor" ><strong>Product</strong></td>
<td colspan="2">' . $productName . ' </td>
<td id="tablecolor"><strong>Category</strong></td>
<td>' . $productCategory . ' </td>
</tr>
<tr>
<td id="tablecolor"><strong>Description:</strong></td>
<td colspan="3">' . $productDescription . ' </td>
</tr>
<tr>
<td id="tablecolor" ><strong>Price:</strong></td>
<td>$' . $productPrice . ' </td>
</tr><tr>
<td id="tablecolor"colspan="1"><strong>Sale Price:</strong></td>
<td>$' . $productDiscountedPrice . ' </td>
<td id="tablecolor"colspan="2"><strong>In Stock </strong></td>
<td>' . $productStock . ' </td>
</tr>
</table>';
}
$self= $_SERVER['PHP_SELF'];
for($page=1; $page<=$maxpages; $page++){
if($page == $pageNum){
$nav= "$page";
}
else{
$nav= "$page";
}
}
if($page > 1)
{
$page=$pageNum-1;
$prev ="[Prev]";
$first="[First Page]";
}
else
{
$prev= "&nbsp";
$first="&nbsp";
}
if($pageNum < $maxPages)
{
$page=$pageNum+1;
$next ="[Next]";
$last="[Last Page]";
}
else
{
$next= "&nbsp";
$last="&nbsp";
}
$pageList.= $first. $prev. $nav. $next. $last;
}
else{
$msg_to_user3="You have no products listed.";
}
//$pageList.="";
//for($i = 0 ; $i <= $maxpages ; $i++) {
//if($i == $page) {
//$pageList.= "<B>$i</B>";
//}else{
//$pageList.= ''.$i.'';
//
//}
//}
?>
Thanks for all you help!!!
I'll ignore the inefficiencies (refer to comments on your question). The problem is not your offsetting code—that works fine. Your links are broken.
When generating your numbered links into $nav, you need to append, not overwrite. Use .=, not =. Also, beware of capitalization. $maxpages is not $maxPages.
Here's updated code. Proof this works. Unless your database query is misconstructed (I can't test that, sorry!), you should be good to go.
$self= $_SERVER['PHP_SELF'];
for($page=1; $page<=$maxpages; $page++){
if($page == $pageNum){
$nav.= "$page";
}
else{
$nav.= "$page";
}
}
if($page > 1)
{
$page=$pageNum-1;
$prev ="[Prev]";
$first="[First Page]";
}
else
{
$prev= "&nbsp";
$first="&nbsp";
}
if($pageNum < $maxpages)
{
$page=$pageNum+1;
$next ="[Next]";
$last="[Last Page]";
}
else
{
$next= "&nbsp";
$last="&nbsp";
}
$pageList.= $first. $prev. $nav. $next. $last;

How can I get the selected category id after 1st page in pagination?

<?php
include "includes/connection.php";
//$id=$_REQUEST['category'];
//$catid=mysql_escape_string($id);
$catid = isset($_GET['category']) ? (int)$_GET['category'] : 0;
$recordsPerPage =4;
# 0
// //default startup page
$pageNum = 1;
if(isset($_GET['p']))
{
$pageNum = $_GET['p'];
settype($pageNum, 'integer');
}
$offset = ($pageNum - 1) * $recordsPerPage;
//set the number of columns
$columns = 1;
//set the number of columns
$columns = 1;
$query = "SELECT temp_id, temp_img, temp_header, temp_resize, temp_small, temp_name, temp_type, cat_id, col_id, artist_id FROM `templates` where cat_id = '{$catid}' ORDER BY `temp_id` DESC LIMIT $offset, $recordsPerPage";
$result = mysql_query($query);
//we add this line because we need to know the number of rows
$num_rows = mysql_num_rows($result);
echo "<div>";
//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $num_rows; $i++) {
while($row = mysql_fetch_array($result)){
if($i % $columns == 0) {
//if there is no remainder, we want to start a new row
echo "<div class='template'>";
}
echo ...........my data(s).
if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
echo "</div>";
}
}
}
echo "</div>";
//}
?>
<div class="pagination">
<?
$query = "SELECT COUNT( temp_id ) AS `temp_date` FROM `templates` where cat_id ='{$catid}'";
$result = mysql_query($query) or die('Mysql Err. 2');
$row = mysql_fetch_assoc($result);
$numrows = $row['temp_date'];
//$numrows = mysql_num_rows($result);
$self = $_SERVER['PHP_SELF'];
$maxPage = ceil($numrows/$recordsPerPage);
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{ if ($page == $pageNum)
{
$nav .= "<span class=\"pegination-selected\">$page</span>";
}
else
{
$nav .= "<aa class=\"pegination\" hreeef=\"javascript:htmlData('$self','p=$page')\">$page</a>";
}
}
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = "<aa class=\"pagination\" hreeef=\"javascript:htmlData('$self','p=$page')\"><strong><imgee src=\"images/previous.gif\" alt=\"previous\" width=\"5\" height=\"10\" border=\"0\"/></strong></a>";
$first = "<aa class=\"pagination\" hreeef=\"javascript:htmlData('$self','p=1')\"><strong><imgee src=\"images/previous1.gif\" alt=\"first\" width=\"7\" height=\"10\" border=\"0\" /></strong></a>";
}
else
{
$prev = '<strong> </strong>';
$first = '<strong> </strong>';
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = "<aa hreeef=\"javascript:htmlData('$self','p=$page')\"> <strong> <imgee src=\"images/next.gif\" alt=\"next\ width=\"5\" height=\"10\" border=\"0\" /></strong></a>";
$last = "<a class=\"pagination\" hreeef=\"javascript:htmlData('$self','p=$maxPage')\"> <strong> <imgee src=\"images/next1.gif\" alt=\"next\" border=\"0\" width=\"7\" height=\"10\" /></strong></a>";
}
else
{
$next = '<strong> </strong>';
$last = '<strong> </strong>';
}
echo "<div class=\"pagination\"> $first $prev <span class=\"pagination-selected\">$nav </span> $next $last </div>";
?>
Here my ajax code:
function GetXmlHttpObject(handler)
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtResult").innerHTML=xmlHttp.responseText
}
else
{
//alert(xmlHttp.status);
}
}
function htmlData(url, qStr)
{
if (url.length==0)
{
document.getElementById("txtResult").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
url=url+"?"+qStr;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true) ;
xmlHttp.send(null);
}
How can I get the selected category id after 1st page in pagination?
Do you pass though category in the request? You haven't given us that information (what is the value of qstr in the javascript?), but I'd guess not.
You're also passing it straight into an SQL query, which leaves you open to injection.
You should use mysql_escape_string() to fix that.
Post it with the AJAX call and return it
Store it in a local JS variable
Add it to the URL
...
You seem to be aware that $_GET['p'] gets the value of the 'p' parameter passed in the querystring. Well $_REQUEST['category'] is doing the same thing. (Technically $_REQUEST checked everything in $_POST, $_GET and $_COOKIE).
So if you haven't set the 'category' in the querystring then it wont contain anything in your code.
You should add ?category=XXX&sid=RAND... to your url
Better use $category = isset($_GET['category']) ? (int)$_GET['category'] : 0;
<?php
include ('database connection file ');
?>
**and this is my index.php**
<?php
if(isset($_GET['page']))
{
$page=$_GET['page'];
$offset=$limit * ($page - 1);
}
else{
$page=1;
$offset=0;
}
if($page==0 || $page > $page_rec){
header('location:index.php?page=1');
}
?>
<body>
<div>
<div class="headingSec"> <h1 align="center">Welcome AdminLogout</h1>
<p align="center">Manage your student database here.</p></div>
<table class="trBgColr">
<thead class="bgColorSec">
<th>Registration Id</th>
<th>Image</th>
<th>Signature</th>
<th>Name</th>
<th>Father's Name</th>
<th>City</th>
<th>Registration Category</th>
<th>Phone Number</th>
<th>Mobile Number</th>
<th>Status</th>
<th>
</thead>
<?php
//$getstudentdetails = "select * from student_details";
$result=mysqli_query($conn,"select * from `student_details` LIMIT $offset,$limit");
/* fetch associative array */
while($row = mysqli_fetch_array($qry)) {
?>
<tr>
<td><?php echo $row["registration_number"]; ?>
<td><?php echo $row["Name"]; ?></td>
<td><?php echo $row["Father_Name"]; ?></td>
<td><?php echo $row["City"]; ?></td>
<td><?php echo $row["Registration_Category"]; ?></td>
<td><?php echo $row["Phone_Number"]; ?></td>
<td><?php echo $row["Mobile_Number"]; ?></td>
<?php if($row['payment_status']==1)
{?>
<td><?php echo "Sucsess"; ?></td>
<?php
}
else{
?>
<td><?php echo "Failed"; ?></td>
<?php
}?>
<?php
}
$pre=$page - 1;
$next=$page + 1;
?>
</tr>
</table>
</div>
<br />
<br />
<div class="pagination">
<?php
for($i=1;$i<=$page_rec;$i++){
continue;
?>
<?php $i;?>
<?php } ?>
Previous«
Next»

Categories