Problem with array_count_values($cart)

Discussion in 'PHP' started by ianlufc, Feb 25, 2008.

  1. #1
    Hi Guys

    im using array_count_values($cart) to try and count the values in an array and get the following error

    Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in C:\Users\htdocs\projectBookstore\show_cart.php on line 48

    the contents are titles of books so i thought they would be strings anyone have any idea how to solve, or use a toString method on the array contents NOT to turn the entire array to one string but serperate strings...

    Cheers Guys
     
    ianlufc, Feb 25, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Do a:
    
    printf('<pre>%s</pre>', print_r($cart, true));
    
    PHP:
    And tell us what output you get.
     
    nico_swd, Feb 25, 2008 IP
  3. ianlufc

    ianlufc Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Array
    (
    [The Lemur] => 1
    )


    /////////////////////////

    The Lemur ia a title of a book in the array
     
    ianlufc, Feb 25, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Try
    
    array_count_values(array_keys($cart));
    
    PHP:
    Or perhaps array_sum() is what you're looking for?
     
    nico_swd, Feb 25, 2008 IP
  5. ianlufc

    ianlufc Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    nah mte it didnt work

    what im doing is creating a bookstore adding books to a cart and then when purchasing a book allowing the user to change the qty of books..it works fine for qty of 1 but when i go to change qty it errors..anyways thanks for your help i know its hard when you havent got the code in front of you

    cheers

    Ian
     
    ianlufc, Feb 25, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Hm, not sure why you need array_count_values() then. Wouldn't be something like this easier?

    
    $title = 'The Lemur';
    $quantity = 1;
    
    if (isset($cart[$title]))
    {
        $cart[$title] += $quantity;
    }
    else
    {
        $cart[$title] = $quantity;
    }
    
    PHP:
    ??
     
    nico_swd, Feb 25, 2008 IP
  7. ianlufc

    ianlufc Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?
    include (functions.php');
    // The shopping cart needs sessions, so start one
    session_start();

    if($new)
    {
    //new item selected
    if(!session_is_registered("cart"))
    {
    $cart = array();
    session_register("cart");
    $items = 0;
    session_register("items");
    $total_price = "0.00";
    session_register("total_price");
    }
    if($cart[$new])
    $cart[$new]++;
    else
    $cart[$new] = 1;
    $total_price = calculate_price($cart);
    $items = calculate_items($cart);

    }
    if($save)
    {
    foreach ($cart as $isbn => $qty)
    {
    if($$isbn=="0")
    unset($cart[$isbn]);
    else
    $cart[$isbn] = $$isbn;
    }
    $total_price = calculate_price($cart);
    $items = calculate_items($cart);
    }

    do_html_header("Your shopping cart");

    if($cart&&array_count_values($cart))
    display_cart($cart);
    else
    {
    echo "<p>There are no items in your cart";
    echo "<hr>";
    }
    $target = "index.php";

    // if we have just added an item to the cart, continue shopping in that category
    if($new)
    {
    $details = get_book_details($new);
    if($details["catid"])
    $target = "show_cat.php?catid=".$details["catid"];
    }
    display_button($target, "continue-shopping", "Continue Shopping");
    $path = $PHP_SELF;
    $path = str_replace("show_cart.php", "", $path);
    display_button("https://".$SERVER_NAME.$path."checkout.php", "go-to-checkout", "Go To Checkout");
    do_html_footer();
    ?>


    thats a part of the code mate the error happens when i click save,
    iv been making this myself and looking at a book for parts of it so its a mixture of both.

    basically when i click save (which is save changes to the qty input box) i want it to take the new value as qty thats when i get the ERROR

    Cheers dude
     
    ianlufc, Feb 25, 2008 IP