[function.array-count-values]:error

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

  1. #1
    hi Guys

    i am using
    if($cart&&array_count_values($cart))
    display_cart($cart);

    to count values and i get an error saying

    "[function.array-count-values]: Can only count STRING and INTEGER values"

    anyone shed any light on it for me
    $cart is an array holding what i know to be strings $

    cheers
     
    ianlufc, Feb 27, 2008 IP
  2. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try to
    ...
    var_dump($cart);
    ...
    PHP:
    and tell us the output
     
    mvl, Feb 27, 2008 IP
  3. ianlufc

    ianlufc Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    im adding books to a shopping cart, i get the error when i change the quantities of the books in the cart hence the NULL for the first book as i tried to change qty to 2

    Output is as follows:
    array(3) { ["Tragicomic Tale of the 9/11 Report"]=> NULL ["The Archaeology of Hunger"]=> int(1) ["A Turkish Triangle"]=> int(1) }

    cheers man
     
    ianlufc, Feb 27, 2008 IP
  4. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    array_count_values counts values but it seems you are trying to count keys.

    Assume you have the following array:
    ...
    $cart = array(
    0 => "this is a book",
    1 => "this is some other book",
    2 => "this is the title of a third book",
    3 => "this is a book",
    4 => "this is a book");
    
    // now use array_count_values
    $counted = array_count_values($cart);
    ...
    
    PHP:
    Now $counted is an array containing:

    "this is a book" => 3,
    "this is some other book => 1,
    "this is the title of a third book" => 1
     
    mvl, Feb 27, 2008 IP
  5. ianlufc

    ianlufc Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    im trying to do exactly what you said count the occurences of the title in an array and give me $qty
    then edit it through an input box in a form if need be

    it works fine when i add one book to the cart
    1=> title

    but if i got to edit the 1 quantity it gives me the integer string error

    have you any idea why this would be happening, or how to flag the code that would be causing the error
     
    ianlufc, Feb 27, 2008 IP
  6. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The var_dump() of $cart you gave me shows the book-titles are the keys of the array, while array_count_values() counts the values, not the keys. Maybe if you post more of the script, including the parts where you change quantities, I can do something about it.
     
    mvl, Feb 27, 2008 IP
  7. ianlufc

    ianlufc Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    //if save - this is in a show_cart function
    //if user preses save it will usualy mean that they have changed the qty's
    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);
    }

    /////below i have stored in a functions.php file where i store all function
    ///// this part is used to display the cart
    function display_cart($cart, $change = true) {

    include("connect.php");
    global $items;
    global $total_price;
    global $price;
    $price = 10.99;

    //top bordersss
    echo "<table border=0 width = 100% cellspacing = 0>
    <form action = show_cart.php method = post>
    <tr><th bgcolor=\"#cccccc\">Item</th>
    <th bgcolor=\"#cccccc\">Price</th><th bgcolor=\"#cccccc\">Quantity</th>
    <th bgcolor=\"#cccccc\">Total</th></tr>";

    foreach ($cart as $isbn => $qty)
    {
    $book = get_book_details($isbn);
    echo "<tr>";
    echo "<td align = left>";
    echo $isbn;//title in main bar
    echo "</td><td align = center>$".number_format($price, 2);
    echo "</td><td align = center>";
    if ($change == true){//change is true when saved is pressed

    echo "<input type = text name = \"$isbn\" value = $qty size = 3>";//qunatity input area
    }
    else{
    echo $qty;
    echo "</td><td align = center>$".number_format($book["price"]*$qty,2)."</td></tr>\n";

    }

    }//close for



    echo "<tr>
    <th colspan = 2 bgcolor=\"#cccccc\">&nbsp;</td>
    <th align = center bgcolor=\"#cccccc\">
    $items<!-- amount of items -->
    </th>
    <th align = center bgcolor=\"#cccccc\">
    \$".number_format($total_price, 2).
    "</th>
    </tr>";

    echo "</form></table> <p> <hr> ";

    // session_destroy();

    }//close function


    //////i have the input field to change the qty part in bold it gets called the $isbn which is the title of each book in the cart ...each one wil be different

    i understand its tough looking at code dumps man i appreciate wat youv said already, you have made me understabd in slightly better already
     
    ianlufc, Feb 27, 2008 IP
  8. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You don't need the array_count_values() function. Apparently you are already keeping track of the amount of books per title in the $cart array:
    foreach ($cart as $isbn => $qty)
    PHP:
    I suggest you change
    if($cart&&array_count_values($cart))
    display_cart($cart);
    
    PHP:
    to
    
    if(is_array($cart))
    display_cart($cart);
    
    PHP:
     
    mvl, Feb 27, 2008 IP
  9. ianlufc

    ianlufc Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i tried that and it still has the same effect when i change the value of qty and click save the textbox has "size" as text inside it and it unsets that book from the array....

    no idea why, i have to head off now man...cheers for your help

    i dont really understand that whole key part of the
    foreach ($cart as $isbn => $qty)


    il have to look into that

    Thanks Man
     
    ianlufc, Feb 27, 2008 IP