문제

I have a PHP page that retrieves the arrays in my $_SESSION['products'] session. Each array in that session is a product added by the user to their "shopping cart". Currently my session has eleven arrays meaning I have added eleven products to the cart. I am now trying to display the arrays on my view_cart.php page, and paginate them by ten. Basically I would like the page to show the first ten arrays then to display the eleventh array on view_cart.php?Page=2. Right now, the code displays the first ten results on the first page, but on view_cart.php?Page=2 it displays the same ten results all over again.

Here is my full code for the view_cart.php page:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
session_start();
include_once("config.php");

$objConnect = mssql_connect('gdafgm','gdacf','Rdas!');  
$objDB = mssql_select_db('Gdar',$objConnect ); 

$strSQL = "SELECT * FROM products WHERE 1=1 ".$cheack." ORDER BY id ASC"; 

$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");  
$Num_Rows = mssql_num_rows($objQuery);  

$Per_Page = 10;   // Per Page  
$Page = $_GET["Page"];  
if(!$_GET["Page"])  
{  
$Page=1;  
}  

$Prev_Page = $Page-1;  
$Next_Page = $Page+1;  

$Page_Start = (($Per_Page*$Page)-$Per_Page);  
if($Num_Rows<=$Per_Page)  
{  
$Num_Pages =1;  
}  
else if(($Num_Rows % $Per_Page)==0)  
{  
$Num_Pages =($Num_Rows/$Per_Page) ;  
}  
else  
{  
$Num_Pages =($Num_Rows/$Per_Page)+1;  
$Num_Pages = (int)$Num_Pages;  
}  
$Page_End = $Per_Page * $Page;  
IF ($Page_End > $Num_Rows)  
{  
$Page_End = $Num_Rows;  
}  
?>

<?php
    if(isset($_SESSION["products"]))
    {
        $total = 0;
        echo '<form method="post" action="PAYMENT-GATEWAY">';
        echo '<ul>';
        $cart_items = 0;
$i = 0;
foreach ($_SESSION['products'] as $cart_itm)
   {
     if(++$i > 10) break;
            $product_code = $cart_itm["code"];
           $queryy = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code' LIMIT " . $Page_Start . "," . $Per_Page;
           $results = mssql_query($queryy, $mysqli);
           $obj = mssql_fetch_object($results);

            echo '<li class="cart-itm">';
            echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
            echo '<div class="p-price">'.$currency.$obj->price.'</div>';
            echo '<div class="product-info">';
            echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
            echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
            echo '<div>'.$obj->product_desc.'</div>';
            echo '</div>';
            echo '</li>';
            $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
            $total = ($total + $subtotal);

            echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
            echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
            echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->product_desc.'" />';
            echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
            $cart_items ++;

        }

        echo '</ul>';
        echo '<span class="check-out-txt">';
        echo '<strong>Total : '.$currency.$total.'</strong>  ';
        echo '</span>';
        echo '</form>';
        echo '<a href="checkout.php">Checkout</a>';
    }

?>
</body>
</html>

Here is all of the code for my config.php page:

<?php
$mysqli = mssql_connect('gda','Gbc','Rgda');  
$objConnectee = mssql_select_db('Gbdac',$mysqli ); 
?>

Here are the errors I am getting when I run the view_cart.php page:

Warning: mssql_query() [function.mssql-query]: message: Must declare the scalar variable "@start". (severity 15) in D:\Hosting\4502990\html\partscatalogue\view_cart4.php on line 65

Warning: mssql_query() [function.mssql-query]: Query failed in D:\Hosting\4502990\html\partscatalogue\view_cart4.php on line 65

Warning: mssql_fetch_object(): supplied argument is not a valid MS SQL-result resource in D:\Hosting\4502990\html\partscatalogue\view_cart4.php on line 66

Thank you for any help. All help is greatly appreciated.

도움이 되었습니까?

해결책

Wont fix it:

You will have to replace your $queryy assignment with this line:

$query = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code' LIMIT " . $Page_Start .

"," . $Per_Page;

Haven't tested this on your code, can you post the result if its not fixing your Problem? Also think of MySQL's LIMIT _start_,_length_ to be used for the advised purpose of a pagination.

Edit:

Sorry overread SQL Server

Fix for SQL Server:

Replace $query assignment with this:

$query = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code'
     AND product_id BETWEEN ($Page_Start) AND ($Page_Start + $Per_Page)";

다른 팁

You should be able to get a page's worth of results by saying something like

$strSQL = "
    SELECT * FROM products
    WHERE 1=1 {$cheack}
    ORDER BY id
    OFFSET (($Page - 1) * $Per_Page)
    FETCH NEXT $Per_Page ONLY
";

(after making sure the appropriate PHP variables are set, of course).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top