Coding problem,please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cty007
Forum Newbie
Posts: 9
Joined: Thu Dec 14, 2006 11:15 pm

Coding problem,please help

Post by cty007 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

//function.inc.php

<?php
function writeShoppingCart() {
	$cart = $_SESSION['cart'];
	if (!$cart) {
		return '<p>You have no items in your shopping basket</p>';
	} else {
		// Parse the cart session variable
		$items = explode(',',$cart);
		
		return '<p>You have <a href="cart.php">'.count($items).' item in your shopping cart</a></p>';
	}
}

function showCart() {
	
	$db=new mysqli('localhost','root','');

	if(mysqli_connect_errno())
	{
	die('Connect failed:'.mysqli_connect_error());
	}

	$db->select_db('test')
	or die('select_db failed:'.$db->error);

	$cart = $_SESSION['cart'];
	if ($cart) {
		$items = explode(',',$cart);
		$contents = array();
		foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}
		$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
		$output[] = '<table>';
		foreach ($contents as $id=>$qty) {
			
			$query='SELECT * FROM books WHERE id = '.$id;
			$result=$db->query($query)
			or die('query failed'.$db->error);

			$row = $result->fetch_assoc();
			extract($row);
			$output[] = '<tr>';
			
			$output[] = '<td>'.$title.' by '.$author.'</td>';
			$output[] = '<td>RM'.$price.'</td>';
			
			$output[] = '<td><a href="cart.php?action=delete&id='.$id.'">Remove</a></td>';
			$total += $price;
			$output[] = '</tr>';
		}
		$output[] = '</table>';
		$output[] = '<p>Grand total: <strong> RM'.$total.'</strong></p>';
		//$output[] = '<div><button type="submit">Update cart</button></div>';
		$output[] = '</form>';
	} else {
		$output[] = '<p>You shopping cart is empty.</p>';
	}
	return join('',$output);
}
?>

Code: Select all

//cart.php

<?php

// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
	case 'add':
		if ($cart){
	
			if($cart!=$_GET['id']){
			
		
				$cart .= ','.$_GET['id'];
				}		
			
		
			else{ 
			
			echo "you already order the book!";
		}
	
		}
		
		else{
		
			$cart = $_GET['id'];
			
		}
		break;
	case 'delete':
		if ($cart) {
			$items = explode(',',$cart);
					
			$newcart = '';
			foreach ($items as $item) {
				if ($_GET['id'] != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			$cart = $newcart;
		}
		break;
}
$_SESSION['cart'] = $cart;
?>
	
<body>


<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>


<h1>You have order the following book</h1>

<?php
echo showCart();
?>

<p><a href="index.php">Order More Book</a></p>

</body>
</html>

Code: Select all

//index.php

<?php

// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
?>

<body>

<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>

<h1>Books In Our Store</h1>

<?php
$db=new mysqli('localhost','root','');

if(mysqli_connect_errno())
{
die('Connect failed:'.mysqli_connect_error());
}

$db->select_db('test')
or die('select_db failed:'.$db->error);

$query='SELECT * FROM books ORDER BY id';;


$result=$db->query($query)
or die('query failed'.$db->error);


$output[] = '<ol>';
while ($row = $result->fetch_assoc()) {
	$output[] = '<li>"'.$row['title'].'" by '.$row['author'].': RM'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
}
$output[] = '</ol>';
echo join('',$output);
?>



</body>
</html>

Code: Select all

CREATE TABLE books(
id int auto_increament,
title varchar
author varchar
price decimal
PRIMARY KEY(id)
)
examples:

Code: Select all

INSERT INTO books VALUES(1,'ABC','kelly','25.00');
INSERT INTO books VALUES(2,'XYZ','john','80.00');
my Question:

I am using MySQL+PHP5+IIS

(*NOTE:i just let the user to buy each book for ONE unit only.)

step 1(correct):

when i click "Add to cart"(index.php) for the book "ABC" ,
a message was shown in (cart.php):"You have 1 item in your shopping basket"
when i click the "Add to cart"(index.php) for the book "ABC" AGAIN,
a message shown including:"You have 1 item in your shopping basket" and
"you already order the book!"



step 2(have bugs/error occur):

when i click "when i click "Add to cart"(index.php) for the book "XYZ" ,
a message was shown in (cart.php):"You have 2 item in your shopping basket"
when i click the "Add to cart"(index.php) for the book "ABC" AGAIN,
a message shown including:"You have 3 item in your shopping basket"
(*the item number will continue increase)

what i want is:i hope i can get the message "You have 2 item in your shopping basket"
Also,will shown "you already order the book!" when i try to add the same item again!


The coding part that i suspect make the error occur as below:

Code: Select all

case 'add':
		if ($cart){
	
			if($cart!=$_GET['id']){
			$cart .= ','.$_GET['id'];
				}		
			else{ 
			echo "you already order the book!";
			}
			}
		
		else{
		
			$cart = $_GET['id'];
			
		}
--->can anyone help me to edit it?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

From what I've seen of your code, it looks like $cart is a comma delimited list

Use strstr to test for the id in $cart

Code: Select all

if (strstr ($cart, $_GET['id'])) {

// code

}
cty007
Forum Newbie
Posts: 9
Joined: Thu Dec 14, 2006 11:15 pm

Post by cty007 »

Code: Select all

session_start();
// Process actions
$cart = $_SESSION['cart']; 
$action = $_GET['action']; 
switch ($action) { 
        case 'add': 
                if ($cart){ 
        
                        if (strstr ($cart, $_GET['id'])) { 
                        
                
                                $cart .= ','.$_GET['id']; 
                                }               
                        
                
                        else{ 
                        
                        echo "you already order the book!"; 
                } 
        
                } 
                
                else{ 
                
                        $cart = $_GET['id']; 
                        
                } 
                break;

i try to edit it,but have error shown:
"query failedUnknown column 'Array' in 'where clause'
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

In that last snippet, there's no database query, so where exactly is the error?
cty007
Forum Newbie
Posts: 9
Joined: Thu Dec 14, 2006 11:15 pm

anyone able to help me to edit the coding?

Post by cty007 »

Hoepfully there are people who able to edit my coding to correct one.Thanks
Post Reply