Webmaster Key - Discussion Forums


Welcome, Guest. Please login or register.
Did you miss your activation email?
February 09, 2012, 09:21:07 PM

Login with username, password and session length
Welceome to Forums!

Important information for guests and new members:

In order to understand the full benefits of becoming an active member of this forum, please review the following information on guest and new member restrictions. These forum changes have been prompted by an overwhelming and unreasonable amount of bot postings and incoherent guest spam messages. We wish to prevent these events from happening in the future and make our community a more comfortable place for all of our members.

For guests:

Guests are not allowed to open new topics, polls, or posts attachments.
If you wish to open up new discussions on this forum, we encourage you to register.

For new members:

New members with less than five posts are not allowed to modify additional profile information such as avatars, contact information, biographies, and signatures. However, new members are encouraged to post their own topics or reply to topics initiated by other members. Become active on the forums and 5 posts should be an easy task!

We are a diverse community with members from all over the world. We encourage new ideas and interesting conversation. Do not be afraid to post webmaster/computer-related questions or problems, as our active members are always willing to help when they are able. Interested? Join us.

+ Webmaster Key Forums
|-+ Webmaster Corner
| |-+ Site Design and Web Authoring
| | |-+ Coding Talk
| | | |-+ PHP sum
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Go Down Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print
Author Topic: PHP sum  (Read 3274 times)
stu
Key Keeper
Member
**
Posts: 83



WWW
« on: August 19, 2009, 04:20:29 PM »

Hi

I am trying to write a short code but can not get it to work. the part I am stuck on is getting it all to add up. Its  simple cart script. and the cart parts fine but i need to find the total price.

Code:

$order_id = $_GET['order_id']; // customers order id

$get_prices_sql = "SELECT price, amount FROM orders WHERE order_id='$order_id";
$get_prices_res = mysqli_query($mysqli, $get_prices_sql) or die(mysqli_error($mysqli));

while ($price_info = mysqli_fetch_array($get_prices_res)) {
$price = $price_info['price'];
$amount = $price_info['price'];

echo results" price and amount for each item":



// i now have the prices and the amount of each products the customer wants in the array above
// lets say the customer has order 3 diffrent items which has created 3 rows in the database

/*
This is the part i cart work out how to do, i now need to add the prices * the amounts = total, then add each rows total togeather to create a cart total
*/

// once cart total has been found i need to add that to VAT * 15%

// once cart total + VAT has been found i need to add that to shipping cost to create total with shipping


{ // close array

I know its a big ask but any help you can give i would be very thankfull. I've spent two days searching the net to try and find something i can work from and I just carnt.


thanks


stuart
Report to moderator   Logged

stu
Key Keeper
Member
**
Posts: 83



WWW
« Reply #1 on: August 20, 2009, 12:57:24 AM »

Hi ive put what working code i have here. the only thing i can not do is get the sub total all other functions are perfect.

Thank you in advance for any help

Code:

<?php
//Include database connection details
require_once('config.php');

//verify the topic exists
$verify_products_sql "SELECT descrition, price, qty FROM orders WHERE ref = '00123'";
$verify_products_res =  mysqli_query($mysqli$verify_products_sql) or die(mysqli_error($mysqli));

if (
mysqli_num_rows($verify_products_res) < 1) {
//this topic does not exist
echo "<p><em>your cart is empty</em></p>";
} else {


?>

 <table width="550" border="0" align="center" cellpadding="5" cellspacing="1" class="infoTable">
        <tr class="infoTableHeader">
            <td colspan="3">Ordered Items</td>
        </tr>
        <tr class="label">
            <td>Item</td>
            <td>Unit Price</td>
            <td>Total</td>
        </tr>


<?php

//get the cart products
while ($product_info mysqli_fetch_array($verify_products_res)) {
$descrition stripslashes($product_info['descrition']);
  $price stripslashes($product_info['price']);
$qty stripslashes($product_info['qty']);
$total_price = ($price $qty);


// this should now get the cart sub total
        
$subTotal 0;
        for (
$i 0$i $total_price$i++) {
   
      $subTotal += $price $qty;
}


?>


        <tr class="content">
            <td class="content"><?php echo $descrition?></td>
            <td align="right"><?php echo $price?></td>
            <td align="right"><?php echo $total_price?></td>
        </tr>

<?php


}

$shipping 5.00;
?>


    <tr class="content">
            <td colspan="2" align="right">Sub-total</td>
            <td align="right"><?php echo $subTotal?></td>
        </tr>
        <tr class="content">
            <td colspan="2" align="right">Shipping</td>
            <td align="right">£<?php echo $shipping?></td>
        </tr>
        <tr class="content">
            <td colspan="2" align="right">Total</td>
            <td align="right">£<?php echo ($subTotal $shipping); ?></td>
        </tr>
    </table>
 
<?php
}
?>

Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #2 on: August 20, 2009, 11:38:31 AM »

You should typecast the price that you get from the database to a number. My guess is that it is being treated as a string or object.

PHP is loosely-typed so it sometimes can't tell what the data is supposed to represent.

So a price of "5.56" may be a string as far as PHP can tell. So you need to do something like:

Code:
<?php
$price 
=  (float) $product_info['price'];
?>


This makes PHP treat the data as a floating point number.

Check out this link for more info: http://jp2.php.net/manual/en/language.types.float.php

Also, you only use stripslashes on string data. User input of a number should be sanitized.

p.s. to pay back, consider linking to my forum: http://webmasterjuice.com  Also I have a PHP section there, if you have any further questions to post regarding PHP/MySQL etc. please post them to my site Smiley
Report to moderator   Logged

stu
Key Keeper
Member
**
Posts: 83



WWW
« Reply #3 on: August 20, 2009, 08:48:29 PM »

Hi

Thanks Andy you are a star. I did work it out and have now got a fully functioning cart I found a script early hours of this morning which i was able to reverse engineer to give me the desired effects I needed. Sometimes you just have to see somthing working to work out how it works.

Didnt know you had a forum. I will have a look at that shortly. I will use it from now on as you are always there to help. Its good to have someone to point you in the right direction. A lot of the time thats all you need.

Plus I will link to your site for your. Thats the least I can do after all the help you give me.

P.S. I thought you had somthing to do with this forum as you are the one that always answers me.


Thanks again
Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #4 on: August 21, 2009, 02:43:56 PM »

Stu, like you I started learning from this forum how to make a website and other webmaster skills many years ago so I stuck around. I felt very grateful to Sensovision, Tamuril, 12Noon and White Wolf and others that helped me out along the way. So I paid back with the extra knowledge that I picked up after my apprenticeship.

Now this forum is in decline, I felt it was a good time to move on and create my own forum to focus on what I and others are interested in such as coding and marketing. Rather than attempting to keep this particular site above water, I decided to let it slowly die a dignified death if you like.

So the new place to hang out is http://webmasterjuice.com if you like my posts. Otherwise stay around here and post your own stuff to keep the place alive.

On my new forum it will become an exciting place to hang out with a new member's club for people that are passionate about working together to get online success.
Report to moderator   Logged

Pages: [1] Go Up Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print 
+ Webmaster Key Forums
|-+ Webmaster Corner
| |-+ Site Design and Web Authoring
| | |-+ Coding Talk
| | | |-+ PHP sum

Jump to:  
« previous next »


Our Partners
RelmaxTOP Ranking System Web Hosting RelmaxTOP Ranking System
Staff Sites
12Noon[12Noon Gallery] Andy[Urgentclick]
Tamuril[Tamuril's Digital Art Exhibit] Sensovision
Powered by MySQL Powered by PHP We are hosted by Relmax Inc. |Our Privacy Policy | Sitemap
Powered by SMF 1.1.9 | SMF © 2006-2009, Simple Machines LLC
Forum design by Tamuril © 2005.
Valid XHTML 1.0! Valid CSS!