[SOLVED] Someone please help! - Array's

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bobsta63
Forum Commoner
Posts: 28
Joined: Thu Apr 21, 2005 7:03 pm
Location: Ipswich, UK

[SOLVED] Someone please help! - Array's

Post by bobsta63 »

Hi All,

Im having some problems at the moment, Im currently using wfCart, This is free cart software that Im trying to add to my exsisting shopping site I have created. However the cart software currently comes with a static array ($products) (see code below). Could some one please post some code on how I would get it to get the array from my current MySQL database, I have been stuck on this for weeks I would be sooo greatfull if anyone could help, Thanks Bobby

Code: Select all

<?php
// wfCart Demo

// You must included wfcart.php BEFORE you start the session. 
include "wfcart.php";
session_start();      // start the session


$cart =& $_SESSION['wfcart']; // point $cart to session cart.
if(!is_object($cart)) $cart = new wfCart(); // if $cart ( $_SESSION['cart'] ) isn't an object, make a new cart

// end of header stuff

?>
<html><head><title>wfCart Demo</title></head>
<body><h3>wfCart Demo</h3>

<?

// Usually you would get your products from a database but we'll pretend.. 

$products = array();
$products[1] = array("id"=>1,"name"=>"A Bar of Soap","price"=>2.00);
$products[2] = array("id"=>2,"name"=>"Shampoo","price"=>4.80);
$products[3] = array("id"=>3,"name"=>"Pizza","price"=>12.95);


// check to see if any items are being added
if($_POST['add']) {
	$product = $products[$_POST['id']];
	$cart->add_item($product['id'],$_POST['qty'],$product['price'],$product['name']);
}


// spit some forms
// You can have many different types of forms, such as many quantity boxes
// and an "add to cart" button at the bottom which adds all items
// but for the purposes of this demo we will handle one item at a time. 
echo "<table>";
foreach($products as $p) {
	echo "<tr><td><form method='post' action='demo.php'>";
	echo "<input type='hidden' name='id' value='".$p['id']."'/>";
	echo "".$p['name'].' $'.number_format($p['price'],2)." ";
	echo "<input type='text' name='qty' size='5' value='1'><input type='submit' value='Add to cart' name='add'>";
	echo "</form></td></tr>";
}
echo "</table>";


echo "<h2>Items in cart</h2>";

if($cart->itemcount > 0) {
	foreach($cart->get_contents() as $item) {
		echo "<br />Item:<br/>";
		echo "Code/ID :".$item['id']."<br/>";
		echo "Quantity:".$item['qty']."<br/>";
		echo "Price   :$".number_format($item['price'],2)."<br/>";
		echo "Info    :".$item['info']."<br />";
		echo "Subtotal :$".number_format($item['subtotal'],2)."<br />";
	}
	echo "---------------------<br>";
	echo "total: $".number_format($cart->total,2);
} else {
	echo "No items in cart";
}

?>
Or If anyone knows any good free cart scripts that intergrate into your own databse please let me know, Thanks again,

Bobby[/b]
Last edited by bobsta63 on Sat Apr 23, 2005 5:44 pm, edited 1 time in total.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Well you would start by making a db connection.

mysql_connect();

then mysql_select_db();

then mysql_query();

and then use mysql_fetch_row(); to get your products from the db.

There are lots of tutorials on how to do this.

Hope this helps.
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

something like the following:

Code: Select all

//Shouldnt hard code, but im doing it for demo purposes
$connection = mysql_connect("localhost", "root", "") or die("Could Not Connect to Database: ".mysql_error();

$database = mysql_select_db("cart") or die("Could Not Select Database: ".mysql_error();

$query = "SELECT * FROM Products";
$result = mysql_query($query) or die("Could Not Execute Query: ".$query.": ".mysql_error();

while ($row = mysql_fetch_array($result))
{
  //Do everything you want to do here
}
might be a bit dificult to understand. Take a look here for more information the mysql functions and the syntax
bobsta63
Forum Commoner
Posts: 28
Joined: Thu Apr 21, 2005 7:03 pm
Location: Ipswich, UK

Post by bobsta63 »

Does anyone know where I can download a FREE Cart script that intergrates into an existing MySQL database. I would be so greatfull if anyone could help me out, Thanks

Bobby
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

have you looked at osCommerce? There might be an exsisting hack for that sort of thing...
bobsta63
Forum Commoner
Posts: 28
Joined: Thu Apr 21, 2005 7:03 pm
Location: Ipswich, UK

Post by bobsta63 »

Thanks mate, I have just taken a look at osCommerce but it seems to indepth and would have to change so much code, but thanks for your help anyway.

Does anyone else know of any simple cart scripts that intergrates into a MySQL database? Please help
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

bobsta63 wrote:Does anyone know where I can download a FREE Cart script that intergrates into an existing MySQL database. I would be so greatfull if anyone could help me out, Thanks

Bobby
to be honest, reading your above posts and judging your PHP abilities, i would not recommend you run an ecommerce site when you do not know how to properly code such simple scripts such as what you have asked for, security is absolutely imperative in this situation and with you editing code, you could open up the software to vulnerabilities
bobsta63
Forum Commoner
Posts: 28
Joined: Thu Apr 21, 2005 7:03 pm
Location: Ipswich, UK

Post by bobsta63 »

Hidden
Last edited by bobsta63 on Fri Apr 22, 2005 10:47 am, edited 1 time in total.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

tested it, i love the design btw, looks very smart

anyway....i like the cart system somewhat but when you clik on the purchase button i think it should take you back to the category overview (list of products the in previously chosen category and have the "your balance" just update
bobsta63
Forum Commoner
Posts: 28
Joined: Thu Apr 21, 2005 7:03 pm
Location: Ipswich, UK

Post by bobsta63 »

malcolmboston wrote:tested it, i love the design btw, looks very smart

anyway....i like the cart system somewhat but when you clik on the purchase button i think it should take you back to the category overview (list of products the in previously chosen category and have the "your balance" just update
Yer, I agree once they have purchased the item it will take you back to categorie overview, but im just so stuck on the cart bit. Could you help me? please :)
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

paid yes, unpaid no

sorry, i just dont have time anymore for coding for free.

PM me if interested, price depends on complexity (i expect around £10-£30)
Post Reply