Jump to content

Trying to convert code to use arrays instead of MySQL database for items


n1concepts

Recommended Posts

I've been playing around with this code from http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart

 

Objective: I'm trying to make the scripts work but without pulling the data from MySQL Database.

Instead, I created an array and inserted some info there and trying to replicate the same results but getting confused.

 

For example, on the Index.php file, I made the following changes:

 

<?php

// Start the session
session_start();

// Include functions
require_once('inc/functions.inc.php');

// Products multidimensional Array
$products = array("book" => array(  'id' => '1',
                                    'title' => 'Learn Your ABC\'s',
                                    'author' => 'John Doe',
                                    'price' => '14.95'),
                    "video" => array('id' => '2',
                                    'title' => 'Visual Guide to learn',
                                    'author' => 'Adam Smith',
                                    'price' => '21.38'),
                    "puzzle" => array('id' => '3',
                                    'title' => 'Can You Solve?',
                                    'author' => 'Sara Brown',
                                    'price' => '9.41'),
                    "exam" => array('id' => '4',
                                    'title' => 'Test Your Knowledge',
                                    'author' => 'Kim Carver',
                                    'price' => '11.15'));


?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP Shopping Cart Demo &#0183; Bookshop</title>
<link rel="stylesheet" href="css/styles.css" />
</head>

<body>

<div id="shoppingcart">

<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="booklist">

<h1>Books In Our Store</h1>

<?php
/*
$sql = 'SELECT * FROM books ORDER BY id';
$result = $db->query($sql);
$output[] = '<ul>';
while ($row = $result->fetch()) {
$output[] = '<li>"'.$row['title'].'" by '.$row['author'].': $'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
}
$output[] = '</ul>';
echo join('',$output);
*/
?>

<?php
$output[] = '<ul>';
foreach ($products as $product => $product_item) {
// $flavor is the key and $piece is the value
foreach ($product_item as $item => $piece) {

    if ($item == "id") {                            # Conditional logic defined to map specific keys to specified name value
        $row['id'] = $piece;
//        echo "The id is ". $id."<br />";                            // set just to confirm value correct for variable
    }  elseif ($item == "title")  {                        # Conditional logic defined to map 1st key to price value
        $row['title'] = $piece;
//        echo "The title is ". $title."<br />";                            // set just to confirm value correct for variable
    }  elseif ($item == "author")  {                        # Conditional logic defined to map 1st key to price value
        $row['author'] = $piece;
//        echo "The author is ". $author."<br />";                            // set just to confirm value correct for variable
    }  elseif  ($item == "price")   {                    # Conditional logic defined to map 1st key to shipping value
        $row['price'] = $piece;
//        echo "Price: ". $price."<br /><br />";                        // set just to confirm value correct for variable
    }

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

}
}

?>
</div>

</body>
</html>

 

And here's the original index.php before my edits which show the sql query to the db:

 

<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP Shopping Cart Demo &#0183; Bookshop</title>
<link rel="stylesheet" href="css/styles.css" />
</head>

<body>

<div id="shoppingcart">

<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="booklist">

<h1>Books In Our Store</h1>

<?php
$sql = 'SELECT * FROM books ORDER BY id';
$result = $db->query($sql);
$output[] = '<ul>';
while ($row = $result->fetch()) {
$output[] = '<li>"'.$row['title'].'" by '.$row['author'].': £'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
}
$output[] = '</ul>';
echo join('',$output);
?>

</div>

</body>
</html>

 

Also, I included the zipped file of the entire script setup.

 

Again, I'm trying to figure out how to make the cart work with using an array for the product instead of MySQL database.

My goal is to remove ALL SQL statements from the scripts but stuck with the looping portion to display the title, author, and price from the array as shown above.

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.