www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP Site all of sudden stopped working properly

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    3

    PHP Site all of sudden stopped working properly

    My site www.woodwerxofyorkshire.co.uk has been working correctly for the last 12 months but about a week ago, I started receiving error messages and certain functions no longer work correctly. One of these is the shopping basket. Items are placed in the basket correctly but when the quantity is changed within the basket, it no longer changes the quantity. This has only just started happening over the last week and having revisited the code, I cannot see why this would be. I have contacted my hosting provider (Ipage) and they have insisted that they haven't changed anything, however neither have I. I was wondering whether somebody could review my code (second pair of eyes) to see if I'm missing something in relation to the change of quantity. I have pasted the code below and any help would be massively appreciated.

    <?php
    session_start(); // Start session first thing in script
    // Script Error Reporting
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    // Connect to the MySQL database
    include "Storescripts/connect_to_mysql.php";
    ?>
    <?php
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Section 1 (if user attempts to add something to the cart from the product page)
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
    $wasFound = false;
    $i = 0;
    // If the cart session variable is not set or cart array is empty
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    // RUN IF THE CART IS EMPTY OR NOT SET
    $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
    } else {
    // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
    foreach ($_SESSION["cart_array"] as $each_item) {
    $i++;
    while (list($key, $value) = each($each_item)) {
    if ($key == "item_id" && $value == $pid) {
    // That item is in cart already so let's adjust its quantity using array_splice()
    array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
    $wasFound = true;
    } // close if condition
    } // close while loop
    } // close foreach loop
    if ($wasFound == false) {
    array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
    }
    }
    header("location: shopping_basket.php");
    exit();
    }
    ?>
    <?php
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Section 2 (if user chooses to empty their shopping cart)
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
    unset($_SESSION["cart_array"]);
    }
    ?>
    <?php
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Section 3 (if user chooses to adjust item quantity)
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
    // execute some code
    $item_to_adjust = $_POST['item_to_adjust'];
    $quantity = $_POST['quantity'];
    $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
    if ($quantity >= 100) { $quantity = 99; }
    if ($quantity < 1) { $quantity = 1; }
    if ($quantity == "") { $quantity = 1; }
    $i = 0;
    foreach ($_SESSION["cart_array"] as $each_item) {
    $i++;
    while (list($key, $value) = each($each_item)) {
    if ($key == "item_id" && $value == $item_to_adjust) {
    // That item is in cart already so let's adjust its quantity using array_splice()
    array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
    } // close if condition
    } // close while loop
    } // close foreach loop
    }
    ?>
    <?php
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Section 4 (if user wants to remove an item from cart)
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
    // Access the array and run code to remove that array index
    $key_to_remove = $_POST['index_to_remove'];
    if (count($_SESSION["cart_array"]) <= 1) {
    unset($_SESSION["cart_array"]);
    } else {
    unset($_SESSION["cart_array"]["$key_to_remove"]);
    sort($_SESSION["cart_array"]);
    }
    }
    ?>

  2. #2
    Join Date
    Jun 2010
    Posts
    16
    Hey Leroy,

    I looked at your code, and making a few assumptions I was able to duplicate the problem and arrive at a solution. The problem lies in section 3.

    Try changing your code accordingly:

    PHP Code:
    $i 0;
    foreach (
    $_SESSION["cart_array"] as $each_item) { 
       while (list(
    $key$value) = each($each_item)) {
          if (
    $key == "item_id" && $value == $item_to_adjust) {
             
    // That item is in cart already so let's adjust its quantity using array_splice()
             
    array_splice($_SESSION["cart_array"], $i1, array(array("item_id" => $item_to_adjust"quantity" => $quantity)));
          } 
    // close if condition
       
    // close while loop
       
    $i++;
    // close foreach loop 
    Note that I made two changes.
    (1) I moved $i++ to the bottom of the foreach
    (2) I changed $i-1 to $i in the arrayslice() function call.

    I hope that helps.

    Regards..
    SCJP 2, SCWCD 5, ZCE 5.3

  3. #3
    Join Date
    Jun 2011
    Posts
    3
    Hi Dukes,

    Thanks for the help.

    I've tried the amendments you suggested but I am still incurring the same error. Items can be placed in the basket but when changing the quantity, the page refreshes but the quantity doesn't actually change.

    Frustrating to say the least because it was working perfectly until last week. Whether I was getting lucky as a newbie to php I'm not sure. Don't suppose you have any more suggestions? I really appreciate the help.

    Regards

    Leroy

  4. #4
    Join Date
    Jun 2010
    Posts
    16
    Quote Originally Posted by Leroy06 View Post
    Hi Dukes,

    Thanks for the help.

    I've tried the amendments you suggested but I am still incurring the same error. Items can be placed in the basket but when changing the quantity, the page refreshes but the quantity doesn't actually change.

    Frustrating to say the least because it was working perfectly until last week. Whether I was getting lucky as a newbie to php I'm not sure. Don't suppose you have any more suggestions? I really appreciate the help.

    Regards

    Leroy
    Hi Leroy,

    Here is the test code I used. Maybe it will help.

    PHP Code:
    <?php 
       $pid 
    "12345";
       
    $item_id $pid;
       
    $cart_array = array(=> array("item_id" => $pid"quantity" => 1));
       
    $cart_array[] = array("item_name_1" => "Two Blocks of Iroko No.2""quantity_1" => 3);
       
    $cart_array[] = array("item_name_2" => "Solid Light Oak Tall Mantle Clock""quantity_2" => 1);

       echo 
    "before\n";
       
    print_r($cart_array);
       echo 
    "\n";
       
       
    //Section 3 (if user chooses to adjust item quantity)
        //execute some code
        
    $item_to_adjust "Two Blocks of Iroko No.2";
        
    $quantity "1";
        
    $quantity preg_replace('#[^0-9]#i'''$quantity); // filter everything but numbers
        
    if ($quantity >= 100) { 
          
    $quantity 99
        }
        if (
    $quantity 1) { 
           
    $quantity 1
        }
        if (
    $quantity == "") { 
           
    $quantity 1
        }
        
    $i 0;
        foreach (
    $cart_array as $each_item) { 
          
    # print_r($each_item);
          
    while (list($key$value) = each($each_item)) {
             
    # echo "*** [$key - $value] ***\n";
             
    $item_id "item_name_".$i;
             
    $quantity_id "quantity_".$i;

             
    # change to true for debugging
             
    if (false) {
                echo 
    "checking key [$key] [$item_id]\n";
                if (
    $key == $item_id) {
                   echo 
    "$key == $item_id\n";
                }
                                    
                echo 
    "checking value [$value] [$item_to_adjust]\n";
                if (
    $value == $item_to_adjust) {
                   echo 
    "$value == $item_to_adjust\n";
                }
             }
                                    
               if (
    $key == $item_id && $value == $item_to_adjust) {
                
    # echo "found\n";  
                   //That item is in cart already so let's adjust its quantity using array_splice()
                   
    array_splice($cart_array$i1, array(array($item_id => $item_to_adjust$quantity_id => $quantity)));
               } 
    // close if condition
               # change to true for debugging
             
    else if (false) {
                echo 
    "not found\n";
                echo 
    ">>> [$item_id - $value] <<<\n";
             }
           } 
    // close while loop
          
    $i++;
       } 
    // close foreach loop
       
       
    echo "after\n";
       
    print_r($cart_array);
       echo 
    "\n";
    ?>
    Produces the following output:

    Code:
     
    before
    Array
    (
        [0] => Array
            (
                [item_id] => 12345
                [quantity] => 1
            )
    
        [1] => Array
            (
                [item_name_1] => Two Blocks of Iroko No.2
                [quantity_1] => 3
            )
    
        [2] => Array
            (
                [item_name_2] => Solid Light Oak Tall Mantle Clock
                [quantity_2] => 1
            )
    
    )
    
    after
    Array
    (
        [0] => Array
            (
                [item_id] => 12345
                [quantity] => 1
            )
    
        [1] => Array
            (
                [item_name_1] => Two Blocks of Iroko No.2
                [quantity_1] => 1
            )
    
        [2] => Array
            (
                [item_name_2] => Solid Light Oak Tall Mantle Clock
                [quantity_2] => 1
            )
    
    )
    SCJP 2, SCWCD 5, ZCE 5.3

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles