change $wpdb to global scope for use in widget? (5 posts)

  1. donzacharias
    Posted 10 years ago #

    Hi, here is the page:

    the area in question is the error in the second SELECT in the right column. The following PHP is in the HEAD:

    $authorsArchives = $wpdb->get_results("SELECT * FROM $wpdb->users");

    and then the following PHP is used in a King Text widget:

    <?php foreach ($authorsArchives as $authorArchiveLink): ?>
    <option value="<?php echo $authorArchiveLink->user_nicename; ?>"><?php echo $authorArchiveLink->display_name; ?></option>
    <?php endforeach; ?>

    and you see what that does on the above link. I'm guessing it has something to do with the scope of $wpdb but I don't really understand how to fix that. Can anybody help?


  2. TechGnome
    Posted 10 years ago #

    Actualy... it's not the select that's causing your error.... it's the foreach code.... for what ever reason, it thinks that $authorsArchives is empty.... I'd suspect the scope of the $authorsArchives rather than $wpdb. But with out more source code, it's hard to say.


  3. donzacharias
    Posted 10 years ago #

    the foreach loop works fine when i use it in the header.php file, but barfs when i try to use it in the widget. what other source code do you think would help you see what's going on?

  4. sunburntkamel
    Posted 10 years ago #

    at the start of the widget, you'll need to add:

    global $authorArchiveLink;

    if you really need wpdb as well, then add

    global $wpdb;

  5. donzacharias
    Posted 10 years ago #

    I don't know why I was stuck on doing the variables in the header.php. It worked fine when i stuck all the PHP in the widget. Thanks so much!!

Topic Closed

This topic has been closed to new replies.

About this Topic