calculation

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
suki2shoes
Forum Newbie
Posts: 4
Joined: Thu Mar 30, 2006 10:57 am

calculation

Post by suki2shoes »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey

This is what i'm trying to do :

I have a table "yacht_basket", that contains a parent field and every product bought will get the parent value of "1" in the table.

I want to SUM the parent field for the last session_id, and then :

if parent = 3 

then take 1% off the total price. ( i have a price variable already called total, stored in the session ) $_SESSION['total'] = $total;}

so i want to take this total and do take 1% off the value, obviously if parent doesnt = 3 then just use the original value. 

I am very new to php, but this is what i have so far, i dont know if i'm on the right lines though.
any advice would be greats, thanks

----------------------------------------------------------------------------------------------------------------------

Code: Select all

<?php

$last_id = mysql_insert_id();
$result=mysql_query("SELECT * FROM yacht_basket where session_id = $session_id");
if (mysql_errno()!=0)
{
echo mysql_error(); This will select the last session id
exit;
}
?> 

<?php 
$res3 = mysql_query("SELECT SUM(parent) from `yacht_basket` WHERE`yacht_basket`.`session_id` = '$session_id'"); 
echo mysql_error(); // SUMS the parent column for products of the session_id
while ($row = mysql_fetch_assoc($res3)) {

if(parent=='3')
$total=(($total/100)*99);} //Calculation

?>

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

That looks like it will work so far.
But what if they buy 4 items? Mabe you need >=3 rather than ==.
suki2shoes
Forum Newbie
Posts: 4
Joined: Thu Mar 30, 2006 10:57 am

Post by suki2shoes »

hey thanks for reply.

i have another calculation if the products = 4( 4 is max they can have, i will do this one after thought)

my code above gets parsed without errors, but the price isnt getting 1 percent taken off.

is there a way i can check if the parent field is getting added up by making it into a variable or something and then seeing what value is getting put into there.

my whole script is below, maybe i have put something in the wrong order, as the Total variable is getting calculated above also.
not sure myself

thanks in advance for helping :)

Code: Select all

[php]
<?php
include("session.php");

$server = "localhost"; // this is the server address and port 
$username = "x"; // change this to your username 
$password = "x"; // change this to your password 
 
$dbconnect = mysql_connect ('localhost', x, x)
or die ("Could not connect");
mysql_select_db('sukivid_pchat1', $dbconnect);
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
<html lang="en">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <title>EuroYacht.com - EuroYacht Homepage - The Home of luxury Yachts</title>
   <link rel="stylesheet" href="style.css" type="text/css">
	<link rel="stylesheet" href="checkout.css" type="text/css">
</head>


<body>

<div id="container">
	
	<div id="container2">
		
		<div id="header">
	<!--	<img src="images/banner.jpg" width="900" hight="100"> </img> -->
		</div>
<hr>

<div id="navbar"> 
		<ul> 
		<li><a href="home.php">Home</a></li> 
		<li><a href="yachts.php">Yachts</a></li> 
		<li><a href="discount.php">Discount</a></li> 
		<li><a href="basket.php">Checkout</a></li> 
		</ul> 
		</div> 
	</div>

<hr>

	<div id="container3">

		<div class ="news1">
				<div class ="section">
				<h3> Checkout </h3>
				</div>
		
				<form action="summary.php" method="post">
				<p>Please Complete the form below. All fields are required.</p>
				<fieldset id="customer-details">
				<legend>Customer Details</legend>

	
				<div>
				<label for="firstname">First Name</label> <input type="text" id="name" name="name" value=""> 
				</div>
					<div>
					<label for="surname">Surname</label> <input type="text" id="surname" name="surname" value="">
					</div>
						<div>
						<label for="address">Address</label> <input type="text" id="address" name="address" value="">
						</div>
				</p>
				</fieldset>



				<fieldset id="Comments">
				<legend>Comments</legend>

					<div>
					<TEXTAREA Name="comments" rows="4" cols="35"></TEXTAREA> 
					</div>
			
				</fieldset>



				<fieldset id="Card-details">
				<legend>Card Details</legend>

					<div>
					<label for="name on card">Name On Card</label> <input type="text" id="cardname" name="cardname" value="">
					</div>
						<div>
						<label for="cardnumber">Card Number</label> <input type="text" id="Cardnumber" name="cardnumber" value="">
						</div>
							<div>
							<label for="securitynumber">Security Number</label> <input type="text" id="securitynumber" name="securitynumber" value="">
							</div>
			

				</fieldset>
		
				<fieldset id="basket">
				<legend>Your Basket</legend>
				<h4 class='basket'>Description Price</h4>
				<?php

				
				$last_id = mysql_insert_id();
				$result=mysql_query("SELECT * FROM yacht_basket where session_id = $session_id");
				if (mysql_errno()!=0)
				{
				echo mysql_error();
				exit;
				}
				?> 
			
				<?php 
				$res2 = mysql_query("SELECT `products`.* FROM `yacht_basket`, `products` WHERE `yacht_basket`.`product_id` = `products`.`product_id` AND `yacht_basket`.`session_id` = '$session_id'"); 
				echo mysql_error(); 
				if($row2=mysql_fetch_assoc($res2)){ 
				$total = (float) 0;         
				do{ 
				?> 
				
				<table>
				
					<td><?php echo $row2['description'];?>  </td>
					<td>£<?php echo $row2['price'];?> </td>
				

				</table> 
					
					<?php 
				$total += $row2['price']; 
				}while($row2=mysql_fetch_assoc($res2)); 
				echo '<br/>Total: &pound;'.$total.''; 
				}else{ 
				echo "You Have No Items In Your Shopping Basket"; 
				} 
				?> 
				
<!-- --------------------------------------------------------------------------------------------------- -->
				
				<?php

				$last_id = mysql_insert_id();
				$result=mysql_query("SELECT * FROM yacht_basket where session_id = $session_id");
				if (mysql_errno()!=0)
				{
				echo mysql_error();
				exit;
				}
				?> 
			
				<?php 
				$res3 = mysql_query("SELECT SUM(parent) from `yacht_basket` WHERE `yacht_basket`.`session_id` = '$session_id'");
				echo mysql_error();
				while ($row = mysql_fetch_assoc($res3)) {
				
				if(parent=='3')
				$total=(($total/100)*99);}

				?>
			
			    <?php echo $total ?>
				
				</fieldset>
		

				<div><input type="submit" value="Submit" id="submit-go" name="submit-go"></div></form>

		
		<?php
		if (isset($total)) { 
		$_SESSION['total'] = $total;}
	
		if (isset($name)) { 
		$_SESSION['name'] = $name;}
		?>
		</div>
	
	</div>

</div>

</div>

</body>

</html>[/php]
Post Reply