MyBB Community Forums

Full Version: Store db data to header variable Problems
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I'm trying to get data from database and store it to the variable which would be accessible from header template. I did so right in global.php file (don't know if it is proper way) but I've figured out that forum don't work properly. For example, some parts of registration form don't display (especially Referrer_member, verification image, sequirity question) and some links are bad redirected. Is there better solution how to do it? Have anybody idea what can affect the code?

Thanks in advance.
Hi,

Can you paste the code you added please?
(2016-04-11, 01:53 PM)Euan T Wrote: [ -> ]Hi,

Can you paste the code you added please?
The data I'm selecting is not from mybb_ tables. I forgot to mention that my forum is integrated to the codeigniter project. I work on advertisement portal and I need to have topped ads displayed on  the top of the forum.

Here's the code:

$query = $db->query("
    SELECT dlzka_topovania, nazov_obchodu
    FROM settings
    WHERE settings_id = 1
");

while($settings = $db->fetch_array($query)) {
    $dlzka_topovania = $settings['dlzka_topovania'];
    $nazov_obchodu = $settings['nazov_obchodu'];
}

$query = $db->query("
    SELECT inzerat_id, datum_do
    FROM inzerat_top
    ORDER BY inzerat_id
");
$i = 0;
while($ads = $db->fetch_array($query)) {
    if(strtotime($ads['datum_do']) > strtotime("now")) {
        $top_ads[$i] = $ads['inzerat_id'];
        $i++;
    }
}

$query = "
    SELECT inzerat_id, meno, mesto, datum_narodenia, verified, date_added, topovanie
    FROM inzerat
    WHERE inzerat_id IN (";

    $values = "";
    for($i = 0; $i < count($top_ads); $i++)
    {
        $values .= $top_ads[$i];
        if($i < count($top_ads) - 1) {
            $values .= ", ";
        }
    }

$query .= $values;
$query .= ") 
    AND status = 'active' AND closed_temporarily = 0 ORDER BY inzerat_id";
$query = $db->query($query);

$i = 0;

$top_ads = "";

require_once 'my_functions/page.php';
require_once 'my_settings/paths.php';

while($topped_ads = $db->fetch_array($query)) {
    $ads[$i]['inzerat_id'] = $topped_ads['inzerat_id'];
    $ads[$i]['meno'] = $topped_ads['meno'];
    $ads[$i]['mesto'] = get_mesto($topped_ads['mesto']);
    $ads[$i]['verified'] = $topped_ads['verified'];
    $ads[$i]['date_added'] = $topped_ads['date_added'];
    $ads[$i]['topovanie'] = $topped_ads['topovanie'];
    $date_of_birth = $topped_ads['datum_narodenia'];

    $from     = new DateTime($date_of_birth);
    $to     = new DateTime('today');
    $ads[$i]['vek'] = (int)$from->diff($to)->y;

    //get profile photo
    $query_photo = $db->query("
        SELECT url
        FROM inzerat_photos
        WHERE inzerat_id = {$ads[$i]['inzerat_id']}
        AND profile_photo = 1
    ");
    while($photo = $db->fetch_array($query_photo)) {
        $photo_url = $photo['url'];
    }

    if(empty($photo_url)) {
        $photo_url = 'public/img/nophoto-female.jpg';
    }

    $top_ads .= '<div class="col-sm-2 ad">';
    $top_ads .= '<div class="row user-photo">';
    $top_ads .= '<a href="'. ROOT_PATH . '/user/profile/' . $ads[$i]['inzerat_id'] . '"><img src="../' . $photo_url . '" width="150" height="207" alt="user_photo"></a>';
    $top_ads .= '</div>';
    $top_ads .= '<div class="row user-info">';
    $top_ads .= '<p>' . $ads[$i]['meno'] . ' - (' . $ads[$i]['vek'] . ')';
    $top_ads .= '<br>';
    $top_ads .= $ads[$i]['mesto'] . '</p>';
    $top_ads .= '</div>';
    $top_ads .= '</div>';

    unset($photo_url);

    $i++;
}


/*------*/
I added it before this in global:

// Set up some of the default templates
eval('$headerinclude = "'.$templates->get('headerinclude').'";');
eval('$gobutton = "'.$templates->get('gobutton').'";');
eval('$htmldoctype = "'.$templates->get('htmldoctype', 1, 0).'";');
eval('$header = "'.$templates->get('header').'";');
Interesting. That shouldn't stop the pages from working. However, any variables you wish to use in templates must be global, so you'd need to do:

global $top_ads;

etc.

Can you link me to your forum so I can see the problems on registration?
(2016-04-11, 03:53 PM)Euan T Wrote: [ -> ]Interesting. That shouldn't stop the pages from working. However, any variables you wish to use in templates must be global, so you'd need to do:

global $top_ads;

etc.

Can you link me to your forum so I can see the problems on registration?

I tried to set the variable as global but it did not help. 
The forum isn't uploaded yet. I'm testing it on localhost.
Ah, can you show me a screenshot or something in that case then?
[attachment=36072]

Ok.

The issue is solved.

In my code I use variable $settings and this variable is probably already in use and I overwrote it with my data. I renamed it to $setting and now it works well.