Jump to content

Recommended Posts

Hi.

 

I have built a small script to use in my cart which is run on each page load. I am using AJAX to load this page each time a change is made. The code looks at a string of numbers which change dependant on the items in the cart.

 

In my code below I displayed some example numbers that might appear. The format is always number '+' number ','

 

Each time the code is loaded, regardless if the $cart_check is empty or not, it shows the php notice 'Undefined offset 1', and I can't figure out why.

 

Can anyone help me understand whats happening here, and possibily how to fix it?

$cart_check = '123+0,145+1,134+2,';
$cart_check = trim($cart_check, ',');            

if ($cart_check == "") {
    $cart_check_row_id = '';
} else {
    $cart_check = explode(',', $cart_check);

    $key = array_search(158, $cart_check);
    
    if ($key != "") {
        $cart_check_row_id = explode("+", $cart_check[$key]);
    } else {
        $cart_check_row_id = array(
            "0" => 0,
            "1" => 0
        );            
    }
    $cart_check_row_id = $cart_check_row_id[1];
}


Link to comment
https://forums.phpfreaks.com/topic/278635-undefined-offset-1-issue/
Share on other sites

your cart data definition is very problematic and is resulting in far too much code for any action on the cart.

 

just use an array, where the array index is the id and the value is the quantity, especially since your code is making an array out of the cart every time you reference it. just eliminate all that conversion code and store the cart as an array in the first place.

 

once you have the cart in the form of an array (your existing code or my suggested way of storing it), you need to use array functions to test and access the data. count() would tell you how may elements are in the array. empty() would tell you if the array is empty. your error implies that there are not two elements in the array (index values start at 0, not 1) and you are trying to access the second one.

Hi mac_gyver.

 

Thank you for your response.

 

Unfortunately its not really my cart. I am using a prebuilt cart so I can't use your suggested way unfortunately.

 

I don't understand why the code isn't working, as I have said in conditions, if there is no data then leave $cart_check_row_id empty, if there is data then check for the number in the array, if its there get the key, if key then set that section of the array to $cart_check_row_id, if no key then create an array with 0's.

 

I can't see how the array is empty, or doesn't have two elements.

your array_search() for just the key value will never find anything in exploded $cart_check . to find a key '158', you would need to use a wild card/preg_match type of search because the values at that point are '158+x'

 

to manipulate this cart, take the incoming $cart_check = '123+0,145+1,134+2,'; string and convert it to an array ($cart[123] = 0; $cart[145] = 1; ...) like i suggested. then you can find any key/quantity in it.

If you don't see how the array is empty, then start debugging by printing stuff out!

<?php



$cart_check = '123+0,145+1,134+2,';

$cart_check = trim($cart_check, ',');



if ($cart_check == "") {

    print("Empty cart_check\n");

    $cart_check_row_id = '';

} else {

    $cart_check = explode(',', $cart_check);

    print_r($cart_check);



    $key = array_search(158, $cart_check);

    print("Key: $key\n");

    

    if ($key != "") {

        $cart_check_row_id = explode("+", $cart_check[$key]);

        print("Non-empty key -- cart_check_row_id: " . print_r($cart_check_row_id, true));

    } else {

        $cart_check_row_id = array(

            "0" => 0,

            "1" => 0

        );

        print("Empty key -- cart_check_row_id: " . print_r($cart_check_row_id, true));            

    }

    $cart_check_row_id = $cart_check_row_id[1];

}



print("Final cart_check_row_id: $cart_check_row_id\n");
php -f cartcheck.php

Array

(

    [0] => 123+0

    [1] => 145+1

    [2] => 134+2

)

Key:

Empty key -- cart_check_row_id: Array

(

    [0] => 0

    [1] => 0

)

Final cart_check_row_id: 0

Okay. I am pretty new to all this, so everything you see as simple is mega complicated to me.

 

I have done this:

$cart_check = '157+0,155+1,';
$cart_check = trim($cart_check, ',');			
$cart_check = explode(',', $cart_check);

which outputs this:

 

array(2) { [0]=> string(5) "157+0" [1]=> string(5) "155+1" }

 

Is this what you mean, as its basically what I was already doing isn't it?

 

If not, can you show me how?

array_search() cannot find the string '158' in values that are like - "157+0", "155+1" you must either use a more advanced search or get your data into a form that you can find the key values.

 

since you are going have to wasted a bunch of time trying to get this to work, just trash this cart and get/write one that stores the cart in an array like i already suggested.

Edited by mac_gyver
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.