Пример #1
0
function cart()
{
    foreach ($_SESSION as $name => $value) {
        echo $name . ' has quantity of ' . $value . '<br />';
        if ($value > 0) {
            //if (substr($name, 0, 5) =  'cart_' ){
            // 0 is counting point and five letters from 0 and subtract that 5 letters
            //substr( string, start, length ) argument meaning
            //minus four. This means "start 4 characters from the left of the end of the string
            //So you provide the function with a string, then you have to tell PHP which character in the string to start at.
            //The length is how many characters you want to grab. This is optional. If you miss it out, you'll grab all the characters to the end of the string.
            $id = substr($name, 5, strlen($name) - 5);
            // start at five which is_ and strlen($name) is 5 and 5-5 equals 0.
            // we want to get id int
            $get = mysql_query('SELECT id, name, price FROM products WHERE id=' . mysql_real_escape_string((int) $id));
            while ($get_rows = mysql_fetch_assoc($get)) {
                $sub = $get_rows['price'] * $value;
                echo $get_rows['name'] . ' x ' . $value . ' @  &dollar;' . number_format($get_rows['price'], 2) . ' =  &dollar;' . number_format($sub, 2) . ' <a href="cart.php?remove=' . $id . ' ">[-]</a> <a href = "cart.php?add=' . $id . ' ">[+]</a>
	 			       	<a href="cart.php?delete=' . $id . ' "> Delete</a><br /><br /><br />';
            }
        }
        $total += $sub;
    }
    if ($total == 0) {
        echo "Your cart is empty.";
    } else {
        echo '<p>Total: &dollar; ' . number_format($total, 2) . '</p>';
        ?>
<p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="*****@*****.**">
<?php 
        paypal_items();
        ?>
<input type="hidden" name="currency_code" value="AUD">
<input type="hidden" name="amount" value="<?php 
        echo $total;
        ?>
">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but03.gif" 
name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
</p>
<?php 
    }
}
Пример #2
0
function cart()
{
    foreach ($_SESSION as $name => $value) {
        if ($value > 0) {
            if (substr($name, 0, 5) == 'cart_') {
                $id = substr($name, 5, strlen($name) - 5);
                $get = mysql_query('SELECT product_id, product_name, product_price FROM mvc_products WHERE product_id=' . mysql_real_escape_string((int) $id));
                while ($get_row = mysql_fetch_assoc($get)) {
                    $sub = $get_row['product_price'] * $value;
                    echo $get_row['product_name'] . ' x ' . $value . ' @  &euro;' . number_format($get_row['product_price'], 2) . ' = 
                      &euro;' . number_format($sub, 2) . '<a href="admin_carrello.php?remove=' . $id . '"</a> [-] <a href="admin_carrello.php?add=' . $id . '"> [+]</a> <a href="admin_carrello.php?delete=' . $id . '"> [Delete]  </a> <br /> ';
                }
            }
            if (empty($total)) {
                if (empty($sub)) {
                    //non fare niente
                } else {
                    //se il totale è indefinito, questo lo definisce
                    //con almeno un valore
                    $total = $sub;
                }
                //ora che è stato definito
            } else {
                //combina il totale di $sub per ogni riga
                $total += $sub;
            }
        }
    }
    if ($total == 0) {
        echo "Il tuo carrello e' vuoto.";
    } else {
        //Mostro a video il totale del carrello
        echo '<p>Totale : &euro;' . number_format($total, 2) . '</p>';
        ?>

        <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_cart">
        <input type="hidden" name="upload" value="1">
        <input type="hidden" name="business" value="*****@*****.**">
        <?php 
        paypal_items();
        ?>
        <input type="hidden" name="currency_code" value="EUR">
        <input type="hidden" name="amount" value="<?php 
        echo $total;
        ?>
">
        <input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but03.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
        </form>
        <?php 
    }
}