Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    array_keys() taking in invalid arguments

    I had a script that worked great, made a small change to use end() function on the array and get the last value of an array in a class... Didnt work, found end() and undefined method, so I guess I may not be able to use end() in a class?

    The problem is even after changing back to the original code (I made so few changes, I cant be believe I forgot an adjustment to it) I cant get rid of these array_key() errors.

    Here is a link to the page with the errors:

    http://paperlesswasp.com/millionaire...id=mk/miracle1

    Here is the code I am having problems with:

    PHP Code:

    class shoppingCart
    {
        protected 
    $items = array();

        public function 
    GetItems()
        {
            return 
    array_keys($this->items);
        }


    PHP Code:
        foreach($shopping_cart->GetItems() as $product_id)
        {
            echo 
    render_shopping_cart_row($shopping_cart$product_id$line_item_counter$book_id);
            
    $line_item_counter++;
        } 
    I have not made any changes to this snippets shown, the places I tried incorporating the end() is here:

    $quantity = end($shopping_cart->GetItemQuantity($product_id));

    and here at different times:

    PHP Code:
    public function GetItemQuantity($product_id)
        {
            return 
    $this->end(items);
        } 
    Like I said told me it was an undefined method for the one in the GetItemQuantity($product_id) function.


    Any thoughts?

    Thanks a lot

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    end isn't defined in this class, so the error is correct. Assuming you want to use the array end(), your usage is not correct; end is a pre-defined function as a part of the PHP core.
    PHP Code:
    public function GetItemQuantity($product_id)
    {
        return 
    end($this->items);

    Looks to me like GetItemQuantity won't need to take any parameters either since you don't do anything with $product_id.

    As for the array keys, this class or a subclass has either unset the $items or altered its type to a scalar type. You'll need to var_dump($this->items) before trying the array_keys to see what's in it.

    This isn't your development domain is it? This looks like an actual site; you should not be performing any modifications within a live environment until its been fully tested in a local one.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    I did the var_dump and I got a value of NULL.

    Thanks for pointing out that problem with the end() function...makes a lot of sense having it set up the way you mentioned.

    I tried setting wamp, mainly because so many other people are doing it for development. Although I started getting all kinds of errors on areas that have worked great for a long time on all browsers... I am not a big fan of debugging, so I would like to reduce it and reconfiguring my code to run on different platforms, especially ones that the public will never see.

    Thanks a lot for the help

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    To fix the $items problem, go through the shoppingcart class and any subclasses looking for either unset($this->items);, $this->items = null;, or any assignment directly from input to $this->items that could contain bad data (from cookies, sessions or text areas). Adding to $this->items[] cannot unset the array, so the problem won't be an appending issue.

    PHP code that does not trigger errors will not cause problems over different architecture. On the other hand, some errors that are not caught (if display_errors is not enabled or if error reporting is set too low), can cause failures on different machines. There are always exceptions to these rules as well, PHP has sections for dealing with apache and IIS servers. Since the functions are different, you cannot use an apache function with IIS (unless they've mapped some, which I don't think they have).
    The second must have is an IDE with an integrated debugging environment. I use the 'Zend eclipse' (google for that), which is eclipse with the zend environment and php-xdebug and zend debugger add-ons. Debuggers allow you to set break points and walk through the processing of code. You cannot use things like sessions and cookies within the debugger (easily), but you can simulate with variable setting instead. This is a part of the reason why I don't recommend using superglobals within classes unless its full purpose is to manage the superglobals, and any other class is either provided this data or aggregates the superglobal handler itself.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks for the advice...

    Here is the code I believe maybe causing the trouble

    PHP Code:
    protected $items = array();

    function 
    __construct()
       {
            
    $this->items unserialize($_SESSION['cart']); // get items from session on object init
       

    The construct was a user submitted solution that got the session_start to work properly

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You have not verified that the cart is set.
    PHP Code:
    $this->items = isset($_SESSION['cart']) ? unserialize($_SESSION['cart']) : array(); 
    This may still give you problems, arrays like objects will serialize themselves when writing to a session, so in order to unserialize you must have manually serialized it prior to the session serialization process. If you did not do so, the cart is already an array which needs to be checked as well:
    PHP Code:
    $this->items = isset($_SESSION['cart']) && is_array($_SESSION['cart']) ? $_SESSION['cart'] : array(); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks for that, I plugged it in and the errors went away. although right now I am getting logic errors. Like before I am trying to the last key or value from $items array in my shoppingCart() class.

    As of now my var_dump($shopping_cart->get_shopping_cart()); is returning

    recap:
    PHP Code:
    function get_shopping_cart()
       {
            return 
    $this->items;
       } 
    array(0) { }


    The purpose is that for every refresh I am suppose to add another value to the array incrementing by one. array(1,2,3,4,5...etc.)

    The tutorial tries to do this in a foreach() looping over a function that prints out a set of cells from a table with variables stored in each cell. This is fine and all except that the foreach() is not adding one more to one cell... but duplicating the entire set of cells counting up by one from the last cell.

    So I am trying to add one more to current cell on a refresh and not create a whole new set of cells in the table. This is why I wanted to use the end() to get the last value from $items array(). I believe that this result will be achieved by dropping my foreach() so the function printing the cells will not loop over again and agin until all the keys in the array are echoed. I have tried to figure this out, but to no avail, just took a few steps backwards.

    my foreach():
    PHP Code:
    $line_item_counter 1;
        
        foreach(
    $shopping_cart->GetItems() as $product_id)
        {
            echo 
    render_shopping_cart_row($shopping_cart$product_id$line_item_counter$book_id);
            
    $line_item_counter++;
        } 

    my render_shopping_cart_row():
    PHP Code:
    function render_shopping_cart_row(shoppingCart $shopping_cart$product_id$line_item_counter$book_id)
    {
        
    $quantity $shopping_cart->GetItemQuantity($product_id);
        
    print_r($quantity);

        
    $output "
        <tr>
            <td>
                $book_id
            </td>
            <td>
                $quantity
            </td>
            <td>
                $amount
            </td>
        </tr>
        "
    ;
            return 
    $output;



    Lastly I did do a if statement and resulted that the $shopping_cart object's session is set and serialized just after instantiation.

    Thanks a lot for your continued help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •