Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Sum array PDO

  1. #1
    New to the CF scene
    Join Date
    Jul 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sum array PDO

    hello.. i have a problem whit a function.. don't work to make sum a tow rows.. i don't know what to do...

    Please help me.

    Thanks in advance .

    function pret_total(){

    $total = 0;

    global $handeler;

    $ip = getIp();

    $sel_price = "select * from cart where ip_add = '$ip'";
    $run_price =$handeler->query($sel_price);
    while ($p_price=$run_price->fetch(PDO::FETCH_ASSOC)){


    $pro_id = $p_price['p_id'];



    $pro_price = "SELECT * FROM products WHERE product_id = '$pro_id'";
    $run_pro_price = $handeler->query($pro_price);
    while ($pp_price = $run_pro_price->fetch(PDO::FETCH_ASSOC)) {

    $product_price = array($pp_price['pret_produs']);

    $values = array_sum($product_price);

    $total +=$values;
    }

    echo $total;
    }
    }

  2. #2
    Regular Coder
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    673
    Thanks
    1
    Thanked 103 Times in 98 Posts
    Well... your code really doesn't make any sense... I'm not sure what in blazes you're trying to do here, but I'm pretty sure it's ALL wrong.

    1) if you're gonna use PDO, use it! -- and that means no screwing around with putting variables into the query strings -- that's prepare/execute's job... particularly if you're going to use "while".

    2) If you are only using one result, select just that result not the entire bloody row.

    3) Why are you making a single entry array, and then summing it?!? What possible purpose does that serve?!?

    4) If all you are pulling is a sum, JOIN your results so it only needs ONE query.

    5) $handeler? Is that some funky foreign spelling, or a typo?

    I'm guessing wildly since I don't fully know your table structure, but that should go something more like this:

    Code:
    function pret_total(){
    	$stmt = $handeler->prepare('
    		SELECT SUM(products.pret_produs)
    		FROM products, cart
    		WHERE cart.ip_add = ?
    		AND cart.p_id = products.product_id
    	');
    	$stmt->execute([getIp()]);
    	if ($sum = $stmt->fetch_column()) echo $sum;
    	else echo 'Error';
    } // pret_total
    If all you're doing is grabbing the total of all products.pret_produs that are in the requested IP's cart.

    NOT that I'd be using the IP address to track the user, since multiple users could come from the same IP... that's why you use the session hash or a random hash instead. (since I regenerate the session every pageload, I'd go with a random hash)

    Basically though, stop brute forcing it and let SQL do its job, and stop putting variables into your query strings 2005 style.
    From time to time the accessibility of websites must be refreshed with the blood of designers and owners; it is its natural manure.
    http://www.cutcodedown.com

  3. #3
    New to the CF scene
    Join Date
    Jul 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh.. i understand.. thanks a lot for you advice and time.. i try to learn pdo.. thanks again

  4. #4
    New to the CF scene
    Join Date
    Jul 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it's work..super thank again.. my code was very bad.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •