2013-05-18 122 views
-3

以下代碼是我正在處理的電子商務網站。分頁爲查詢的第一頁選擇正確的東西,但在第二頁上選擇錯誤的東西。我究竟做錯了什麼?任何幫助將不勝感激分頁無法正常工作

<?php ob_start(); 

include "config.php"; 
function currency($from_Currency,$to_Currency,$amount) { 
    $amount = urlencode($amount); 
    $from_Currency = urlencode($from_Currency); 
    $to_Currency = urlencode($to_Currency); 
    $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency"; 
    $ch = curl_init(); 
    $timeout = 0; 
    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    $rawdata = curl_exec($ch); 
    curl_close($ch); 
    $data = explode('"', $rawdata); 
    $data = explode('.', $data['3']); 
    $data[0] = str_replace(" ", "",preg_replace('/\D/', '', $data[0])); 
    if(isset($data[1])){ 
    $data[1] = str_replace(" ", "",preg_replace('/\D/', '', $data[1])); 
    $var = $data[0].".".$data[1];   
    } else{ 
    $var = $data[0]; 
    } 
    return round($var,2); } 
?> 
<?php require_once("../include/membersite_config.php"); ?> 
<?php 
    // Create connection 
$con=mysqli_connect("RichWebmasterReg.db.10749243.hostedresource.com","RichWebmasterReg","[email protected]","RichWebmasterReg"); 

// Check connection 
if (mysqli_connect_errno($con)) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
?> 
<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Products</title> 
<link href="../jscss/pproducts.css" rel="stylesheet" type="text/css"> 
<link href="../jscss/main.css" rel="stylesheet" type="text/css"> 
<link href="../jscss/heads.css" rel="stylesheet" type="text/css"> 
<link href='http://fonts.googleapis.com/css?family=Oswald:400,700,300' rel='stylesheet' type='text/css'> 
</head> 

<body> 

<?php 
require('../nmheader.php'); 
?> 
<br><br><br><br> 
<?php 


///////////////////Pagination///////////////////////////////////// 
$perpage = 8;             // 
$pages_query = mysql_query("SELECT count(p_name) from product ");// 
$pages = ceil(mysql_result($pages_query, 0)/ $perpage);   // 
$page = (isset($_GET['page']))? (int)$_GET['page']:1;   // 
$start = ($page - 1) * $perpage;         // 
/////////////////// EndPagination////////////////////////////////// 
//$query="SELECT * FROM product ORDER by date_added DESC LIMIT $start, $perpage"; 
session_start(); 

$category1 = $_REQUEST['categorys']; 
switch($category1) 
{ 
case $category1 == 'ALL': 
$category = 'ALL'; 
$query="SELECT * FROM product ORDER by date_added DESC LIMIT $start, $perpage"; 
$sel = 1; 
break; 

case $category1 == 'BOTTLES': 
$category = 'BOTTLES'; 
$sel = 2; 
$query="SELECT * FROM product WHERE cat = 'bottles' ORDER by date_added DESC LIMIT $start, $perpage"; 
break; 

case $category1 == 'EXERCISE APPAREL': 
$category = 'EXERCISE APPAREL'; 
$query="SELECT * FROM product WHERE cat = 'exercise apparel' ORDER by date_added DESC LIMIT $start, $perpage"; 
$sel = 4; 
break; 

case $category1 == 'TOP SELLERS': 
$category = 'TOP SELLERS'; 
$query="SELECT * FROM product WHERE cat = 'Top sellers' ORDER by date_added DESC LIMIT $start, $perpage"; 
$sel = 6; 
break; 

case $category1 == 'MUSCLE FORMULAS': 
$category = 'MUSCLE FORMULAS'; 
$query="SELECT * FROM product WHERE cat = 'muscle formulas'ORDER by date_added DESC LIMIT $start, $perpage"; 
$sel = 5; 
break; 

case $category1 == 'SPECIALS': 
$category = 'SPECIALS'; 
$query="SELECT * FROM product WHERE cat = 'specials' ORDER by date_added DESC LIMIT $start, $perpage"; 
$sel = 7; 
break; 

case $category1 == 'SUPPLIMENTS': 
$category = 'SUPPLIMENTS'; 
$query="SELECT * FROM product WHERE cat = 'suppliments' ORDER by date_added DESC LIMIT $start, $perpage"; 
$sel = 3; 
break; 

default: 
$query="SELECT * FROM product ORDER by date_added DESC LIMIT $start, $perpage"; 
} 

$result = mysql_query($query); 
$total = mysql_num_rows($result); 
$showing = $perpage > $total? $total: $perpage; 
?> 

<div style="font-family:Oswald; font-size:120%"><?php echo $category;?> </div> 
<form action="products.php" method="post" > 
<select name="categorys" style="padding:0px !important; font-size:12px !important; "> 
<option value="ALL" <?php if($sel == 1)echo 'selected';?> >All</option> 
<option value="BOTTLES" <?php if($sel == 2)echo 'selected';?>> Bottles</option> 
<option value="SUPPLIMENTS" <?php if($sel == 3)echo 'selected';?>>Supplements</option> 
<option value="EXERCISE APPAREL" <?php if($sel == 4)echo 'selected';?>> Exercise Apparel</option> 
<option value="MUSCLE FORMULAS" <?php if($sel == 5)echo 'selected';?>>Muscle Formulas</option> 
<option value="TOP SELLERS" <?php if($sel == 6)echo 'selected';?>>Top Sellers</option> 
<option value="SPECIALS" <?php if($sel == 7)echo 'selected';?>>Specials</option> 
</select> 

<input type="submit" value="Submit" > 
</form> 
<div style="background-color:#CCC; width:1064PX">Showing <?=$showing?> of <?=$total?> Results</div> 
<?php 



$result = mysqli_query($con,$query); 
$total = mysqli_num_rows($result); 
echo "<table width='1064px' cellspacing='10px' >"; 
echo '<tr width:210px;" height="40%">'; 

    session_start(); 
for ($i = 1; $row = mysqli_fetch_assoc($result); $i++) { 
    $name = $row["p_name"];$name = strtoupper($name); 
    $number = $row["p_number"]; 
    $price = $row["p_price"]; 
    $url = $row["p_url"]; 
    $manufacturer = $row["p_man"]; 
    $p_desc_link = $row["p_desc_link"]; 
    if($_SESSION['mon'] = 'JMD') 
    { 
    $price = currency(usd,jmd,$price); 
    } 

    if($_SESSION['mon'] = 'USD') 
    { 
    $price = $row['p_price']; 
    } 

$cur = $_SESSION['mon']; 

    $user = $_SESSION['user']; 
    echo "<td> 
     <form action='http://definitionxjm.com/product_page.php' method='post'> 
     <input type='hidden' name='inum' value='$number'> 
     <input type='image' src='http://www.definitionxjm.com/$url' title='More Details' alt='More Details'> 
     </form> 
     <br> 
     <div class='productname' ><a target='_blank' style=' color:#727172; text-decoration:none !important;' href='$p_desc_link'>$name</a></div> 
     <br> 
     <div class='productdetails'> 
      Manufacturer: $manufacturer<BR> 
      Item number : $number<br> 
     </div> 
     <br> 
     <div class='PRODUCTPRICE'>$cur $price</div><br><br> 
     <div width='210px'> 
     <div style='width:104px; float:left'> 
     <form action='http://definitionxjm.com/shopping/renderCart.php' method='post' > 
     <input type='hidden' name='user' value='$user'> 
     <input type='hidden' name='quantity' value='1'> 
     <input type='hidden' name='itemNum' value='$number'> 
     <input type='image' src='../images/ADD-TO-CART.png' width='87' height='36'> 
     </form> 
     </div> 

     <div style='width:104px; float:right'> 
     <form action='http://definitionxjm.com/product_page.php' method='post'> 
     <input type='hidden' name='inum' value='$number'> 
     <input type='image' src='../images/DETAILS.png' width='87' height='36'> 
     </form> 
     </div> 
     </div> 
    </td> 
    ";      
    if (($i % 4 == 0) && ($i < $total)) { 
     echo '</tr><tr width:20%;" height="40%">'; 

} 
} 
echo '</tr>'; 
echo '</table>'; 



    if($pages >= 1 && $page <=$pages) 
    { 
     for($x = 1; $x<= $pages; $x++) 
     { 
      echo '<a href="?page='.$x.'">'.$x.'</a> '; 
     } 
    } 

?> 



</div> 
<?php require('../footer.php'); ?> 
</body> 
</html> 
+0

您的變量$ start將爲$ start =($ page - 1)* $ perpage + 1; –

+1

你需要再次發送'categorys'頁碼在錨標記..另外當你點擊第二頁時,它總是會出現開關情況下的默認情況 – alwaysLearn

+0

[**請勿使用'mysql_ *'函數在新代碼中**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://www.brightmeup.info/article.php?a_id=2)。 –

回答

1

您沒有設置正確的偏移量。

設置$start$start = ($page - 1) * $perpage+1;