0

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.

12
  • 2
    I don't see that function returning anything at all. It just echoes some HTML? You should also learn how to pass variables to your method instead of using global when it isn't needed. Globals should be avoided when ever possible. Commented Oct 2, 2016 at 15:16
  • Can't you pass the arguments to your function normally instead of using global variables? Commented Oct 2, 2016 at 15:19
  • It seems your example calls the function only once. Commented Oct 2, 2016 at 15:19
  • 2
    Regarding creating functions in functions in PHP, you should read this question: stackoverflow.com/questions/1631535/function-inside-a-function. Like it says, you can do it, but it won't behave as expected.. Commented Oct 2, 2016 at 15:22
  • 1
    Added the comment as an answer. Thanks @cale_b for the heads up. :) Commented Oct 3, 2016 at 5:56

1 Answer 1

3

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:

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you again sir, nice catch. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.