Displaying records from my database (not that simple though)

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
john3872
Forum Newbie
Posts: 5
Joined: Sat Mar 04, 2006 4:15 pm

Displaying records from my database (not that simple though)

Post by john3872 »

Hello in this part of my code for the shopping basket part of my website I need to display all of the 3 records in my database, at the moment it only displays the last one.

Please could someone tell me what needs to be done. I have tried a 2D array for the $products array but to no avail. Someone please advise.

Thank you

Code: Select all

require "DatabaseConnect.php";
$query = "SELECT * FROM wines";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
	$products = array();
	$products[$row['WineID']] = array("id"=>$row['WineID'],"name"=>$row['Wine_Name'],"Producer"=>$row['Name_of_Producer'],"price"=>$row['Price_($)']);
}

if($_POST['add'])
{
	$product = $products[$_POST['id']];
	$cart->add_item($product['id'],$_POST['qty'],$product['price'],$product['name']);
}
if($_POST['remove'])
{
	$rid = intval($_POST['id']);
	$cart->del_item($rid);

}

echo "<table>";
echo "<tr>Wine Name</tr>";
foreach($products as $p)
{
	echo "<td><form method='post' action='Wines.php'>";
	echo "<input type='hidden' name='WineID' value='".$p['WineID']."'/>";
	echo "<td>".$p['name']."</td>";
	echo "<td>".$p['Producer']."</td>";
	echo "<td>".' $'.number_format($p['price'],2)."</td>";
	echo "<td><input type='text' name='qty' size='5' value='1'><input type='submit' value='Add to cart' name='add'></td>";
	echo "</form></td></tr>";
}
echo "</table>";
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

since i cant edit your posts,use the php tags

Code: Select all

require "DatabaseConnect.php"; 
$query = "SELECT * FROM wines"; 
$result = mysql_query($query); 
while ($row = mysql_fetch_array($result)) 
{ 
   $products = array(); 
   $products[$row['WineID']] = array("id"=>$row['WineID'],"name"=>$row['Wine_Name'],"Producer"=>$row['Name_of_Producer'],"price"=>$row['Price_($)']); 
} 

if($_POST['add']) 
{ 
   $product = $products[$_POST['id']]; 
   $cart->add_item($product['id'],$_POST['qty'],$product['price'],$product['name']); 
} 
if($_POST['remove']) 
{ 
   $rid = intval($_POST['id']); 
   $cart->del_item($rid); 

} 

echo "<table>"; 
echo "<tr>Wine Name</tr>"; 
foreach($products as $p) 
{ 
   echo "<td><form method='post' action='Wines.php'>"; 
   echo "<input type='hidden' name='WineID' value='".$p['WineID']."'/>"; 
   echo "<td>".$p['name']."</td>"; 
   echo "<td>".$p['Producer']."</td>"; 
   echo "<td>".' $'.number_format($p['price'],2)."</td>"; 
   echo "<td><input type='text' name='qty' size='5' value='1'><input type='submit' value='Add to cart' name='add'></td>"; 
   echo "</form></td></tr>"; 
} 
echo "</table>";
john3872
Forum Newbie
Posts: 5
Joined: Sat Mar 04, 2006 4:15 pm

What do you mean

Post by john3872 »

Sorry Im a bit tied but, this is just part of my code, therefore start and end tags are not shown, did you change anything?

cheers
john
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

nope i did not :( But im jus subbin for the mods who should be checking for these things :lol:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

move

Code: Select all

$products = array()
outside of the loop.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

im sorry all mods if i am taking your job as a mod and tellin people to edit their psots :lol:
john3872
Forum Newbie
Posts: 5
Joined: Sat Mar 04, 2006 4:15 pm

Cheers that works but now i get an error

Post by john3872 »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I changed what you said and it does display all the produts now although I now get an error:

Fatal error: Call to undefined function: wf_get_price() in /home/www/********/wfcart.php on line 35

This is the code for the page it occurs on (Wines.php):

Code: Select all

<?php
session_start();
	if(!isset($_SESSION['Email_Address']))
	{
			header("Location: LoginForm.php");
			exit;
	}
	require "DatabaseConnect.php";
	$sql = "SELECT * FROM wines";
	$result = mysql_query($sql, $connection)
			or die("Couldn't perform query $sql <br />".mysql_error());

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD xhtml 1.0 Transitional//EN"
"http://www.w3.org/TR/shtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Classic Vinters</title>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
<body>

<div id='Border'>

<div id='Logo'></div>

<div id='banner'>
<p>

<a href="Logout.php">Log out</a>
<a href="Wines.php">Wine menu</a>
<a href="CustomerDetails.php">Change your details</a>
<a href="SearchSelection.php">Search for products</a>
<a href="Checkout.php">Checkout</a>

</p>
</div>

<div id='Wines'>

<h2>List Of Wines</h2>

<?php
include "wfcart.php";
session_start();

$cart =& $_SESSION['wfcart'];
if(!is_object($cart)) $cart = new wfCart();

require "DatabaseConnect.php";
$query = "SELECT * FROM wines";
$result = mysql_query($query);
$products = array();
while ($row = mysql_fetch_array($result))
{
   $products[$row['WineID']] = array("id"=>$row['WineID'],"name"=>$row['Wine_Name'],"Producer"=>$row['Name_of_Producer'],"price"=>$row['Price_($)']);
}

if($_POST['add'])
{
   $product = $products[$_POST['id']];
   $cart->add_item($product['id'],$_POST['qty'],$product['price'],$product['name']);
}
if($_POST['remove'])
{
   $rid = intval($_POST['id']);
   $cart->del_item($rid);

}

echo "<table>";
echo "<tr>Wine Name</tr>";
foreach($products as $p)
{
   echo "<td><form method='post' action='Wines.php'>";
   echo "<input type='hidden' name='WineID' value='".$p['WineID']."'/>";
   echo "<td>".$p['name']."</td>";
   echo "<td>".$p['Producer']."</td>";
   echo "<td>".' $'.number_format($p['price'],2)."</td>";
   echo "<td><input type='text' name='qty' size='5' value='1'><input type='submit' value='Add to cart' name='add'></td>";
   echo "</form></td></tr>";
}
echo "</table>";

echo "<h2>Items in your 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 "<form method=post><input type='hidden' name='id' value='".$item['id']."'/><input type='submit' name='remove' value='Remove'/></form>";
	}
	echo "-------------------<br>";
	echo "total: $".number_format($cart->total,2);
} else {
	echo "No items in cart";
}

?>

</div>
</div>
</body>
</html>
This is the script that is linked to it (wfcart.php):

Code: Select all

<?php
class wfCart
{
	var $total = 0;
	var $itemcount = 0;
	var $items = array();
        var $itemprices = array();
	var $itemqtys = array();
        var $iteminfo = array();

	function cart() {} // constructor function

	function get_contents()
	{ // gets cart contents
		$items = array();
		foreach($this->items as $tmp_item)
		{
		        $item = FALSE;

			$item['id'] = $tmp_item;
                        $item['qty'] = $this->itemqtys[$tmp_item];
			$item['price'] = $this->itemprices[$tmp_item];
			$item['info'] = $this->iteminfo[$tmp_item];
			$item['subtotal'] = $item['qty'] * $item['price'];
                        $items[] = $item;
		}
		return $items;
	} // end of get_contents


	function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE)
	{ // adds an item to cart
                if(!$price)
		{
		        $price = wf_get_price($itemid,$qty);
		}

                if(!$info)
		{
                        $info = wf_get_info($itemid);
		}

		if($this->itemqtys[$itemid] > 0)
                { // the item is already in the cart..
		  // so we'll just increase the quantity
			$this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid];
			$this->_update_total();
		} else {
			$this->items[]=$itemid;
			$this->itemqtys[$itemid] = $qty;
			$this->itemprices[$itemid] = $price;
			$this->iteminfo[$itemid] = $info;
		}
		$this->_update_total();
	} // end of add_item


	function edit_item($itemid,$qty)
	{ // changes an items quantity

		if($qty < 1) {
			$this->del_item($itemid);
		} else {
			$this->itemqtys[$itemid] = $qty;
			// uncomment this line if using
			// the wf_get_price function
			// $this->itemprices[$itemid] = wf_get_price($itemid,$qty);
		}
		$this->_update_total();
	} // end of edit_item


	function del_item($itemid)
	{ // removes an item from cart
		$ti = array();
		$this->itemqtys[$itemid] = 0;
		foreach($this->items as $item)
		{
			if($item != $itemid)
			{
				$ti[] = $item;
			}
		}
		$this->items = $ti;
		$this->_update_total();
	} //end of del_item


        function empty_cart()
	{ // empties / resets the cart
                $this->total = 0;
	        $this->itemcount = 0;
	        $this->items = array();
                $this->itemprices = array();
	        $this->itemqtys = array();
                $this->itemdescs = array();
	} // end of empty cart


	function _update_total()
	{ // internal function to update the total in the cart
	        $this->itemcount = 0;
		$this->total = 0;
                if(sizeof($this->items > 0))
		{
                        foreach($this->items as $item) {
                                $this->total = $this->total + ($this->itemprices[$item] * $this->itemqtys[$item]);
				$this->itemcount++;
			}
		}
	} // end of update_total

}
?>
If I cam just get this working I will be very happy.

Thanks anyone


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
john3872
Forum Newbie
Posts: 5
Joined: Sat Mar 04, 2006 4:15 pm

got it wokring

Post by john3872 »

got it wokring no worries
john3872
Forum Newbie
Posts: 5
Joined: Sat Mar 04, 2006 4:15 pm

It is mostly working now but....

Post by john3872 »

When I click add it displays the item, details and price etc, but when i choose a different item it just displays that new item and not the old one aswell. It should display all the different items that have been clicked on.

Please could someone advise as i am getting very close to finishing this.

Thank you
John :D
Post Reply