Jump to content

Simple Cart System not working too well.


timfox

Recommended Posts

Hello, I'm a beginner php programmer (honestly because I have to)

 

I'm attempting to set up a simple cart system on my site but running into

Warning: Cannot modify header information - headers already sent by (output started at /home/myravep1/public_html/header.php:8) in /home/myravep1/public_html/index.php on line 7

I'm not exactly sure whats going on. I deleted all the whitespace I'm aware of. I gave up looking around for an existing solution. I had to talk to someone real!

 

index.php

<?
include("header.php");

if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
	$pid=$_REQUEST['productid'];
	addtocart($pid,1);
	header("location:checkout.php");
	exit();
}
?>
<form name="form1">
<input type="hidden" name="productid" />
    <input type="hidden" name="command" />
</form>
<div align="center">
<h1 align="center">Products</h1>
<table border="0" cellpadding="2px" width="600px">
	<?
		$result=mysql_query("select * from products");
		while($row=mysql_fetch_array($result)){
	?>
    	<tr>
        	<td><img src="<?=$row['picture']?>" /></td>
            <td>   	<b><?=$row['name']?></b><br />
            		<?=$row['description']?><br />
                    Price:<big style="color:green">
                    	$<?=$row['price']?></big><br /><br />
                    <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
		</td>
	</tr>
        <tr><td colspan="2"><hr size="1" /></td>
        <? } ?>
    </table>
</div>
<?php include("footer.php"); ?>

 

header.php

<? 	include("includes/db.php");
include("includes/functions.php");?>
<!DOCTYPE html>
<html lang="en">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>My Rave Pics</title>
    <script type="text/javascript" src="jquery.js"></script>
<script language="javascript">

function addtocart(pid){
	document.form1.productid.value=pid;
	document.form1.command.value='add';
	document.form1.submit();
}
//cart
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>
<section id="box">
<nav>
<ul>
	<li><a href="index.php">products</a></li>
	<li><a href="checkout.php">checkout</a></li>
</ul>
</nav>
</section>
<section id="content">

 

checkout.php

<?
include("header.php");

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 products not updated!, quantity must be a number between 1 and 999';
		}
	}
}

?>
<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='index.php'" />
    </div>
    	<div style="color:#F00"><?=$msg?></div>
    	<table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
    	<?
		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;
		?>
            		<tr bgcolor="#FFFFFF"><td><?=$i+1?></td><td><?=$pname?></td>
                    <td>$ <?=get_price($pid)?></td>
                    <td><input type="text" name="product<?=$pid?>" value="<?=$q?>" maxlength="3" size="2" /></td>                    
                    <td>$ <?=get_price($pid)*$q?></td>
                    <td><a href="javascript:del(<?=$pid?>)">Remove</a></td></tr>
            <?					
			}
		?>
			<tr><td><b>Order Total: $<?=get_order_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="Place Order" onClick="window.location='billing.php'"></td></tr>
		<?
            }
		else{
			echo "<tr><td>There are no items in your shopping cart!</td>";
		}
	?>
        </table>
    </div>
</form>
<?php include("footer.php"); ?>

 

db.php

<?
@mysql_connect("localhost","admin","pass") or die("Demo is not available, please try again later");
@mysql_select_db("myravep1_store") or die("Demo is not available, please try again later");
session_start();
?>

 

functions.php

<?
function get_product_name($pid){
	$result=mysql_query("select name from products where serial=$pid");
	$row=mysql_fetch_array($result);
	return $row['name'];
}
function get_price($pid){
	$result=mysql_query("select price from products where serial=$pid");
	$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;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/185349-simple-cart-system-not-working-too-well/
Share on other sites

your header.php is outputting html so it will show you that error, you cannot output anything to the browser before using header .

Thanks for the reply!

 

So within my index.php I commented out the header change

//header("location:checkout.php");

 

so is there an alternative? Id like to have the same theme, header, through out the pages and this has been the way I've known to do it.

 

At

<input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />

 

is there a code that forwards me properly to the checkout.php? I'm not too experienced at this so its all a learning process!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.