Jump to content


Photo

Need help doing math equation before DB submission *SOLVED*


  • Please log in to reply
7 replies to this topic

#1 digitald

digitald
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 September 2006 - 08:47 AM

Hi people,

This is my first post and I hate it to be one that I am asking help with but I really am stuck with such an easy task.

Im trying to add 7.5% of the productprice and 5.5 to work out the shippingprice.
Ive tried so hard to get this working but it just wont happen.

Could someone please help.
below is the code

if(isset($_POST[s1]))
{
	$MyProductName = htmlentities(strip_tags($_POST[ProductName]));
	$MyProductName2 = stripslashes($MyProductName);
	$NewCategoryInfo = explode("|", $_POST[CategoryInfo]);
	$NewDesc = htmlentities(strip_tags($_POST[ProductDesc]));
	$NewDesc2 = stripslashes($NewDesc);
	$ImageName = $_FILES[ProductImage][name];
	$NewShipping = strip_tags($_POST[ShippingPrice]);
	$NewPrice = strip_tags($_POST[ProductPrice]);

	if(empty($MyProductName))
	{
		$add_error = "<center><font color=red size=2><b>Enter the product name, please!</b></font></center>";
	}
	elseif(empty($_POST[CategoryInfo]))
	{
		$add_error = "<center><font color=red size=2><b>Select the product category, please!</b></font></center>";
	}
	elseif(empty($NewPrice))
	{
		$add_error = "<center><font color=red size=2><b>Enter the product price, please!</b></font></center>";
	}
	else
	{
		$t = time();

		//upload the image
		if(!empty($ImageName))
		{
			$NewImageName = "$t$ImageName";
			copy($_FILES[ProductImage][tmp_name], "../items_images/$NewImageName");
		}

		//update the database
		$q1 = "insert into devbg_products set 
						ItemName = '$MyProductName',
						ItemCategory = '$NewCategoryInfo[0]',
						ItemSubcategory = '$NewCategoryInfo[1]',
						ItemDescription = '$NewDesc',
						ItemPrice = '$NewPrice',
						ItemShipping = '$NewShipping',
						ItemImage = '$NewImageName',
						ProductStatus = 'unapproved',
						AddDate = '$t' ";
		mysql_query($q1) or die(mysql_error());

		//get the ItemID
		$qn = mysql_query("select last_insert_id()") or die(mysql_error());
		$an = mysql_fetch_array($qn);
		$LastID = $an[0];

		//get the category name
		$q1 = "select CategoryName from devbg_categories where CategoryID = '$NewCategoryInfo[0]' ";
		$r1 = mysql_query($q1) or die(mysql_error());
		$a1 = mysql_fetch_array($r1);

		$DisplayCategory = $a1[0];

		if(!	empty($NewCategoryInfo[1]))
		{
			//get the subcategory name
			$q2 = "select SubcategoryName from devbg_subcategories where SubcategoryID = '$NewCategoryInfo[1]'  ";
			$r2 = mysql_query($q2) or die(mysql_error());
			$a2 = mysql_fetch_array($r2);

			$DisplayCategory .= " / $a2[0]";
		}

		?>

		<form method=post action=AddProduct.php>
		<table align=center width=440>
		<tr>
					<td valign=top width=120>
					<?
					if(!empty($NewImageName))
					{
						echo "<img src=\"../items_images/$NewImageName\" width=90>";
					}
					else
					{
						echo "&nbsp;";
					}
					?>
					</td>
	
					<td width=320 valign=top>
						<font color=black face=verdana size=2><b><?=$MyProductName2?></b></font>
							<br>
						<font color=black face=verdana size=1><b>Category: <?=$DisplayCategory?></b></font>
							<br>
						<font color=red face=verdana size=2><b>Price: $<?=number_format($NewPrice, 2, ".", "");?></b></font>
					</td>					
			</tr>

			<tr>	
				<td colspan=2>
					<?=nl2br($NewDesc2);?>
				</td>
			</tr>	

			<input type=hidden name="Image" value="<?=$NewImageName?>">
			<input type=hidden name="ItemID" value="<?=$LastID?>">

			<tr>
				<td  colspan=2 align=center><br><br>

					<table align=center width=100%>
					<tr>
						<td align=center valign=top><input type=submit name=s2 value="Accept"></td>
						<td align=center valign=top><input type=submit name=s2 value="Delete"></form></td>
						<td align=center valign=top>	
							<form method=post action=EditProduct.php>
								<input type=hidden name="ItemID" value="<?=$LastID?>">
								<input type=submit name=s10 value="Edit">
							</form>
						</td>
					</tr>
					</table>
				</td>
			</tr>
		</table>

			<input type=hidden name="Image" value="<?=$NewImageName?>">
			<input type=hidden name="ItemID" value="<?=$LastID?>">

		</form>

		<?

		exit();
	}
}
elseif(isset($_POST[s2]))
{
	if($_POST[s2] == "Accept")
	{
		$q1 = "update devbg_products set ProductStatus = 'approved' where ItemID = '$_POST[ItemID]'  ";
		mysql_query($q1) or die(mysql_error());

		echo "<br><br><center>The new product was added successfully.</center>";
	}
	if($_POST[s2] == "Delete" )
	{
		mysql_query("delete from devbg_products where ItemID = '$_POST[ItemID]' ") or die(mysql_error());

		if(!empty($_POST[NewImageName]))
		{
			unlink("items_images/$_POST[NewImageName]");
		}
	}
}


//get the categories list
$q1 = "select * from devbg_categories order by CategoryName ";
$r1 = mysql_query($q1);

if(!$r1)
{
	header("location:../error1.php");
}
else
{
	if(mysql_num_rows($r1) == '0')
	{
		echo "<br><br><center>You need to add some categories, before adding products.</center>";
		exit();
	}
	else
	{
		$MySelect = "<select name=CategoryInfo>\n\t";
		$MySelect .= "<option value=\"\"></option>\n\t";

		while($a1 = mysql_fetch_array($r1))
		{
			$MySelect .= "<option value=\"$a1[CategoryID]\">$a1[CategoryName]</option>\n\t";
			//get the subcategories
			$q2 = "select * from devbg_subcategories where CategoryID = '$a1[CategoryID]'  ";
			$r2 = mysql_query($q2) or die(mysql_error());
			while($a2 = mysql_fetch_array($r2))
			{
				$MySelect .= "<option value=\"$a1[CategoryID]|$a2[SubcategoryID]\">$a1[CategoryName] - $a2[SubcategoryName]</option>\n\t";
			}

		}

		$MySelect .= "</select>";
	}
}

?>


<form method=post action=AddProduct.php enctype="multipart/form-data">
<table align=center width=450>
<caption align=center><b>Add a new product</b><br><?=$add_error?></caption>

<tr>
	<td>Product Name:</td>
	<td><input type=text name=ProductName value="<?=$_POST[ProductName]?>"></td>
</tr>

<tr>
	<td>Category:</td>
	<td>
		<?=$MySelect?>
	</td>
</tr>

<tr>
	<td valign=top>Description:</td>
	<td><textarea rows=6 cols=30 name=ProductDesc><?=$_POST[ProductDesc]?></textarea></td>
</tr>

<tr>
	<td>Image:</td>
	<td><input type=file name=ProductImage></td>
</tr>

<tr>
	<td>Shipping Price:</td>
	<td><input type=text name=ShippingPrice size=5 value="<?=$_POST[ShippingPrice]?>"></td>
</tr>

<tr>
	<td>Product Price:</td>
	<td><input type=text name=ProductPrice size=5 value="<?=$_POST[ProductPrice]?>"></td>
</tr>

<tr>
	<td colspan=2>
		<input type=submit name=s1 value="Preview">
	</td>
</tr>

</form>

Thanks.

Tom

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 12 September 2006 - 09:00 AM

could you post just the bit that show's the calculation as im at work and scanned thru but can't see where you even have the value 7.5 to be working out the percentage..


i take it you want to add 7.5% of the totaly onto the price and then add £5.50?

$value=156;
$percentage=($value/100*7.5);
$newvalue=$value+$percentage+5.5;


That should work as far as i know
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 digitald

digitald
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 September 2006 - 09:12 AM

Thanks for your reply.

as you can tell im a newcomer to php.

what i am trying to do is get 7.5% of the price of a product and add 5.50 to it to get a total for the shipping price instead of just entering a shipping price everytime i add a product.

i have taken out all the editing i did for it as it would not work at all.

i was trying to edit it in the code below

<tr>
	<td>Shipping Price:</td>
	<td><input type=text name=ShippingPrice size=5 value="<?=$_POST[ShippingPrice]?>"></td>
</tr>

Should i be making a

$percent = 7.5
$inital = 5.5

then somehow doing the equation as $ShippingPrice = $ItemPrice % $percent + $inital ?


#4 radalin

radalin
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 12 September 2006 - 09:16 AM

% sign means modulus and not percent.

when you try to do 5 % 2 you will get 1 as result. Because when make 5/2 operation it's left 1 . (5 - 2 = 3, 3-2 = 1 , 1 < 2 , result 1)
Roy Simkes
Yet Another Parkyeri Developer

#5 digitald

digitald
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 September 2006 - 09:33 AM

Thanks for the heads up Radalin, however I am still very stuck on this problem.

Im not sure what i need to add in order to make this work

i need to modify the data before it is put in the database.

any takers out there?

#6 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 12 September 2006 - 09:36 AM

try  it by .075 and it should work

#7 digitald

digitald
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 September 2006 - 09:51 AM

Thanks everyone for you help.

Ive got the problem solved.

here is what I needed to do, I knew it would be something silly and easy.

$NewShipping = strip_tags($_POST[ProductPrice]) / '100' * '7.5' + '5.5';


#8 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 September 2006 - 10:22 AM

<?php

$shipping = ($price * 0.075) + 5.5;

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users