Hello, i'm new in PHP so i dont know much about it so ill need a little help with this "eazy" problem. I need to calculate the total price of the items on every "checkbox" check. here is my code:

<?php
            $mikrobranova = 3999;
            $smasina = 4999;
            $amasina = 9999;
            $sol = 60;
            $prasok = 200;
            $salama = 150;
            $banani = 80;
            $vegeta = 100;
            $zejtin = 70;
            $paketce = 199;
            if ($_GET['prasok'] == 'prasok') {
                echo $prasok;
            } else if($_GET['salama'] == 'salama') {
                echo $salama;
            } else if($_GET['banani'] == 'babani') {
                echo $banani;
            } else if($_GET['sol'] == 'sol') {
                echo $sol;
            } else if($_GET['vegeta'] == 'vegeta') {
                echo $vegeta;
            } else if($_GET['zejtin'] == 'zejtin') {
                echo $zejtin;
            } else if($_GET['paketce'] == 'paketce') {
                echo $paketce;
            } else if($_GET['amasina'] == 'amasina') {
                echo $amasina;
            } else if($_GET['smasina'] == 'smasina') {
                echo $smasina;
            } else {
                echo '0';
            }
        ?>

Thank you for the assisteding. :)

Recommended Answers

All 13 Replies

You can create an array of prices, for example:

$prices = array(
    'sol' => 60,
    'prasok' => 200,
    'vegeta' => 100
);

And then, use a loop to check if the keys of this array matches with the $_GET/$_POST array:

$sum = array();
$getkeys = array_keys($_GET);

foreach($prices as $key => $value)
{
    if(in_array($key, $getkeys)) $sum[] = $value;
}

At the end use array_sum to add the values saved in the $sum array:

echo array_sum($sum);

This works also as white list, since only the values in the array of prices are considered, and everything else sent by the form is discarded.

THANK YOU !!! :D

Kindly create an array and as well using loop to check if the keys of the stipulated array matches. I think I agree with Cereal.

Well i actualy need another help with making "Shoping Card" so for every checkbox is checked to go into ( "Kosnica" == "Shoping Card" ) i have this code but doesnt add +1 on every checkbox...

<form method='get'>
    <style>
        p.pos_fixed 
        {
            position:fixed;
            top:70px;
            right:250px;
        }
    </style>
    <a href="kosnica.html"><p class="pos_fixed"><b>Kosnica 
    <?php
        $num = 0;
        if ($_GET['prasok'] == 'prasok') {
                echo '( '.($num++).' )';
            } else if($_GET['salama'] == 'salama') {
                echo '( '.($num++).' )';
            } else if($_GET['banani'] == 'banani') {
                echo '( '.($num++).' )';
            } else if($_GET['sol'] == 'sol') {
                echo '( '.($num += 1).' )';
            } else if($_GET['vegeta'] == 'vegeta') {
                echo '( '.($num += 1).' )';
            } else if($_GET['zejtin'] == 'zejtin') {
                echo '( '.(++$num).' )';
            } else if($_GET['paketce'] == 'paketce') {
                echo '( '.(++$num).' )';
            } else if($_GET['amasina'] == 'amasina') {
                echo '( '.(++$num).' )';
            } else if($_GET['smasina'] == 'smasina') {
                echo '( '.(++$num).' )';
            } else if($_GET['mikrobranova'] == 'mikrobranova') {
                echo '( '.(++$num).' )';
            } else {
                echo '( '.(0).' )';
            }
    ?>
    </b></p></a>

</form>

Or also like you can see i have created local class for "Shoping Card" (kosnica.html) so if is possible to work from there and when you will click that to show the products ...

Thanks again and sorry if im wrong with posting here another different queston on this section. :)

That happens because if the first IF condition is satisfied it will stop executing the other statements. Can you explain what you want to achieve?

For the next time, consider also, to start a new thread, since your new request is different from the original.

Actualy i want to count the products that are selected and to archive into "Kosnica"

If you're using my previous example you can use count($sum), it will return the total of the selected items. Otherwise you need to create a new loop and apply the IF statements there.

Thank you this works, but now if i want to go the products in "kosnica.html" page how can i do that ?
Here is the website i want to do that: http://secretchatsk.host56.com/

I actualy want to ask what i need to do that does need some database or it can be done with out it, cause when ill click "Kosnica" count is beeing reseted ?

Thank you again but i need to show the products on kosnica.html u dont need to write the code just give me some exsample on how to continue cause i dont know how to connect the classes in php and how to make the products show in the other page, sorry if im poor with this questions but i need to do for college work :/

That page (kosnica.html) needs to be processable by the PHP engine, so you can:

  • change the extension to .php
  • otherwise modify Apache to process .html files as php scripts

As suggested you should use tables to save data, otherwise when the browser window is closed the data is lost, at the end of this post there's a link to the PDO library.

To simply send data from a page to another you can use session. In the page in which you calculate the total, start the session and save the data:

<?php

    session_start();

    # other code here
    # . . .


    # set the sessions
    $_SESSION['total_in_cart'] = array_sum($sum);
    $_SESSION['items_in_cart'] = count($sum);

When you go to kosnica.php, again start the session and retrieve what you have saved in the previous page:

<?php

    session_start();

    echo '<p>Items in cart('.$_SESSION['items_in_cart'].')</p>';
    echo '<p>The total is '.$_SESSION['total_in_cart'].'</p>';

This is a simple example, if you check the documentation I linked to you in my previous post you will find few examples about the session.

In addition, you should read:

Good work!

commented: SOLVED +0

Thank you cereal your the best ! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.