2 Replies - 972 Views - Last Post: 06 January 2013 - 03:14 PM Rate Topic: -----

#1 hunter123  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 06-January 13

php shopping cart

Posted 06 January 2013 - 08:07 AM

Hi guys,

I am currently in the process of making a shopping cart by following this tutorial - http://v3.thewatchma...om/journal/276/ . I am getting two errors when I follow this tutorial - Fatal error: Call to a member function query() on a non-object in includes/functions.inc.php on line 27 and - Undefined index: action. I have attached my code and if you could provide any help or feedback that would be greatly appreciated.

functions.inc.php:

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

function showCart() { 
    global $db; 
    $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="index.php?p=cart&amp;?action=update" method="post" id="cart">'; 
        $output[] = '<table>'; 
        foreach ($contents as $id=>$qty) { 
            $sql = 'SELECT * FROM products WHERE prod_id = '.$id; 
            $result = $db->query($sql); 
            $row = $result->fetch(); 
            extract($row); 
            $output[] = '<tr>'; 
            $output[] = '<td><a href="index.php?p=cart&amp;?action=delete&id='.$id.'" class="r">Remove</a></td>'; 
            $output[] = '<td>'.$product_name.' - '.$description.'</td>'; 
            $output[] = '<td>&pound;'.$price.'</td>'; 
            $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; 
            $output[] = '<td>&pound;'.($price * $qty).'</td>'; 
            $total += $price * $qty; 
            $output[] = '</tr>'; 
        } 
        $output[] = '</table>'; 
        $output[] = '<p>Grand total: <strong>&pound;'.$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); 
} 
?> 




cart.php:

<?php 
if(!isset($_SESSION['username'])) {
echo "Welcome Guest!"; 
} else {
echo "<font color='red'>You are logged in as </font><font color='purple'>$_SESSION[username].</font><font color='orange'><a href='index.php?p=logout'>  logout?</a> </font><br><font color='red'>Dont forget $_SESSION[username]. always feel free to contact us for support when needed most</font><br><br>";
}
?>
<?php # main.inc.php 

/*  
 *    This is the main content module. 
 *    This page is included by index.php. 
 */ 

// Redirect if this page was accessed directly: 
if (!defined('BASE_URL')) { 

    // Need the BASE_URL, defined in the config file: 
    require_once ('../includes/config.inc.php'); 
     
    // Redirect to the index page: 
    $url = BASE_URL . 'index.php'; 
    header ("Location: $url"); 
    exit; 
     
} // End of defined() IF. 

// Include functions 
require_once('includes/functions.inc.php'); 
// Start the session 
// Process actions 
$cart = $_SESSION['cart']; 
$action = $_GET['action']; 
switch ($action) { 
    case 'add': 
        if ($cart) { 
            $cart .= ','.$_GET['id']; 
        } 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; 
    case 'update': 
    if ($cart) { 
        $newcart = ''; 
        foreach ($_POST as $key=>$value) { 
            if (stristr($key,'qty')) { 
                $id = str_replace('qty','',$key); 
                $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); 
                $newcart = ''; 
                foreach ($items as $item) { 
                    if ($id != $item) { 
                        if ($newcart != '') { 
                            $newcart .= ','.$item; 
                        } else { 
                            $newcart = $item; 
                        } 
                    } 
                } 
                for ($i=1;$i<=$value;$i++) { 
                    if ($newcart != '') { 
                        $newcart .= ','.$id; 
                    } else { 
                        $newcart = $id; 
                    } 
                } 
            } 
        } 
    } 
    $cart = $newcart; 
    break; 
} 
$_SESSION['cart'] = $cart; 
?> 

    <h2>Your Shopping Cart</h2> 
    <?php echo writeShoppingCart(); ?> 
     
    <h2>Please Check Quantities</h2> 
    <p>Please check all the products with their quanitites and total and then please go to the checkout to buy your products</p> 
    <hr /> 
    <div id="cartdisplay"> 
    <?php echo showCart(); ?> 
    </div> 
    <hr /> 
    <p><a href="index.php">Back to Home Page!</a></p>



Is This A Good Question/Topic? 0
  • +

Replies To: php shopping cart

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: php shopping cart

Posted 06 January 2013 - 02:59 PM

Some of the things I don't like about that tutorial:
  • It uses a global. This is almost always bad.
  • The global is never instantiated. This is the problem you're seeing.
  • It mixes server side (PHP) and client side (HTML) code. Yes, it can be done, but it makes things confusing, and beginners don't need more confusion.
  • It does not use prepared statements. This alone should be a deal breaker at this point. Prepared statements should be mandatory.
  • A shopping cart should be an object containing information about the buyer (Name, phone, etc.) and an array of objects (the items selected for purchase). This doesn't seem to be doing that.

Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: php shopping cart

Posted 06 January 2013 - 03:14 PM

By the way, the warnings that show up on the tutorial page should also give you pause!
Deprecated: Function set_magic_quotes_runtime() is deprecated in /nfs/c01/h12/mnt/313/domains/v3.thewatchmakerproject.com/html/textpattern/lib/txplib_db.php on line 14

Warning: Cannot modify header information - headers already sent by (output started at /nfs/c01/h12/mnt/313/domains/v3.thewatchmakerproject.com/html/textpattern/lib/txplib_db.php:14) in /nfs/c01/h12/mnt/313/domains/v3.thewatchmakerproject.com/html/textpattern/lib/txplib_misc.php on line 1240


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1