Click here to Skip to main content
15,391,528 members
Home / Discussions / Linux, Apache, MySQL, PHP
   

Linux, Apache, MySQL, PHP

 
PinnedForum Guidelines - PLEASE READ PinPopular
Chris Maunder28-Jul-09 2:39
cofounderChris Maunder28-Jul-09 2:39 
Questionimplement ElFinder in PHP language Pin
Alexis Sanchez Vanegas19-May-22 4:28
MemberAlexis Sanchez Vanegas19-May-22 4:28 
QuestionAapanel upload project laravel Pin
marziyeh barooei9-May-22 1:05
Membermarziyeh barooei9-May-22 1:05 
Questionhow do I get total quantity from this code Pin
wixily jnr28-Apr-22 13:29
Memberwixily jnr28-Apr-22 13:29 
PHP
<?php 
session_start();

ini_set('display_errors', 1);
error_reporting(E_ALL);

define("PRODUCTIMAGE",0);
define("PRODUCTCODE", 1);
define("PRODUCTNAME", 2);
define("QUANTITY", 3);
define("PRICE", 4);

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
   if (isset($_POST['productcode']))
   {
      AddToCart();
   }
   else
   {
      $action = isset($_POST['action']) ? $_POST['action'] : '';
      $value = strtoupper(substr($action, 0, 5));
      switch ($value)
      {
      // continue shopping
      case "CONTI":
         header("Location: "."products.html");
         break;

      // recalculate
      case "RECAL": 
         RecalculateCart();
         break;

      // proceed to checkout
      case "CHECK":
         header("Location: "."customer.php");
         break;
      } 
   }
} 


function AddToCart()
{
   $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
   $itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;

$productname = $_POST['productname'];
$extra_price = 0;
$productname = stripslashes($productname);

// Let's see if this product already exists
   for ($i=0; $i < $itemcount; $i++)
   {
     if ($cart[PRODUCTNAME][$i] == $productname) {
         $cart[QUANTITY][$i] = $cart[QUANTITY][$i] + intval($_POST['quantity']);
         $_SESSION['cart'] = $cart;
         $_SESSION['itemcount'] = $itemcount;
         header("Location: "."cart.php");
         exit;
   }
   }

   $cart[PRODUCTIMAGE][$itemcount] = $_POST['productimage'];
   $cart[PRODUCTCODE][$itemcount] = $_POST['productcode'];
   $cart[PRODUCTNAME][$itemcount] = $_POST['productname'];
   $cart[QUANTITY][$itemcount] = intval($_POST['quantity']);
   $cart[PRICE][$itemcount] = $_POST['price'];
   $itemcount = $itemcount + 1;

   $_SESSION['cart'] = $cart;
   $_SESSION['itemcount'] = $itemcount;
} 


function RecalculateCart()
{
   $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
   $itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;

   for ($i=0; $i<$itemcount; $i++)
   {
      $quantity = $_POST['quantity'.($i)];
      if (empty($quantity))
      {
         $quantity = 0;
      }
      else
      if (($quantity < 0) || (!is_numeric($quantity)))
      {
         $quantity = 0;
      } 
      $cart[QUANTITY][$i] = intval($quantity);
   }

   for ($j=0; $j<$itemcount; $j++)
   {
      $quantity = $cart[QUANTITY][$j];

      // remove item from the cart
      if ($quantity == 0)
      {
         $itemcount--;
        
         $curitem = $j;

         while(($curitem+1) < count($cart[0]))         
         {
            for ($k=0; $k<4; $k++)
            {
               $cart[$k][$curitem] = $cart[$k][$curitem+1];
               $cart[$k][$curitem+1] = '';
            }
            $curitem++;
         } 
      } 
   }
   $_SESSION['itemcount'] = $itemcount;
   $_SESSION['cart'] = $cart;
} 

?>


modified 29-Apr-22 3:30am.

AnswerRe: how do I get total quantity from this code Pin
Richard MacCutchan28-Apr-22 21:31
mveRichard MacCutchan28-Apr-22 21:31 
QuestionROLES ET PRIVILEGES UTILISATEURS D'UN SITE Pin
ameb290825-Apr-22 4:06
Memberameb290825-Apr-22 4:06 
QuestionHow can I do a google search and get results in backend using JS or PHP? Pin
Social Bookmarking Site14-Apr-22 15:27
MemberSocial Bookmarking Site14-Apr-22 15:27 
Questionproblem in pagination click Pin
irfankundi7861-Apr-22 23:39
Memberirfankundi7861-Apr-22 23:39 
AnswerRe: problem in pagination click Pin
Richard Deeming3-Apr-22 21:36
mveRichard Deeming3-Apr-22 21:36 
GeneralRe: problem in pagination click Pin
irfankundi7864-Apr-22 4:01
Memberirfankundi7864-Apr-22 4:01 
QuestionGUI for Linux Server Pin
Deepak Vasudevan25-Jan-22 3:29
MemberDeepak Vasudevan25-Jan-22 3:29 
QuestionError in page rendering in php Pin
Rahul Gupta Jan202217-Jan-22 1:16
MemberRahul Gupta Jan202217-Jan-22 1:16 
AnswerRe: Error in page rendering in php Pin
Richard Deeming17-Jan-22 1:53
mveRichard Deeming17-Jan-22 1:53 
Questionhow to add date while importing data in csv from mysql query Pin
him_mca16-Jan-22 20:00
Memberhim_mca16-Jan-22 20:00 
Questioni want make check user offline with mysql Pin
Ping tester1-Jul-21 10:56
MemberPing tester1-Jul-21 10:56 
QuestionHow to print for each primary key all the foreign keys column related to it? Pin
EliotSimpleMan5-May-21 14:40
MemberEliotSimpleMan5-May-21 14:40 
AnswerRe: How to print for each primary key all the foreign keys column related to it? Pin
SeanChupas13-May-21 1:50
MemberSeanChupas13-May-21 1:50 
QuestionHow do I calculate the column in PHP union all function Pin
Developer Account18-Apr-21 23:12
MemberDeveloper Account18-Apr-21 23:12 
AnswerRe: How do I calculate the column in PHP union all function Pin
SeanChupas13-May-21 1:51
MemberSeanChupas13-May-21 1:51 
QuestionPHP :: VSCode always opens a new blank window (untitled-1) Pin
Gustavo Laufer AU4-Apr-21 22:00
MemberGustavo Laufer AU4-Apr-21 22:00 
AnswerRe: PHP :: VSCode always opens a new blank window (untitled-1) Pin
SeanChupas5-Apr-21 3:15
MemberSeanChupas5-Apr-21 3:15 
Questioni have this errors in my project please i need help Pin
frank saki24-Mar-21 11:55
Memberfrank saki24-Mar-21 11:55 
AnswerRe: i have this errors in my project please i need help Pin
Richard Deeming24-Mar-21 22:12
mveRichard Deeming24-Mar-21 22:12 
QuestionHow can I echo the numbers using list() function in foreach loop? Pin
Best Tutor24-Feb-21 9:33
MemberBest Tutor24-Feb-21 9:33 
QuestionRe: How can I echo the numbers using list() function in foreach loop? Pin
Richard MacCutchan24-Feb-21 21:56
mveRichard MacCutchan24-Feb-21 21:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.