2013-05-10 50 views
1

我無法使用此分頁腳本維護GET變量。希望你們都能幫忙。使用分頁與Mysql查詢

我正在使用GET函數,因此用戶可以選擇顯示哪些類別。我使用的是我在網上找到的分頁腳本,當我選擇「第2頁」時,我現在遇到了這個問題。$ dynCat不再在URL中解析,因爲分頁腳本創建了一個新的URL並且也使用了GET。

有人可以幫助我與變量查詢使用分頁嗎?謝謝。

錯誤注意:未定義的索引:dynCat ----/dynCat.php 32行 我明白爲什麼它是未定義的,我只是不知道如何整合和維護用戶的查詢(即GET變量來自URL )與分頁腳本。再次感謝。

第1頁的index.php(用戶選擇的選項)

<?php 
//Generate and list Categories include "storescripts/connect_to_mysql.php"; 
$dynCat = ""; 
$data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error()); 
while($info = mysql_fetch_array($data)) 
{ 
$listcategory = $info["category"]; 
$dynCat .= 
' 
<li> 
<a href="dynCat.php?dynCat='.$listcategory.'" > '.$listcategory.' </a> 
</li> 
'; 
} 
mysql_close(); 
?> 
<?php echo $dynCat; ?> 

第二頁dynCat.php(與分頁用戶視圖選擇)

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
?> 
<?php 
//Generate and list Categories 
include "storescripts/connect_to_mysql.php"; 
$dynGallery = ""; 
$dynCat = ""; 
$data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error()); 
while($info = mysql_fetch_array($data)) 
{ 
$listcategory = $info["category"]; 
$dynCat .= 
' 
<li> 
<a href="dynCat.php?dynCat='.$listcategory.'" > '.$listcategory.' </a> 
</li> 
'; 
} 
mysql_close(); 
?> 
<?php 
//Query User Selection & Pagination 
include('storescripts/connect_to_mysql.php'); 
$cat = mysql_escape_string($_GET['dynCat']); 
$tableName="products"; 
$targetpage = "dynCat.php"; 
$limit = 3; 
$query = "SELECT COUNT(*) as num FROM $tableName WHERE category = '$cat'"; 
$total_pages = mysql_fetch_array(mysql_query($query)); 
$total_pages = $total_pages['num']; 
$stages = 3; 
$page = mysql_escape_string(isset($_GET['page'])) ? mysql_escape_string($_GET['page']) : 0; 
if($page){ 
$start = ($page - 1) * $limit; 
}else{ 
$start = 0; 
} 
// Get category Data and Images 
$query1 = "SELECT * FROM $tableName WHERE category = '$cat' ORDER BY views DESC LIMIT $start, $limit"; 
$result = mysql_query($query1); 
// Initial page num setup 
if ($page == 0){$page = 1;} 
$prev = $page - 1; 
$next = $page + 1; 
$lastpage = ceil($total_pages/$limit); 
$LastPagem1 = $lastpage - 1; 
$paginate = ''; 
if($lastpage > 1) 
{ 
$paginate .= "<div class='paginate'>"; 
// Previous 
if ($page > 1){ 
$paginate.= "<a href='$targetpage?page=$prev#gallery'>previous</a>"; 
}else{ 
$paginate.= "<span class='disabled'>previous</span>"; } 
// Pages 
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up 
{ 
for ($counter = 1; $counter <= $lastpage; $counter++) 
{ 
if ($counter == $page){ 
$paginate.= "<span class='current'>$counter</span>"; 
}else{ 
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";} 
} 
} 
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few? 
{ 
// Beginning only hide later pages 
if($page < 1 + ($stages * 2)) 
{ 
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++) 
{ 
if ($counter == $page){ 
$paginate.= "<span class='current'>$counter</span>"; 
}else{ 
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";} 
} 
$paginate.= "..."; 
$paginate.= "<a href='$targetpage?page=$LastPagem1#gallery'>$LastPagem1</a>"; 
$paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>"; 
} 
// Middle hide some front and some back 
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2)) 
{ 
$paginate.= "<a href='$targetpage?page=1#gallery'>1</a>"; 
$paginate.= "<a href='$targetpage?page=2#gallery'>2</a>"; 
$paginate.= "..."; 
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++) 
{ 
if ($counter == $page){ 
$paginate.= "<span class='current'>$counter</span>"; 
}else{ 
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";} 
} 
$paginate.= "..."; 
$paginate.= "<a href='$targetpage?page=$LastPagem1#gallery'>$LastPagem1</a>"; 
$paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>"; 
} 
// End only hide early pages 
else 
{ 
$paginate.= "<a href='$targetpage?page=1#gallery'>1</a>"; 
$paginate.= "<a href='$targetpage?page=2#gallery'>2</a>"; 
$paginate.= "..."; 
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++) 
{ 
if ($counter == $page){ 
$paginate.= "<span class='current'>$counter</span>"; 
}else{ 
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";} 
} 
} 
} 
// Next 
if ($page < $counter - 1){ 
$paginate.= "<a href='$targetpage?page=$next#gallery'>next</a>"; 
}else{ 
$paginate.= "<span class='disabled'>next</span>"; 
} 
$paginate.= "</div>"; 
} 
mysql_close(); 
?> 
<?php 
$productCount = mysql_num_rows($result); // count the output amount 
while($row = mysql_fetch_array($result)) 
{ 
$id = $row["id"]; 
$product_name = $row["product_name"]; 
$price = $row["price"]; 
$category = $row["category"]; 
$subcategory = $row["subcategory"]; 
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); 
$dynGallery .= 
' 
<div> 
<li> 
<a href="product.php?id=' . $id . '#gallery"> 
<img style="border:#FFF 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="180" height="255" border="1" style="opacity: 0;/></a> 
<h4> <a name= ' . $id . ' id= ' . $id . ' value= ' . $id . ' href="product.php?id= ' . $id . '#gallery"> ' . $product_name . ' </a> </h4> 
<p>'. $category .'</p> 
<p>'. $subcategory .'</p> 
<span> $' . $price . '</span> 
</li> 
</div> 
'; 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<body> 
<?php include ("../header.php");?> 
<?php include ("../menu.php");?> 
<a id="shop"></a> 
<div class="body"> 
<div class="sidebar"> 
<div class="first"> 
<h2><a href="index.php">Dora Designs</a></h2> 
<!-- Dynamic Categories --> 
<ul> 
<?php echo $dynCat; ?> 
</ul> 
</div> 

</div> 
<div class="content"> 
<div class="figure"> 
<img src="/images/galleryholder.png" alt=""/> 
</div> 
<div class="products"> 
<div class="paging" align = "center"> 
<a id="gallery"></a> 
<? echo $paginate;?> 
<? echo '</br>'. $cat;?> 
<? echo '</br>'. $total_pages.' Results'; ?> 
</div> 
<ul> 
<?php echo $dynGallery; ?> 
</ul> 

我討厭在後傾這麼多的代碼,但我我真的很茫然,以前從未使用過分頁。任何幫助將不勝感激。

回答

0

變化

$targetpage?page= 

$targetpage?dynCat=$cat&page= 

你想$貓一個2份爲DB不斷實例都需要用函數mysql_escape_string爲用urlencode的URL逃逸()其他()

更換線

$cat = mysql_escape_string($_GET['dynCat']); 

$cat=urldecode($_GET['dynCat']); //from the url, raw value to display 
$cat_mysql = mysql_escape_string($cat); //use in mysql queries 
$cat_url=urlencode($cat); //use in the url 

然後

$ targetpage?dynCat = $貓&頁=

現在將

$ targetpage?dynCat = $ cat_url &頁=

+0

你可能需要urlencode()然後urldecode()$ cat var – 2013-05-10 02:46:06

+0

這正是我需要做的。但是,我相信'$ cat = mysql_escape_string($ _ GET ['dynCat']); '正在導致問題,因爲我的查詢不再有效。 '<?php echo $ cat?>'不再包含適當命名類別中的撇號。該類別的名稱是Circ'(最初回顯爲Circ /'),但是當我點擊下一頁後回顯該變量時,它將顯示Circ //。我相信我會嘗試自己修復它。但是我想知道如何處理這個問題。非常感謝你。 – cheelo007 2013-05-10 02:46:46

+0

我的意思不是要勺子喂,但我會如何實現urlencode? – cheelo007 2013-05-10 02:57:42

0

嘗試將dynCat=$cat添加到您看起來像這樣的所有鏈接 - <a href='$targetpage?page=...

<a href='$targetpage?dynCat=$capage=... 

在快速搜索有約。其中14個要改變。