WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. donzacharias
    Member
    Posted 8 years ago #

    Hi, here is the page:
    http://wp.sacrag.com/

    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?

    Thanks!

  2. TechGnome
    Moderator
    Posted 8 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.

    -tg

  3. donzacharias
    Member
    Posted 8 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
    Member
    Posted 8 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
    Member
    Posted 8 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