How to get free information from a database and display them

In this example, consider creating a template tag display 5 most recent comments and the avatar of user, add a comment.

The source code of all of template tags do not belong to any module are in the folder themes/functions. We add our file for the template tag. Call it show_last_comments.php.

Example:

<?php
echo 'DIAFAN.CMS';

Now, if one of the templates located in the folder themes, write a template tag <insert name="show_last_comments">, the site we see the inscription "DIAFAN.CMS".

We extend our method – namely, we choose using SQL-query the last active 5 comments, as well as user names, they added.

Example:

$rows = DB::query_fetch_all("SELECT u.name, c.text FROM {comments} AS c LEFT JOIN {users} AS u ON c.user_id=u.id WHERE c.act='1' AND c.trash='0' ORDER BY c.id DESC LIMIT 5");
Attention!
LEFT JOIN in this case we use in order to select all of the latest comments, regardless of who left them – an authorized user or a guest. For non-authorized users field user_id of table {comments} is set to 0. For the comments left by the authorized user, this field corresponds id to the table {users}.

Now the file will look like this:

Example:

<?php
$rows
= DB::query_fetch_all("SELECT u.name, c.text FROM {comments} AS c
LEFT JOIN {users} AS u ON c.user_id=u.id WHERE c.act='1' AND c.trash='0'
ORDER BY c.id DESC LIMIT 5"
);
$text = '';
foreach (
$rows as $row)
{
    
$text .= $row['text'].'<br>';
}
echo
$text;

But in this form it does not output anything. Let us derive the text of comments:

Example:

$text = '';
foreach (
$rows as $row)
{
    
$text .= $row['text'].'<br>';
}
echo
$text;

The source code became look like this:

Example:

<?php
$rows
= DB::query_fetch_all("SELECT u.name, c.text FROM {comments} AS c
LEFT JOIN {users} AS u ON c.user_id=u.id WHERE c.act='1' AND c.trash='0'
ORDER BY c.id DESC LIMIT 5"
);
$text = '';
foreach (
$rows as $row)
{
    
$text .= $row['text'].'<br>';
}
echo
$text;

Due to the presence of the user names, avatars get them is not difficult. But before the withdrawal is necessary to check whether the settings of the system included the ability to add them:

Example:

if ($this->diafan->configmodules("avatar", "users"))
{
    
// ...
}

If the opportunity is there, and there is an avatar image that can be displayed:

Example:

if ($this->diafan->configmodules("avatar", "users") && file_exists(ABSOLUTE_PATH.USERFILES.'/avatar/'.$row['name'].'.png'))
{
    
$avatar = BASE_PATH.USERFILES.'/avatar/'.$row['name'].'.png';
    
$avatar_width = $this->diafan->configmodules("avatar_width", "users");
    
$avatar_height = $this->diafan->configmodules("avatar_height", "users");
    
$text .= '<img src="'.$avatar.'" width="'.$avatar_width.'"
    height="'
.$avatar_height.'" alt="'.$row['name'].'"><br>';
}

Get the code:

Example:

<?php
$rows
= DB::query_fetch_all("SELECT u.name, c.text FROM {comments} AS c
LEFT JOIN {users} AS u ON c.user_id=u.id WHERE c.act='1' AND c.trash='0'
ORDER BY c.id DESC LIMIT 5"
);
$text = '';
foreach (
$rows as $row)
{
    if (
$this->diafan->configmodules("avatar", "users") && file_exists(ABSOLUTE_PATH.USERFILES.'/avatar/'.$row['name'].'.png'))
    {
        
$avatar = BASE_PATH.USERFILES.'/avatar/'.$row['name'].'.png';
        
$avatar_width = $this->diafan->configmodules("avatar_width", "users");
        
$avatar_height = $this->diafan->configmodules("avatar_height", "users");
        
$text .= '<img src="'.$avatar.'" width="'.$avatar_width.'"
        height="'
.$avatar_height.'" alt="'.$row['name'].'"><br>';
    }
    
$text .= $row['text'].'<br>';
}
echo
$text;

At the beginning of the file add the following code to avoid direct access to the file.

Example:

<?php
if (! defined('DIAFAN'))
{
    
$path = __FILE__; $i = 0;
    while(!
file_exists($path.'/includes/404.php'))
    {
        if(
$i == 10) exit; $i++;
        
$path = dirname($path);
    }
    include
$path.'/includes/404.php';
}

We get the final code:

Example:

<?php
if (! defined('DIAFAN'))
{
    
$path = __FILE__; $i = 0;
    while(!
file_exists($path.'/includes/404.php'))
    {
        if(
$i == 10) exit; $i++;
        
$path = dirname($path);
    }
    include
$path.'/includes/404.php';
}

$rows = DB::query_fetch_all("SELECT u.name, c.text FROM {comments} AS c
LEFT JOIN {users} AS u ON c.user_id=u.id WHERE c.act='1' AND c.trash='0'
ORDER BY c.id DESC LIMIT 5"
);
$text = '';
foreach (
$rows as $row)
{
    if (
$this->diafan->configmodules("avatar", "users") && file_exists(ABSOLUTE_PATH.USERFILES.'/avatar/'.$row['name'].'.png'))
    {
        
$avatar = BASE_PATH.USERFILES.'/avatar/'.$row['name'].'.png';
        
$avatar_width = $this->diafan->configmodules("avatar_width", "users");
        
$avatar_height = $this->diafan->configmodules("avatar_height", "users");
        
$text .= '<img src="'.$avatar.'" width="'.$avatar_width.'"
        height="'
.$avatar_height.'" alt="'.$row['name'].'"><br>';
    }
    
$text .= $row['text'].'<br>';
}
echo
$text;

Then, basically any site template, such as the themes/site.php, the function can be called up anywhere in the template tag <insert name="show_last_comments">.