这个问题对一些人来说是一个简单的问题,但我无法理解我需要做些什么来解决这个问题。
我有一个显示分页表格的网页。表格上方是上一页的会话传递的一些信息,用户从下拉菜单中选择了一个选项。
当用户分页到第二页等时,传递的信息丢失,可能是页面刷新。
我在Ajax,jquery和php等上搜索并观看了大量的教程(超过20个),但我无法理解我在看什么。当我按照教程,我无法解释一个可以解决我的问题。
我想一步一步的指导将是解决这个问题的唯一方法,而不是让某人为我做这件事。
我非常感谢这件事的任何帮助。
我尝试传递给分页中第二页的代码是变量$brandname
,$picked
,$pickchek
。代码的php部分列在下面。
echo "<div id=firstpick>";
$brandname = $_GET['brandname'];
$picked = $_GET['picked'];
$pickcheck = $_GET['pickcheck'];
$brands =($brandname);
$_SESSION['$brandname']= $brandname;
$pick =($picked);
$_SESSION['$picked']= $pick;
$picker =($pickcheck);
$_SESSION['$pickcheck']=$picker;
echo "</div>";
$tbl_name="pickme";
$adjacents = 3;
$query = "SELECT COUNT(*) as num FROM tirestock";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages["num"];
$targetpage = "connecttest.php"; //your file name (the name of this file)
$limit = 5; //how many items to show per page
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$page=mysql_real_escape_string($page);
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0;
$sql = "SELECT * FROM tirestock LIMIT $start, $limit";
$result = mysql_query($sql);
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
$lpm1 = $lastpage - 1; //last page minus 1
$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
{
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";
}
echo "<div style='font-weight:bold' text-align:center;><font size='5'>You picked Brand:
$brandname<br></br></hr></div>";
echo "<div style='font-weight:bold'><font size='5'>You picked Size: $picked<br></br></hr>
</div>";
echo "<div style='font-weight:bold'><font size='5'>You picked Type: $pickcheck<br></br>
</hr></div>";
答案 0 :(得分:1)
将品牌名称,挑选和挑选附加到分页脚本创建的链接。
<a href=\"$targetpage?page=$prev&brandname=$brandname&picked=$picked&pickcheck=$pickcheck\">« previous</a>`
答案 1 :(得分:0)
您可以设置session中页面之间丢失的数据,或者更好的解决方案是使用ajax进行分页。这个想法非常简单,你需要在你的分页链接上附加javascript中的点击处理程序,以使用ajax加载页面数据。如果您使用的是jquery,它会为您完成所有艰苦的工作。阅读jquery .ajax()或更简单的.load()方法,他们会有所帮助。
如果您的javascript知识有限,快速解决方案将是使用PHP会话在页面之间保留数据,如果您可以了解它,那么更好的体验就是使用ajax。理想情况下,您将实现两者,与渐进增强保持一致,以便它可以在禁用javascript的情况下使用。
编辑:
点击处理程序的一个简单的jquery示例是:
$(".pagination a").click(function(e){ // this line binds the handler to your pagination
e.preventDefault(); //this prevents the link being loaded as a new page, the default behaviour
var pageToLoad = $(this).attr("href"); //we get the url of the page to load in by ajax
$("#page-contents").load(pageToLoad); //replace the contents of the div with the id="page-contents" with the result of the ajax request.
});
这将要求您首先包含jquery库,并将您要使用id#page-contents的元素内的分页替换为主要内容。请注意,分页链接中的href必须只返回要替换的页面段,而不是整个html页面。您可以通过从您调用的网址返回一个php块,或者只使用jquery load method
加载某个ID中的完整html页面块来执行此操作答案 2 :(得分:0)
你知道如何在Javascript中使用localStorage吗?这是一个如何让它在php中为你工作的教程。 http://www.devshed.com/c/a/PHP/Create-A-ClientSide-Cache-in-PHP/1/
您可以使用它在页面更改之间存储变量。
当然,如果你没有JS,那么这将不会非常有用!