Hello since cereal helped me out with the price and counting in PHP i need just one more thing, to get the names of the products i have tryed different ways to do it by my self but i just cant cause im begginer with PHP language, i have this array:

$names = array (
    "mikrobranova" => "Mikrobranova pecka",
    "smasina" => "Masina za sadovi",
    "amasina" => "Masina za alista",
    "sol" => "Sol",
    "prasok" => "Prasak za perenje",
    "salama" => "Salama",
    "banani" => "Banani",
    "vegeta" => "Vegeta",
    "zejtin" => "Zejtin",
    "paketce" => "Novogodisno paketce"
);
$_SESSION['prducts_and_names'] = print($names);

So i need when ill press the "BUY BUTTON" to show me the products i have "checked" on the checkboxes.
Thanks :)

Member Avatar for diafol

Show the form with checkboxes.
Does the $names array represent all checkbox values or just the ones checked in the form. I realise it's a static array - just checking that it's not just a hard-coded example array.

Your last line doesn't make sense.

$_SESSION['prducts_and_names'] = $names;

Would be OK, but not sure if that's what you want.

    <font color='#4DFF4D'><form style='position:center' method='get' align='left'>
            <input type='checkbox' name='prasok' value='prasok'/> -> Prasak = 200 <font color='red'>DENARI</font>
        <br>
            <input type='checkbox' name='salama' value='salama'/> -> Salama (500g) = 150 <font color='red'>DENARI</font>
        <br>
            <input type='checkbox' name='banani' value='banani'> -> Banani (1kg) = 80 <font color='red'>DENARI</font>
        <br>
            <input type='checkbox' name='sol' value='sol'/> -> Sol = 60 <font color='red'>DENARI</font>
        <br>
            <input type='checkbox' name='vegeta' value='vegeta'/> -> Vegeta = 100 <font color='red'>DENARI</font>
        <br>
            <input type='checkbox' name='zejtin' value='zejtin'/> -> Zejtin = 70 <font color='red'>DENARI</font>
        <br>
            <input type='checkbox' name='paketce' value='paketce'/> -> Novogodisno paketce = 199 <font color='red'>DENARI</font>
        <br>
            <input type='checkbox' name='amasina' value='amasina'/> -> Masina za perenje alista = 9999 <font color='red'>DENARI</font>
        <br>
            <input type='checkbox' name='smasina' value='smasina'/> -> Masina za perenje sadovi = 4999 <font color='red'>DENARI</font>
        <br>
            <input type='checkbox' name='mikrobranova' value='mikrobranova'/> -> Mikrobranova pecka = 3999 <font color='red'>DENARI</font>
        <br>
            <button type='submit'>Buy Products</button>
            <b>Total: </b>
            <?php
            $prices = array (
                'mikrobranova' => 3999,
                'smasina' => 4999,
                'amasina' => 9999,
                'sol' => 60,
                'prasok' => 200,
                'salama' => 150,
                'banani' => 80,
                'vegeta' => 100,
                'zejtin' => 70,
                'paketce' => 199
            );
                $sum = array();
                $getkeys = array_keys($_GET);
                foreach($prices as $key => $value) {
                    if(in_array($key, $getkeys)) $sum[] = $value;
                }

                if ($_GET['prasok'] == 'prasok') {
                    echo array_sum($sum).' den.';
                } else if($_GET['salama'] == 'salama') {
                    echo array_sum($sum).' den.';
                } else if($_GET['banani'] == 'banani') {
                    echo array_sum($sum).' den.';
                } else if($_GET['sol'] == 'sol') {
                    echo array_sum($sum).' den.';
                } else if($_GET['vegeta'] == 'vegeta') {
                    echo array_sum($sum).' den.';
                } else if($_GET['zejtin'] == 'zejtin') {
                    echo array_sum($sum).' den.';
                } else if($_GET['paketce'] == 'paketce') {
                    echo array_sum($sum).' den.';
                } else if($_GET['amasina'] == 'amasina') {
                    echo array_sum($sum).' den.';
                } else if($_GET['smasina'] == 'smasina') {
                    echo array_sum($sum).' den.';
                } else if($_GET['mikrobranova'] == 'mikrobranova') {
                    echo array_sum($sum).' den.';
                } else {
                    echo '0 den.';
                }
            ?>

            <hr color='gray'>
    </form>

And this too:
`

<?php
    session_start();
    $names = array (
        "mikrobranova" => "Mikrobranova pecka",
        "smasina" => "Masina za sadovi",
        "amasina" => "Masina za alista",
        "sol" => "Sol",
        "prasok" => "Prasak za perenje",
        "salama" => "Salama",
        "banani" => "Banani",
        "vegeta" => "Vegeta",
        "zejtin" => "Zejtin",
        "paketce" => "Novogodisno paketce"
    );


        if(count($sum) == 1) {
            echo '( '.(count($sum)).' proizvod)';
            return;
        }
            if ($_GET['prasok'] == 'prasok') {
                echo '( '.(count($sum)).' proizvodi)';
            } else if($_GET['salama'] == 'salama') {
                echo '( '.(count($sum)).' proizvodi)';
            } else if($_GET['banani'] == 'banani') {
                echo '( '.(count($sum)).' proizvodi)';
            } else if($_GET['sol'] == 'sol') {
                echo '( '.(count($sum)).' proizvodi)';
            } else if($_GET['vegeta'] == 'vegeta') {
                echo '( '.(count($sum)).' proizvodi)';
            } else if($_GET['zejtin'] == 'zejtin') {
                echo '( '.(count($sum)).' proizvodi)';
            } else if($_GET['paketce'] == 'paketce') {
                echo '( '.(count($sum)).' proizvodi)';
            } else if($_GET['amasina'] == 'amasina') {
                echo '( '.(count($sum)).' proizvodi)';
            } else if($_GET['smasina'] == 'smasina') {
                echo '( '.(count($sum)).' proizvodi)';
            } else if($_GET['mikrobranova'] == 'mikrobranova') {
                echo '( '.(count($sum)).' proizvodi)';
            } else {
                echo '( '.(0).' )';
            }
            $_SESSION['total_in_cart'] = array_sum($sum);
            $_SESSION['items_in_cart'] = count($sum);
            $_SESSION['prducts_and_names'] = $names;

    ?>

`

That is all code.

Member Avatar for diafol

I'd hold all the data in one array:

<?php
    $products = array (
                'mikrobranova' => array('price'=>3999, 'label'=>'Mikrobranova pecka', 'names'=>"Mikrobranova pecka"),
                'smasina' => array('price'=>4999, 'label'=>'Masina za perenje sadovi', 'names'=>"Masina za sadovi"),
                'amasina' => array('price'=>9999, 'label'=>'Masina za perenje alista', 'names'=>"Masina za alista"),
                'sol' => array('price'=>60, 'label'=>'Sol', 'names'=>"Sol"),
                'prasok' => array('price'=>200, 'label'=>'Prasak', 'names'=>"Prasak za perenje"),
                'salama' => array('price'=>150, 'label'=>'Salama (500g)', 'names'=>"Salama"),
                'banani' => array('price'=>80, 'label'=>'Banani (1kg)', 'names'=>"Banani"),
                'vegeta' => array('price'=>100, 'label'=>'Vegeta', 'names'=>"Vegeta"),
                'zejtin' => array('price'=>70, 'label'=>'Zejtin', 'names'=>"Zejtin"),
                'paketce' => array('price'=>199, 'label'=>'Novogodisno paketce', 'names'=>"Novogodisno paketce")
            );


    $inputs ='';
    foreach($products as $key=>$product)
    {
        $check = (isset($_GET[$key])) ? ' checked' : '';
        $inputs.= "\n\t<label><input type='checkbox' name='$key' value='$key'$check/> -> {$product['label']} = {$product['price']} <span>DENARI</span></label>";
    }

    if($_GET)
    {
        $sum = 0; $count = 0;$items=array();
        foreach($_GET as $k=>$v)
        {
            if(in_array($k,array_keys($products)))
            {
                $sum += $products[$k]['price'];
                $count++;
                $items[] = "<li>{$products[$k]['names']} @ {$products[$k]['price']} den.</li>";
            }
            $itemsString = implode("\n\t",$items);
        }
    }


?>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
label{
    display:block;  
}
label span{
    color: red; 
}
</style>
</head>
<body>
<form>
<?php echo $inputs;?>
     <button type='submit'>Buy Products</button>
</form>
<?php 
    if(isset($sum))
    {
        echo "<p>Total: $sum den.</p>";
        echo "<p>Num Items: $count</p>";  
        echo "<p>Items Chosen:</p><ul>$itemsString</ul>";
    }
?>



</body>
</html>
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.