Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Send a variable to the template in Smarty

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
sebix7
Smarty Rookie


Joined: 15 Jan 2021
Posts: 5

PostPosted: Fri Jan 15, 2021 4:08 am    Post subject: Send a variable to the template in Smarty Reply with quote

Hello! I have a view with a search engine and several tabs. Depending on what I am looking for I get results on the different tabs.

search.php

Code:
 $totalResults = 0;

    $return = array();

   
    $results = $user->search_quick($_POST['query']);

   if($results) {

      foreach($results as $key=>$value) {
         $totalResults++;
      }

      $smarty->assign('results', $results);
      $smarty->assign('totalResults', $totalResults);

      $return['results'] = $smarty->fetch('ajax.search.tpl');

   }

   return_json($return);



Function search_quick in user class:

Code:
public function search_quick($query) {

        global $db, $system;

        $results = [];

        /* search users */

        $get_users = $db->query(sprintf('SELECT user_id, user_name, user_firstname, user_lastname, user_gender, user_picture, user_subscribed, user_verified FROM users WHERE user_name LIKE %1$s OR user_firstname LIKE %1$s OR user_lastname LIKE %1$s OR CONCAT(user_firstname,  " ", user_lastname) LIKE %1$s LIMIT %2$s', secure($query, 'search'), secure($system['min_results'], 'int', false) )) or _error("SQL_ERROR_THROWEN");

        if($get_users->num_rows > 0) {

            while($user = $get_users->fetch_assoc()) {

                $user['user_picture'] = get_picture($user['user_picture'], $user['user_gender']);

                /* get the connection between the viewer & the target */

                $user['connection'] = $this->connection($user['user_id']);

                $user['sort'] = $user['user_firstname'];

                $user['type'] = 'user';

                $results[] = $user;

            }

        }

        /* search pages */

        $get_pages = $db->query(sprintf('SELECT * FROM pages WHERE page_name LIKE %1$s OR page_title LIKE %1$s LIMIT %2$s', secure($query, 'search'), secure($system['min_results'], 'int', false) )) or _error("SQL_ERROR_THROWEN");

        if($get_pages->num_rows > 0) {

            while($page = $get_pages->fetch_assoc()) {

                $page['page_picture'] = get_picture($page['page_picture'], 'page');

                /* check if the viewer liked the page */

                $page['i_like'] = $this->check_page_membership($this->_data['user_id'], $page['page_id']);

                $page['sort'] = $page['page_title'];

                $page['type'] = 'page';

                $results[] = $page;

            }

        }

        /* search groups */

        $get_groups = $db->query(sprintf('SELECT * FROM `groups` WHERE group_privacy != "secret" AND (group_name LIKE %1$s OR group_title LIKE %1$s) LIMIT %2$s', secure($query, 'search'), secure($system['min_results'], 'int', false) )) or _error("SQL_ERROR_THROWEN");

        if($get_groups->num_rows > 0) {

            while($group = $get_groups->fetch_assoc()) {

                $group['group_picture'] = get_picture($group['group_picture'], 'group');

                /* check if the viewer joined the group */

                $group['i_joined'] = $this->check_group_membership($this->_data['user_id'], $group['group_id']);

                $group['sort'] = $group['group_title'];

                $group['type'] = 'group';

                $results[] = $group;

            }

        }

        /* search events */

        $get_events = $db->query(sprintf('SELECT * FROM `events` WHERE event_privacy != "secret" AND event_title LIKE %1$s LIMIT %2$s', secure($query, 'search'), secure($system['min_results'], 'int', false) )) or _error("SQL_ERROR_THROWEN");

        if($get_events->num_rows > 0) {

            while($event = $get_events->fetch_assoc()) {

                $event['event_picture'] = get_picture($event['event_cover'], 'event');

                /* check if the viewer joined the event */

                $event['i_joined'] = $this->check_event_membership($this->_data['user_id'], $event['event_id']);

                $event['sort'] = $event['event_title'];

                $event['type'] = 'event';

                $results[] = $event;

            }

        }

        /* sort results */

        function sort_results($a, $b){

            return strcmp($a["sort"], $b["sort"]);

        }

        usort($results, 'sort_results');

        return $results;

    }


Template search.tpl:

Code:
<div class="tab-content">
                       
        <!-- all -->
        <div class="tab-pane active" id="all">
        {if $totalResults > 1}
            <ul>
                {foreach $results['posts'] as $post}
                {include file='__feeds_post.tpl'}
                {/foreach}
                {foreach $results['articles'] as $post}
                {include file='__feeds_post.tpl'}
                {/foreach}
                {foreach $results['users'] as $_user}
                {include file='__feeds_user.tpl' _tpl="list" _connection=$_user['connection']}
                {/foreach}
                {foreach $results['pages'] as $_page}
                {include file='__feeds_page.tpl' _tpl="list"}
                {/foreach}
                {foreach $results['groups'] as $_group}
                {include file='__feeds_group.tpl' _tpl="list"}
                {/foreach}
                {foreach $results['events'] as $_event}
                {include file='__feeds_event.tpl' _tpl="list"}
                {/foreach}
            </ul>
        {else}
            <div class="text-center text-muted mtb10">
                <img width="25%" src="{$system['system_url']}/content/themes/{$system['theme']}/images/no_results.png">
                <p class="mt10 mb10"><strong>{__("No results to show")}</strong></p>
            </div>
        {/if}
        </div>
        <!-- all -->
                       
        <!-- posts -->
        <div class="tab-panel" id="posts">
        {if count($results['posts']) > 0}
        <ul>
            {foreach $results['posts'] as $post}
                {include file='__feeds_post.tpl'}
            {/foreach}
        </ul>
        {else}
            <div class="text-center text-muted mtb10">
                <img width="25%" src="{$system['system_url']}/content/themes/{$system['theme']}/images/no_results.png">
                <p class="mt10 mb10"><strong>{__("No results to show")}</strong></p>
            </div>
        {/if}
        </div>
        <!-- posts -->

        <!-- blogs -->
       {if $system['blogs_enabled']}
           <div class="tab-pane" id="articles">
           {if count($results['articles']) > 0}
               <ul>
                   {foreach $results['articles'] as $post}
                       {include file='__feeds_post.tpl'}
                   {/foreach}
               </ul>
           {else}
               <div class="text-center text-muted mtb10">
                   <img width="25%" src="{$system['system_url']}/content/themes/{$system['theme']}/images/no_results.png">
                   <p class="mt10 mb10"><strong>{__("No results to show")}</strong></p>
               </div>
           {/if}
           </div>
       {/if}
       <!-- blogs -->

       /* Same with users, pages, groups y events */

   </div>


return_json function:

Code:
function return_json($response = array()) {

        header('Content-Type: application/json');

        exit(json_encode($response));

    }



When I do a search, it shows me the possible results in the respective tabs, but in the All tab it does not show me the total results, that is, the set of results from all the tabs.

I'm sure I'm doing something wrong. If you could help me, I would appreciate it.
Back to top
View user's profile Send private message
sebix7
Smarty Rookie


Joined: 15 Jan 2021
Posts: 5

PostPosted: Fri Jan 15, 2021 5:36 pm    Post subject: Reply with quote

I found the solution: in the end it was not a matter of sending the $totalResults of the php to the template, but of creating the variable in the same .tpl using Smarty's syntax.

In search.php I removed the foreach and lines with the variable $totalResults.

Code:
$return = array();

$results = $user->search_quick($_POST['query']);

if($results) {

    $smarty->assign('results', $results);

    $return['results'] = $smarty->fetch('ajax.search.tpl');

}

return_json($return);


And in the template search.tpl I added:

Code:
{$totalResults = 0}
{$totalResults = count($results['posts']) + count($results['articles']) + count($results['users']) + count($results['pages']) + count($results['groups']) + count($results['events'])}
[/code]
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP