PDA

View Full Version : PHP - MySQL to create html pages


cocoonfx
11-11-2009, 04:33 PM
Hi Chaps


Is it possible to have a PHP form which you fill in and then this updates a MySQL database (easy part) and then get a script then to echo the table to create a html page? I have tried looking around but I have not come across anything yet. Does anyone know a good tut or can advise on how to do it?

tux
11-11-2009, 07:13 PM
Not sure if this will help you.

The following code is an extract from a shopping cart I have on my site.
I beg borrowed and stole to get this working but basically this is a function that is built by the script and then is displayed by calling the function where you want it in the body of the page.

Heres the code......

<?php
require_once("your connection"); // database connection
///////////////////////////////////////////
include("welcome.php");
include("db.php");
///////////////////////////////////////////
$imageID = $_POST['imageID'];
$productID = $_POST['productID'];
$galleryCatID = $_POST['galleryCatID'];
$qty = $_GET['qty'];
$cartID = $_GET['cartID'];
$page = $_POST['page'];
///////////////////////////////////////////cart code
switch($_GET["action"])
{
case "add_item":
{
AddItem($productID, $imageID, $qty);

break;
}
case "update_item":
{
UpdateItem($cartID, $qty);

break;
}
case "remove_item":
{
RemoveItem($cartID);

break;
}
}
//////////////////////////////////////////////add item function
function AddItem($productID, $imageID, $qty){

$result = mysql_query("SELECT COUNT(*) FROM cart WHERE cookieID = '" . GetCartId() . "' AND productID = $productID AND imageID = $imageID");

$row = mysql_fetch_row($result);
$numRows = $row[0];

if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query

@mysql_query("INSERT INTO cart(cookieID, productID, imageID, qty) VALUES('" . GetCartId() . "', $productID, $imageID, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead

UpdateItem($cartID, $qty);
}
///////////////////////////////////////////////update function
}function UpdateItem($cartID, $qty){

mysql_query("UPDATE cart SET qty = $qty WHERE cookieID = '" . GetCartId() . "' AND cartID = $cartID");
//////////////////////////////////////////////remove item function
}function RemoveItem($cartID){

mysql_query("DELETE FROM cart WHERE cookieID = '" . GetCartId() . "' AND cartID = $cartID");
//////////////////////////////////////////////show cart function
}

function ShowCart(){

session_start();

$userID = $_SESSION['id'];

$query = "SELECT * FROM users WHERE userID = $userID";
$result = @mysql_query($query);
$users = @mysql_fetch_array($result);

$result = @mysql_query("SELECT * FROM cart INNER JOIN products ON cart.productID = products.productID INNER JOIN images ON cart.imageID = images.imageID
WHERE cart.cookieID = '" . GetCartId() . "' ORDER BY cart.cartID ASC");
$cart = @mysql_fetch_array($result);

$qty = $cart['qty'];
$cartID = $cart['cartID'];

?>
<p><?php echo welcome(); ?> <?php echo $users['firstname']; ?>, please check your shopping cart. You can alter the quantity or remove an item if you wish before you
proceed to the Checkout.</p>
<form action="checkout.php" method="post" name="frmCart" id="frmCart">
<input name="imageID" type="hidden" id="imageID" value="<?php echo $cart['imageID']; ?>">
<input name="productID" type="hidden" id="productID" value="<?php echo $cart['productID']; ?>">
<input name="cartID" type="hidden" id="cartID" value="<?php echo $cart['cartID']; ?>">
<input name="qty" type="hidden" id="qty" value="<?php echo $cart['qty']; ?>">
<table cellspacing="0" cellpadding="0">
<thead>
<tr>
<th class="th2">Image</th>
<th class="th3" colspan="2">Details</th>
<th class="th5">Qty</th>
<th class="th5">Price</th>
</tr>
</thead>
<?php do {
// Increment the total cost of all items
$totalCost += ($cart["qty"] * $cart["productPrice"]);


?>

<tbody>
<tr>
<td class="td2">
<img class="cart_thumb" src="Images/Gallery/Thumbs/Small/<?php echo $cart['imageURL']; ?>" alt="<?php echo $cart['title']; ?>" title="<?php echo $cart['title']; ?>" />
</td>
<td class="td3" colspan="2">
PTP<?php echo $cart['imageID']; ?>, <?php echo $cart['title']; ?>, <?php echo $cart['productName']; ?>
</td>
<td class="td5">
<select name="<?php echo $cart['cartID']; ?>" onchange="UpdateQty(this)">
<option><?php echo $cart['qty']; ?></option>
<?php for($i = 1; $i <= 20; $i++){
echo "<option ";
if($cart['qty'] == $i){
echo "SELECTED";
}
echo ">" .$i. "</option>";
}
?>
</select>
<?php $cartID = $cart['cartID']; ?>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?action=remove_item&cartID=$cartID"; ?>"><span class="remove">Remove</span></a></td>
<?php $productPrice = $cart['qty'] * $cart['productPrice'] ?>
<td class="td6"><?php echo $productPrice; ?></td>
</tr>

<?php }while ($cart = mysql_fetch_array($result)) ?>


<tr>
<th class="ppth2"></th>
<th class="ppth3"></th>
<th class="ppth4"></th>
<th class="ppth5">Delivery</th>
<?php //postage
$result = @mysql_query("SELECT deliveryRate FROM delivery");
$rate = @mysql_fetch_array($result);
$deliveryRate = $rate['deliveryRate'];

$delivery = $totalCost * $deliveryRate / 100;
?>
<th class="ppth6"><?php echo number_format($delivery, 2, ".", ",") ?></th>
</tr>
<tr>
<th class="totalth2"></th>
<th class="totalth3"></th>
<th class="totalth4"></th>
<th class="totalth5">Total</th>
<?php
// Total cost of all items plus delivery
$totalCost += ($cart["qty"] * $cart["productPrice"]) + $delivery;
?>
<th class="totalth6"><?php echo number_format($totalCost, 2, ".", ","); ?></th>
</tr>


</table>

<div id="cart_buttons3">
<input name="checkout" type="image" src="Images/Buttons/button_checkout.gif" id="checkout" value="" >
</div><!-- end of #cart_buttons1 -->
</form>
<?php
}
?>

I then call the function with this.....

<?php

if($numrows == 0 ){
echo "<p>Your Shopping Cart is Empty!</p>";
}else ShowCart()

?>

As I said, not sure if this will help you but your welcome to pick the bones out of it.

Good luck.

Regards Paul

cocoonfx
11-11-2009, 09:58 PM
thanks Tux i will have a play....

sajuuu
01-04-2011, 03:08 PM
Not sure if this will help you.

The following code is an extract from a shopping cart I have on my site.
I beg borrowed and stole to get this working but basically this is a function that is built by the script and then is displayed by calling the function where you want it in the body of the page.

Heres the code......

<?php
require_once("your connection"); // database connection
///////////////////////////////////////////
include("welcome.php");
include("db.php");
///////////////////////////////////////////
$imageID = $_POST['imageID'];
$productID = $_POST['productID'];
$galleryCatID = $_POST['galleryCatID'];
$qty = $_GET['qty'];
$cartID = $_GET['cartID'];
$page = $_POST['page'];
///////////////////////////////////////////cart code
switch($_GET["action"])
{
case "add_item":
{
AddItem($productID, $imageID, $qty);

break;
}
case "update_item":
{
UpdateItem($cartID, $qty);

break;
}
case "remove_item":
{
RemoveItem($cartID);

break;
}
}
//////////////////////////////////////////////add item function
function AddItem($productID, $imageID, $qty){

$result = mysql_query("SELECT COUNT(*) FROM cart WHERE cookieID = '" . GetCartId() . "' AND productID = $productID AND imageID = $imageID");

$row = mysql_fetch_row($result);
$numRows = $row[0];

if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query

@mysql_query("INSERT INTO cart(cookieID, productID, imageID, qty) VALUES('" . GetCartId() . "', $productID, $imageID, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead

UpdateItem($cartID, $qty);
}
///////////////////////////////////////////////update function
}function UpdateItem($cartID, $qty){

mysql_query("UPDATE cart SET qty = $qty WHERE cookieID = '" . GetCartId() . "' AND cartID = $cartID");
//////////////////////////////////////////////remove item function
}function RemoveItem($cartID){

mysql_query("DELETE FROM cart WHERE cookieID = '" . GetCartId() . "' AND cartID = $cartID");
//////////////////////////////////////////////show cart function
}

function ShowCart(){

session_start();

$userID = $_SESSION['id'];

$query = "SELECT * FROM users WHERE userID = $userID";
$result = @mysql_query($query);
$users = @mysql_fetch_array($result);

$result = @mysql_query("SELECT * FROM cart INNER JOIN products ON cart.productID = products.productID INNER JOIN images ON cart.imageID = images.imageID
WHERE cart.cookieID = '" . GetCartId() . "' ORDER BY cart.cartID ASC");
$cart = @mysql_fetch_array($result);

$qty = $cart['qty'];
$cartID = $cart['cartID'];

?>
<p><?php echo welcome(); ?> <?php echo $users['firstname']; ?>, please check your shopping cart. You can alter the quantity or remove an item if you wish before you
proceed to the Checkout.</p>
<form action="checkout.php" method="post" name="frmCart" id="frmCart">
<input name="imageID" type="hidden" id="imageID" value="<?php echo $cart['imageID']; ?>">
<input name="productID" type="hidden" id="productID" value="<?php echo $cart['productID']; ?>">
<input name="cartID" type="hidden" id="cartID" value="<?php echo $cart['cartID']; ?>">
<input name="qty" type="hidden" id="qty" value="<?php echo $cart['qty']; ?>">
<table cellspacing="0" cellpadding="0">
<thead>
<tr>
<th class="th2">Image</th>
<th class="th3" colspan="2">Details</th>
<th class="th5">Qty</th>
<th class="th5">Price</th>
</tr>
</thead>
<?php do {
// Increment the total cost of all items
$totalCost += ($cart["qty"] * $cart["productPrice"]);


?>

<tbody>
<tr>
<td class="td2">
<img class="cart_thumb" src="Images/Gallery/Thumbs/Small/<?php echo $cart['imageURL']; ?>" alt="<?php echo $cart['title']; ?>" title="<?php echo $cart['title']; ?>" />
</td>
<td class="td3" colspan="2">
PTP<?php echo $cart['imageID']; ?>, <?php echo $cart['title']; ?>, <?php echo $cart['productName']; ?>
</td>
<td class="td5">
<select name="<?php echo $cart['cartID']; ?>" onchange="UpdateQty(this)">
<option><?php echo $cart['qty']; ?></option>
<?php for($i = 1; $i <= 20; $i++){
echo "<option ";
if($cart['qty'] == $i){
echo "SELECTED";
}
echo ">" .$i. "</option>";
}
?>
</select>
<?php $cartID = $cart['cartID']; ?>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?action=remove_item&cartID=$cartID"; ?>"><span class="remove">Remove</span></a></td>
<?php $productPrice = $cart['qty'] * $cart['productPrice'] ?>
<td class="td6"><?php echo $productPrice; ?></td>
</tr>

<?php }while ($cart = mysql_fetch_array($result)) ?>


<tr>
<th class="ppth2"></th>
<th class="ppth3"></th>
<th class="ppth4"></th>
<th class="ppth5">Delivery</th>
<?php //postage
$result = @mysql_query("SELECT deliveryRate FROM delivery");
$rate = @mysql_fetch_array($result);
$deliveryRate = $rate['deliveryRate'];

$delivery = $totalCost * $deliveryRate / 100;
?>
<th class="ppth6"><?php echo number_format($delivery, 2, ".", ",") ?></th>
</tr>
<tr>
<th class="totalth2"></th>
<th class="totalth3"></th>
<th class="totalth4"></th>
<th class="totalth5">Total</th>
<?php
// Total cost of all items plus delivery
$totalCost += ($cart["qty"] * $cart["productPrice"]) + $delivery;
?>
<th class="totalth6"><?php echo number_format($totalCost, 2, ".", ","); ?></th>
</tr>


</table>

<div id="cart_buttons3">
<input name="checkout" type="image" src="Images/Buttons/button_checkout.gif" id="checkout" value="" >
</div><!-- end of #cart_buttons1 -->
</form>
<?php
}
?>I then call the function with this.....


?>[/php]As I said, not sure if this will help you but your welcome to pick the bones out of it.

Good luck.

Regards Paul


i have a VERY similar cart to this from an online tutorial, i was wondering if you could please tell me WHERE to place this command, i have tried it in many places and still shows the cart and displays the messages is wel...

<?php

if($numrows == 0 ){
echo "<p>Your Shopping Cart is Empty!</p>";
}else ShowCart();



p.s. i no i might be pushing my luck now... BUT were you able to create a mini shopping cart from this, to be displayed on each page...?

thank you in advance