Jump to content

Need help doing math equation before DB submission *SOLVED*


digitald

Recommended Posts

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

[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>[/code]

Thanks.

Tom
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

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

Should i be making a

$percent = 7.5
$inital = 5.5

then somehow doing the equation as $ShippingPrice = $ItemPrice % $percent + $inital ?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.