Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [Solved] Array_sum problem

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #30173
    Thijsvani
    Member

    Hi,

    I’m having a problem with the array_sum function. I want to output all custom fields from al posts in this year and then display the total of all numbers from those custom fields. So this is the code I’ve been using



    id="aantal_homepage_home" else { ?> id="aantal_homepage" >

    query_posts('showposts=20'. 'year=' . $year); ?>




    $bomen_jaar = get_post_meta($post->ID, "gegroeide_bomen", true);
    //check that we have a custom field
    if ($bomen_jaar != "")
    {
    // Separate our comma separated numbers into an array
    $bomen_jaar = explode(",", $bomen_jaar);
    //loop through our new array
    foreach ($bomen_jaar as $boom)
    {
    echo array_sum ($boom);
    }
    }
    ?>

    Now it doesn’t display anything. When I just echo the variable $boom like this:

      echo $boom;

    It displays all the numbers from those different custom fields. How can help me with adding up these numbers?

    You can watch the site at http://www.groenetaxi.nl/test

    Thanks
    Thijs

    #80470
    Bob
    Member

    Although I’m not sure, ’cause I’m no way near being a PHP expert, I think I found something.

    In your foreach() function, I think you forgot somethings. The syntax for this function is:

    foreach($something as $key => $value)

    It looks to me like you forgot the “=> $value” part. So lets say it should be:

    foreach($bomen_jaar as $boom => $value)

    And then the array_sum would be like this:

    echo array_sum ($value);

    I can’t really explain well and I don’t have PHP access on this computer so I can’t test it, but to me it seems you’re trying to get the array_sum of the $boom, whereas you should be getting it of $value.

    #80461
    Thijsvani
    Member

    Thanks for the reactions but if I echo the $bomen_jaar in a array_sum it outputs the word array several times. The thing with the => $value didn’t work either… If I echo it out with the => value, then I get a string with some zero’s. Without I get a string with the numbers, now I only have to sum them up.

    #80454
    Bob
    Member

    Shouldn’t you change this:

    // Separate our comma separated numbers into an array
    $bomen_jaar = explode(",", $bomen_jaar);

    To this:

    // Separate our comma separated numbers into an array
    $bomen_jaar_exploded = explode(",", $bomen_jaar);

    ?

    I don’t know, maybe it doesn’t matter but to me it looks like you’re trying to explode $bomen_jaar into a variable you again name $bomen_jaar, which would make me think it’s going to keep exploding on and on and on. Because they both got the same name and php is gonna think it has to be exploded again.
    I don’t know. I’m just trying something and maybe it doesn’t even matter they’re the same name :)
    Oh btw, if it makes it easier to explain the problem, you can also PM me in Dutch, I’m Dutch also.

    #80419
    Thijsvani
    Member

    Thanks for the reactions. When I use a var_dump it outputs a long string as seen on groenetaxi.nl/test. Now I use this code:

    			
    id="aantal_homepage_home" else { ?> id="aantal_homepage" >

    query_posts('showposts=20'. 'year=' . $year); ?>




    $bomen_jaar = get_post_meta($post->ID, "gegroeide_bomen", true);
    //check that we have a custom field
    if ($bomen_jaar != "")
    {
    // Separate our comma separated numbers into an array
    $bomen_jaar = explode(",", $bomen_jaar);
    var_dump($bomen_jaar);
    //loop through our new array
    foreach ($bomen_jaar as $boom);
    {
    echo $boom;
    }
    }
    ?>

    #80353
    Thijsvani
    Member

    I fixed the problem so here’s my final code for people who have the same problem]


    id="aantal_homepage_home" else { ?> id="aantal_homepage" >

    $bomen_sum_2010 = $wpdb->get_var($wpdb->prepare("
    SELECT SUM($wpdb->postmeta.meta_value)
    FROM $wpdb->postmeta, $wpdb->posts
    WHERE $wpdb->postmeta.meta_key='gegroeide_bomen'
    AND $wpdb->postmeta.post_id=$wpdb->posts.id
    AND year($wpdb->posts.post_date)='2010'
    "));
    echo '

    ' . $bomen_sum_2010 . '

    ';
    ?>


    totaal aantal bomen
    dit jaar dankzij Groen vervoer


Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.