Hi,
I am a rookie on php and this is my first php site. I am facing a pagination issue with my site and I hope I can get help here. I've find a pagination code from
internet and it is work for me if my php page just show all record from mysql and don't let user to filter the data. But after I merged the pagination code with my php page (add HTML drop-down and checkbox), The pagination is only work on first page, and when I click on other pages, no data show up.
I've tried to use $_GET install of $_POST and also define some variable using
$_SESSION variable but still got the same issue. Following is my code, maybe a bit long (2xx lines), anyone can advise me how can I fix my issue? Thx
<?php
if(!isset($_SESSION)) {
session_start();
}
print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
print '<html>';
print '<head>';
print '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />';
print '<meta http-equiv="Content-Style-Type" content="text/css" />';
print '<title>result</title>';
print '</head>';
print '<body>';
/*
Place code to connect to your DB here.
*/
$link_ID = mysql_connect("127.0.0.1","???","???");
mysql_set_charset('utf8',$link_ID);
mysql_select_db("???");
$str_get_district="SELECT * FROM district;";
$result_get_district = mysql_query($str_get_district,$link_ID);
$sn_district = mysql_num_rows($result_get_district);
?>
<h2>pls select your choice</h2>
<hr>
<?php
if (isset($_POST["leisure"])) {
foreach ($_POST["leisure"] as $selectedleisure)
$selected[$selectedleisure] = "checked";
}
// include('config.php'); // include your code to connect to DB.
$tbl_name="shop"; //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;
?>
<form method=post name=main>
pls select your district :
<select name=sel_district size=1>
<option value=0>All districts</option>
<?php
for ($index=0; $index < $sn_district; $index++) {
$arr[$index] = mysql_fetch_array($result_get_district);
echo "<option value={$arr[$index]['id']}";
if (isset($_POST["sel_district"]) && $_POST["sel_district"] == $arr[$index]['id']) {
echo " selected='selected'";
}
echo ">{$arr[$index]['name']}</option>";
}
?>
</select>
<hr>
select item(s) : <br>
<input type="checkbox" name="leisure[]" <? echo $selected['1'] ?> value=1 />All<br>
<input type="checkbox" name="leisure[]" <? echo $selected['2'] ?> value=2 />Item 1<br>
<input type="checkbox" name="leisure[]" <? echo $selected['3'] ?> value=3 />Item 2<br>
<input type="checkbox" name="leisure[]" <? echo $selected['4'] ?> value=4 />Item 3<br>
<input type="checkbox" name="leisure[]" <? echo $selected['5'] ?> value=5 />Item 4<br>
<br>
<input type="submit" value="Search" />
</form>
<hr>
<table border="1" width="1920">
<col width="60" />
<col width="200" />
<col width="60" />
<col width="200" />
<col width="100" />
<col width="300" />
<col width="300" />
<tr>
<th>Type</th>
<th>Name</th>
<th>District</th>
<th>Address</th>
<th>Telephone</th>
<th>Info 1</th>
<th>Info 2</th>
</tr>
<?php
/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
/* Setup vars for query. */
$targetpage = "main.php"; //your file name (the name of this file)
$limit = 20; //how many items to show per page
$page = $_GET['page'];
if($page)
$start_from = ($page - 1) * $limit; //first item to display on this page
else
$start_from = 0; //if no page var is given, set start to 0
/* Get data. */
// $sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit";
$list_district = $_POST["sel_district"];
if (count($_POST["leisure"]) == 0) {
echo "Pls select at least one item.";
}
for ($index=0; $index < count($_POST["leisure"]); $index++) {
$arr_item_type[$index]=$_POST["leisure"][$index];
if ($list_district == 0 && $arr_item_type[0] == 1) {
$str_list_shop="SELECT shoptype.name as stype,shop.name as sname,district.name as dname,address,phone,info,info2 FROM shop
LEFT JOIN shoptype ON shop.type=shoptype.id
LEFT JOIN district ON shop.district=district.id
ORDER BY dname,stype ASC LIMIT $start_from, $limit;";
}
else if ($list_district == 0 && $arr_item_type[0] > 1) {
$str_list_shop="SELECT shoptype.name as stype,shop.name as sname,district.name as dname,address,phone,info,info2 FROM shop
LEFT JOIN shoptype ON shop.type=shoptype.id
LEFT JOIN district ON shop.district=district.id
WHERE type='$arr_item_type[$index]'
ORDER BY dname,stype ASC LIMIT $start_from, $limit;";
}
else if ($list_district != 0 && $arr_item_type[0] == 1) {
$str_list_shop="SELECT shoptype.name as stype,shop.name as sname,district.name as dname,address,phone,info,info2 FROM shop
LEFT JOIN shoptype ON shop.type=shoptype.id
LEFT JOIN district ON shop.district=district.id
WHERE district='$list_district'
ORDER BY dname,stype ASC LIMIT $start_from, $limit;";
}
else {
$str_list_shop="SELECT shoptype.name as stype,shop.name as sname,district.name as dname,address,phone,info,info2 FROM shop
LEFT JOIN shoptype ON shop.type=shoptype.id
LEFT JOIN district ON shop.district=district.id
WHERE district='$list_district' AND type='$arr_item_type[$index]'
ORDER BY dname,stype ASC LIMIT $start_from, $limit;";
}
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">? previous</a>";
else
$pagination.= "<span class=\"disabled\">? previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">next ?</a>";
else
$pagination.= "<span class=\"disabled\">next ?</span>";
$pagination.= "</div>\n";
}
$result_get_shop = mysql_query($str_list_shop,$link_ID);
$sn_shop = mysql_num_rows($result_get_shop);
for ($count=0; $count < $sn_shop; $count++) {
$arr_list_shop[$count] = mysql_fetch_array($result_get_shop);
echo "<tr>";
echo '<td>'.$arr_list_shop[$count]['stype'].'</td>';
echo '<td>'.$arr_list_shop[$count]['sname'].'</td>';
echo '<td>'.$arr_list_shop[$count]['dname'].'</td>';
echo '<td>'.$arr_list_shop[$count]['address'].'</td>';
echo '<td>'.$arr_list_shop[$count]['phone'].'</td>';
echo '<td>'.$arr_list_shop[$count]['info'].'</td>';
echo '<td>'.$arr_list_shop[$count]['info2'].'</td>';
echo "</tr>";
}
}
echo "</table>";
?>
<?=$pagination?>
<?
//mysql_close($link_ID);
print '</body>';
print '</html>';
?>