5 Replies - 988 Views - Last Post: 22 April 2013 - 10:03 PM Rate Topic: -----

#1 insik  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 31-January 12

count (pid)

Posted 19 April 2013 - 01:18 AM

Hi everyone.
I want to have idea on how to count pid's that are inside the $_SESSION['cart'];
because if I do this count($_SESSION['cart']); only one quantity every productid although there are
more than 1 quantities per productid. And if I code $_SESSION['cart']['qty'] it will also not count

this is my code:include/functions.php

<?php
	function get_product_name($pid){
		$result=mysql_query("select name from products where serial=$pid") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
		$row=mysql_fetch_array($result);
		return $row['name'];
	}
	function get_price($pid){
		$result=mysql_query("select price from products where serial=$pid") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
		$row=mysql_fetch_array($result);
		return $row['price'];
	}
	function remove_product($pid){
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				unset($_SESSION['cart'][$i]);
				break;
			}
		}
		$_SESSION['cart']=array_values($_SESSION['cart']);
	}
	function get_order_total(){
		$max=count($_SESSION['cart']);
		$sum=0;
		for($i=0;$i<$max;$i++){
			$pid=$_SESSION['cart'][$i]['productid'];
			$q=$_SESSION['cart'][$i]['qty'];
			$price=get_price($pid);
			$sum+=$price*$q;
		}
		return $sum;
	}
	function addtocart($pid,$q){
		if($pid<1 or $q<1) return;
		
		if(is_array($_SESSION['cart'])){
			if(product_exists($pid)) return;
			$max=count($_SESSION['cart']);
			$_SESSION['cart'][$max]['productid']=$pid;
			$_SESSION['cart'][$max]['qty']=$q;
		}
		else{
			$_SESSION['cart']=array();
			$_SESSION['cart'][0]['productid']=$pid;
			$_SESSION['cart'][0]['qty']=$q;
		}
	}
	function product_exists($pid){
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		$flag=0;
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				$flag=1;
				break;
			}
		}
		return $flag;
	}

?>



I wanna obtain this because I want to try to compute the shipping by multiplying the
quantities by our shipping rates.

shoppingcart.php
<?php
	include("includes/db.php");
	include("includes/functions.php");
	
	error_reporting(0);
	
	if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
		remove_product($_REQUEST['pid']);
	}
	else if($_REQUEST['command']=='clear'){
		unset($_SESSION['cart']);
	}
	else if($_REQUEST['command']=='update'){
		$max=count($_SESSION['cart']);
		for($i=0;$i<$max;$i++){
			$pid=$_SESSION['cart'][$i]['productid'];
			$q=intval($_REQUEST['product'.$pid]);
			if($q>0 && $q<=999){
				$_SESSION['cart'][$i]['qty']=$q;
			}
			else{
				$msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
			}
		}
	}

?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<script language="javascript">
	function del(pid){
		if(confirm('Do you really mean to delete this item')){
			document.form1.pid.value=pid;
			document.form1.command.value='delete';
			document.form1.submit();
		}
	}
	function clear_cart(){
		if(confirm('This will empty your shopping cart, continue?')){
			document.form1.command.value='clear';
			document.form1.submit();
		}
	}
	function update_cart(){
		document.form1.command.value='update';
		document.form1.submit();
	}


</script>
</head>

<body>
<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
	<div style="margin:0px auto; width:600px;" >
    <div style="padding-bottom:10px">
    	<h1 align="center">Your Shopping Cart</h1>
    <input type="button" value="Continue Shopping" onclick="window.location='products.php'" />
    </div>
    	<div style="color:#F00"><?php echo $msg?></div>
    	<table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
			<?php
			if(is_array($_SESSION['cart'])){
            	echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
				$max=count($_SESSION['cart']);
				for($i=0;$i<$max;$i++){
					$pid=$_SESSION['cart'][$i]['productid'];
					$q=$_SESSION['cart'][$i]['qty'];
					$pname=get_product_name($pid);
					if($q==0) continue;
			
			$price = get_price($pid);
			$subtotal = get_price($pid)*$q;
			
$total = get_order_total();
			?>
            		<tr bgcolor="#FFFFFF"><td><?php echo $i+1?></td><td><?php echo $pname?></td>
                    <td>Php <?php echo number_format($price);?></td>
                    <td><input type="text" name="product<?php echo $pid?>" value="<?php echo $q?>" maxlength="3" size="2" /></td>                    
                    <td>Php <?php echo number_format($subtotal); ?></td>
                    <td><a href="javascript:del(<?php echo $pid?>)">Remove</a></td></tr>
            
			<?php					
				}
			?>
				
				<tr><td><b>Order Total: Php <?php echo number_format($total); ?></b></td><td colspan="5" align="right"><input type="button" value="Clear Cart" onclick="clear_cart()"><input type="button" value="Update Cart" onclick="update_cart()"><input type="button" value="Checkout" onclick="window.location='billing.php'"></td></tr>
			
			<?php
            }
			else{
				echo "<tr bgColor='#FFFFFF'><td>There are no items in your shopping cart!</td>";
			}
		?>
        </table>
    </div>
</form>
</body>
</html>



any ideas will be appreciated.
thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: count (pid)

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,083
  • Joined: 08-August 08

Re: count (pid)

Posted 19 April 2013 - 07:33 AM

Stop using deprecated mysql. Read up on prepared statements. Fix this major problem first!
Was This Post Helpful? 0
  • +
  • -

#3 insik  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 31-January 12

Re: count (pid)

Posted 19 April 2013 - 07:54 PM

How could we say that the mysql is deprecated sir?Does it mean that my coding is not recommended?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,140
  • Joined: 08-June 10

Re: count (pid)

Posted 20 April 2013 - 02:18 AM

View Postinsik, on 20 April 2013 - 04:54 AM, said:

How could we say that the mysql is deprecated sir?

there’s an official deprecation warning on every page of the docs. e.g. http://www.php.net/m...mysql-query.php


View Postinsik, on 20 April 2013 - 04:54 AM, said:

Does it mean that my coding is not recommended?

as far as the DB interaction is concerned, yes.
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,975
  • Joined: 08-June 10

Re: count (pid)

Posted 21 April 2013 - 01:57 PM

CTphpnwb said:

Stop using deprecated mysql. Read up on prepared statements. Fix this major problem first!

I get the whole anti-MySQL API agenda, really, but honestly, this is more confusing than helpful. You make it sound like the MySQL database is deprecated.

To clarify: MySQL, the database server, is not deprecated. Only the old PHP MySQL API functions used to interact with it (like mysql_query) are deprecated. Recommended alternatives are MySQLi and PDO.

View Postinsik, on 19 April 2013 - 08:18 AM, said:

Hi everyone.
I want to have idea on how to count pid's that are inside the $_SESSION['cart'];
because if I do this count($_SESSION['cart']); only one quantity every productid although there are
more than 1 quantities per productid. And if I code $_SESSION['cart']['qty'] it will also not count

Hey.

No standard PHP function will sum up custom things like that. There is really no point in such a function. All you have to do to count the "qty" field inside each of your "cart" arrays is loop through them and sum up the values yourself. For instance:
$total = 0;
foreach ($cartItems as $item) {
    $total += $item["quantity"];
}


Was This Post Helpful? 0
  • +
  • -

#6 insik  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 31-January 12

Re: count (pid)

Posted 22 April 2013 - 10:03 PM

thanks atli.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1