简体   繁体   English

PHP函数不能运行一次以上

[英]PHP Function Cannot be Run More Than Once

I'm working with a custom PHP script that interacts with the Wordpress database and I've bumped into a small issue. 我正在使用与Wordpress数据库进行交互的自定义​​PHP脚本,因此遇到了一个小问题。

I've written a function that I would like to be able to run multiple times with different variable values sent to it each time it is run, the function is as such: 我编写了一个函数,希望每次运行时都可以发送不同的变量值来多次运行,该函数是这样的:

function ProductByCategory()
{
    // Globalize the Wordpress Database Variable
    GLOBAL $wpdb;
    GLOBAL $term;
    GLOBAL $default;
    // Return All Products in the Category Set by $term
    $return = $wpdb->get_results("SELECT term_id FROM wplz_terms WHERE name = '$term';");
    // Properly Format the Result for an Array
    $array = json_decode(json_encode($return),true);
    // Flatten Array to Simple Array Function
    function array_flatten_recursive($array) { 
    if (!$array) return false;
        $flat = array();
        $RII = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
        foreach ($RII as $value) $flat[] = $value;
        return $flat;
    }
    $flat = array_flatten_recursive($array);    
    // Format for Next MySQL Query
    $in = implode(',', $flat);
    // Resolve Term ID to Object ID(s) 
    $return = $wpdb->get_results("SELECT object_id FROM wplz_term_relationships WHERE term_taxonomy_id IN ($in);");
    // Properly Format the Result for an Array
    $array = json_decode(json_encode($return),true);
    // Refresh $flat Value
    $flat = array_flatten_recursive($array);
    // Format for Next MySQL Query
    $in = implode(',', $flat);  
    // Resolve Products by the Resulting Object ID(s)
    $return = $wpdb->get_results("SELECT p.id, p.post_title, pm.meta_value FROM wplz_posts p INNER JOIN wplz_postmeta pm ON pm.post_id=p.id AND pm.meta_key = '_price' WHERE p.id IN ($in) AND p.post_status = 'publish' ORDER BY p.post_title ASC;");
    // Properly Format the Result for an Array
    $array = json_decode(json_encode($return),true);
    // Set Default Select Value
    echo("<option>" . $default . "</option>");

    foreach($array as $line)
    {
        echo('<option>');
            echo($line['post_title']);
            echo(' - ' . number_format($line['meta_value']) . 'THB');
        echo('</option>');
    }

}

And then in the area of the page where I want to run the function I simply put: 然后在页面上我要运行该功能的区域中,我简单地输入:

<!-- Select CPU Dropdown -->
<!-- Open HTML Select Structure -->
<div class="btn-group bootstrap-select"><select class="selectpicker form-control">

    <?php
    // Set Default Value for Select Drop Down Menu(s)
    $default = "-- None Selected --";
    // Resolve CPU Products
    $term = "CPU";
    // Run ProductByCategory Function
    ProductByCategory();
    ?>

<!-- Close HTML Select Structure -->
</select></div>

For whatever reason, this function runs perfectly the first time that it is called. 无论出于何种原因,该函数在首次调用时都能完美运行。 However whenever I try to redefine $term and $default and then call the function again with the updated variables it simply refuses to return anything. 但是,每当我尝试重新定义$term$default ,然后使用更新的变量再次调用该函数时,它只是拒绝返回任何内容。 I'm rather confused because after quite a while of looking at it I'm not sure where things are going wrong, and thus I have submitted it to you fine people. 我很困惑,因为看了一段时间后,我不确定问题出在哪里,因此我已经将它提交给了您。 Thank you for your help. 谢谢您的帮助。

Regarding creating functions inside other functions in PHP, you should read this SO Q&A: 关于在PHP中的其他函数内部创建函数,您应该阅读以下SO Q&A:

Function inside a function.? 功能内的功能。

Like it says, you can do it, but it won't behave as expected. 就像它说的那样,您可以执行此操作,但是它不会达到预期的效果。 An excerpt from that answer: 该答案的摘录:

(x() = outer function & y() = inner function): (x()=外部函数&y()=内部函数):

Although functions are not limited in scope (which means that you can safely 'nest' function definitions), this particular example is prone to errors: 尽管函数不受范围限制(这意味着您可以安全地“嵌套”函数定义),但是此特定示例容易出错:

1) You can't call y() before calling x(), because function y() won't actually be defined until x() has executed once. 1)您不能在调用x()之前调用y(),因为在执行x()一次之前,实际上不会定义函数y()。

2) Calling x() twice will cause PHP to redeclare function y(), leading to a fatal error: 2)两次调用x()将导致PHP重新声明函数y(),从而导致致命错误:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM