John Doe
John Doe

Reputation: 158

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.

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. 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.

Upvotes: 0

Views: 712

Answers (1)

M. Eriksson
M. Eriksson

Reputation: 13635

Regarding creating functions inside other functions in PHP, you should read this 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):

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.

2) Calling x() twice will cause PHP to redeclare function y(), leading to a fatal error:

Upvotes: 3

Related Questions