duancashi1362 2013-12-02 15:08
浏览 49
已采纳

Wordpress函数= 0

i'm a absolute noob with php coding and I guess after several laughs, the solution is really simple for a professional. I coded a simple average-calculation for the category.php of my wp-template and it works perfectly but I heard that it's the much better way to split design and function and so I tried to convet this to a template function:

<?php

$total = 0;
$count = 0;

foreach($posts as $post)
{
     if(get_field('weight')){ // If we have a value add it to the total and count it
        $total += get_field('weight');
        $count++;
     }
}

$Average = $total / $count; 
echo $Average;       
?>

The result is always "0" because I guess he pulls the variable from above and skips the foreach-procedure. But why?

function averageit()
{
$total = 0;
$count = 0;
foreach($posts as $post)
    if(get_field('weight'))
    $total += get_field('weight');
    $count++;
    $Average = $total / $count;

  echo $Average; 
}

I hope somebody is able to help me. Thank you in advance!

  • 写回答

2条回答 默认 最新

  • dqhdpppm02183 2013-12-02 15:25
    关注

    Your problem is likely to be one of variable scope. The "scope" of a variable is the section of code where that variable "exists", if you will.

    In your case, consider your foreach loop, looping through items in the '$posts' variable. Where is the $posts variable declared? I suspect that at the point of the function running, that variable doesn't exist, so the loop is not being executed at all.

    I'd suggest the following amendments to your function:

    function find_average_weight($posts)
    {
        $total = 0;
        $count = 0;
        foreach($posts as $post)
        {
            if(get_field('weight'))
            {
                $total += get_field('weight');
                $count++;
            }
        }
        $Average = $total / $count;
        return $Average; 
    }
    

    There's a few changes here.

    • While writing the function out I realised your foreach loop had no braces. This can cause issues of readability and indeed make the logic work incorrectly, so I put them in :)
    • I added an parameter to the function to pass the required "$posts" variable in.
    • I changed the function name to be slightly more descriptive, though not knowing the exact application I may be off the mark here, so take the suggestion with a grain of salt.
    • I altered your function to return a value, which you can then do whatever you wish with. This makes the function a little more agnostic regarding what your intentions are - perhaps at some point you will want to find the average of these posts without outputting the results?

    To use the new function and output the result, you could now write a line like this:

    echo(find_average_weight($posts));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序