Jump to content

[SOLVED] Cannot increment/decrement overloaded objects nor string offsets


Alexhoward

Recommended Posts

Hi Thanks for replying!

 

it's grown into a bit of a monster..

 

but here we go...

 

<? session_start() ?>

<style type="text/css">
<!--
.style1 {font-size: 24px}
.style2 {font-size: 12px}
.style4 {font-size: 12px; font-weight: bold; }
-->
</style>
<p><a href="http://www.everyonlinestore.co.uk/snookereast/index.php"><img src="images/logo.jpg" alt="Snooker East" border="0" longdesc="http://www.everyonlinestore.co.uk/snookereast/index.php" /></a></p>
<p class="style1">Your Shopping Basket </p>

<table width='800' border='1' cellspacing='0' cellpadding='0'>
  <tr>
    <td align='left' bgcolor='#C9E4E4'><span class='style4'>Product</span></td>
    <td align='center' bgcolor='#C9E4E4'><span class='style4'>Quantity</span></td>
    <td align='center' bgcolor='#C9E4E4'><span class='style4'>Amount</span></td>
  </tr>

<?php

include("config.php");

//connect to the mysql server
$link = mysql_connect($host, $db, $pass)
or die ("Could not connect to mysql because ".mysql_error());

//select the database	
mysql_select_db($db)
or die ("Could not select database because ".mysql_error());

 //if(!isset($_GET['id'])) { $_GET['id'] = " "; } 
     $product_id = $_GET['id']; 

 //if(!isset($_GET['action'])) { $_GET['action'] = " "; } 
     $action     = $_GET['action']; 

     
     function productExists($product_id) {
             
             $sql = sprintf("SELECT id FROM products "); 
                 
             return mysql_num_rows(mysql_query($sql)) > 0;
     }

     
     if($product_id && !productExists($product_id)) {
         die("Error. Product Doesn't Exist");
     }

 if($action == "add" ) {
 $_SESSION['a'][$product_id]++;
 }
 else if ($action == "remove" ) {
 $_SESSION['a'][$product_id]--;
 }
 else if($action = "empty" ) {
 unset($_SESSION['mycart']);
 }

 if($_SESSION['a'][$product_id] == 0 ) {
 unset($_SESSION['a'][$product_id]);
 }     

if($_SESSION['a']) {

foreach($_SESSION['a'] as $product_id => $quantity) {

$sql = sprintf("SELECT name, description, price, pandp FROM products WHERE id = %d;", $product_id);

$result = mysql_query($sql);

if(mysql_num_rows($result) > 0) {
                 
    list($name, $description, $price, $pandp) = mysql_fetch_row($result);

$line_cost = $price * $quantity;

if(!isset($total)) { $total = 0; }

$total = $line_cost + $total;

if(!isset($post)) { $post = 0; }

$post = $pandp;	
  
  print " 
  <tr>
    <td width='66%'><span class='style2'>$name</span></td>
    <td width='15%' align='center' valign='middle'><span class='style2'><a href='basket.php?action=remove&id=$product_id'><img src='images/minus.jpg' alt='Remove' width='15' height='15' border='0' valign='middle' /></a>
      <input type='text' value='$quantity' style='width:30' />
      <a href='basket.php?action=add&id=$product_id'><img src='images/plus.jpg' alt='Add' width='15' height='15' border='0' valign='middle' /></a></span></td>
    <td width='17%' align='right'><span class='style2'>£ $line_cost</span></td>
  </tr>";
}
}
}
?>
  <tr>
    <td colspan='3' align='right' bgcolor='#E6E6E6'><span class='style2'>Shipping & Handling Included</span></td>
  </tr>
  <tr>
    <td colspan='3' align='right' bgcolor='#E6E6E6'><span class='style2'><strong>Total : £ <? echo $total ?></strong></span></td>
  </tr>
</table>


    <a href="index.php">Continue Shopping</a>

 

It's falling over on the :

 

if($action == "add" ) {
$_SESSION['a'][$product_id]++;
}
else if ($action == "remove" ) {
$_SESSION['a'][$product_id]--;
}

 

Thanks

Hi Guys,

 

I just unset the session, and it seems to have done the job.

 

think i filled it up with testing...

 

does anyone know a way to avoid this overflow? ? ?

 

I also need some help with error handling, will get back when i iron out the original creases  :D

 

Thanks Again

OK,

 

So i've ironed out what i had before it all went wrong...

 

the code now looks like this:

 

<? session_start() ?>

<style type="text/css">
<!--
.style1 {font-size: 24px}
.style2 {font-size: 12px}
.style4 {font-size: 12px; font-weight: bold; }
-->
</style>
<p><a href="http://www.everyonlinestore.co.uk/snookereast/index.php"><img src="images/logo.jpg" alt="Snooker East" border="0" longdesc="http://www.everyonlinestore.co.uk/snookereast/index.php" /></a></p>
<p class="style1">Your Shopping Basket </p>

<table width='800' border='1' cellspacing='0' cellpadding='0'>
  <tr>
    <td align='left' bgcolor='#C9E4E4'><span class='style4'>Product</span></td>
    <td align='center' bgcolor='#C9E4E4'><span class='style4'>Quantity</span></td>
    <td align='center' bgcolor='#C9E4E4'><span class='style4'>Amount</span></td>
  </tr>

<?php

include("config.php");

//connect to the mysql server
$link = mysql_connect($host, $db, $pass)
or die ("Could not connect to mysql because ".mysql_error());

//select the database	
mysql_select_db($db)
or die ("Could not select database because ".mysql_error());

 if(!isset($_GET['id'])) { $_GET['id'] = " "; } 
     $product_id = $_GET['id']; 

 if(!isset($_GET['action'])) { $_GET['action'] = " "; } 
     $action     = $_GET['action']; 

     
     function productExists($product_id) {
             
             $sql = sprintf("SELECT id FROM products "); 
                 
             return mysql_num_rows(mysql_query($sql)) > 0;
     }

     
     if($product_id && !productExists($product_id)) {
         die("Error. Product Doesn't Exist");
     }

 if($action == "add" ) {
 $_SESSION['a'][$product_id]++;
 }
 else if ($action == "remove" ) {
 $_SESSION['a'][$product_id]--;
 }
 else if($action = "empty" ) {
 unset($_SESSION['a']);
 }

 if(isset($_SESSION['a'])){
 if($_SESSION['a'][$product_id] == 0 ) {
 unset($_SESSION['a'][$product_id]);
 }   
 }  

if(isset($_SESSION['a'])) {

foreach($_SESSION['a'] as $product_id => $quantity) {

$sql = sprintf("SELECT name, description, price, pandp FROM products WHERE id = %d;", $product_id);

$result = mysql_query($sql);

if(mysql_num_rows($result) > 0) {
                 
    list($name, $description, $price, $pandp) = mysql_fetch_row($result);

$line_cost = $price * $quantity;

if(!isset($total)) { $total = 0; }

$total = $line_cost + $total;

if(!isset($post)) { $post = 0; }

$post = $pandp;	
  
  print " 
  <tr>
    <td width='66%'><span class='style2'>$name</span></td>
    <td width='15%' align='center' valign='middle'><span class='style2'><a href='basket.php?action=remove&id=$product_id'><img src='images/minus.jpg' alt='Remove' width='15' height='15' border='0' valign='middle' /></a>
      <input type='text' value='$quantity' style='width:30' />
      <a href='basket.php?action=add&id=$product_id'><img src='images/plus.jpg' alt='Add' width='15' height='15' border='0' valign='middle' /></a></span></td>
    <td width='17%' align='right'><span class='style2'>£ $line_cost</span></td>
  </tr>";
}
}
}
else {

$total = 0.00;

  print " 
  <tr>
    <td width='66%'><span class='style2'>Your Basket is Empty...</span></td>
    <td width='15%' align='center' valign='middle'><span class='style2'>0</span></td>
    <td width='17%' align='right'><span class='style2'>0.00</span></td>
  </tr>";
}

?>
  <tr>
    <td colspan='3' align='right' bgcolor='#E6E6E6'><span class='style2'>Shipping & Handling Included</span></td>
  </tr>
  <tr>
    <td colspan='3' align='right' bgcolor='#E6E6E6'><span class='style2'><strong>Total : £ <? echo $total ?></strong></span></td>
  </tr>
</table>


    <a href="index.php">Continue Shopping</a>

 

The Problem is that when you first add a product to the cart, you get the error:

 

Notice: Undefined index: a  (This being the cart session)

 

Notice: Undefined index: 50  (This being the product id)

 

but then if you add another, it goes away...?

 

so the adding of additional products is fine, no errors, it's just the initial add..

 

any ideas...?

 

thanks again!!

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.