Page 1 of 1

Help me with the button

Posted: Mon Sep 05, 2016 2:26 am
by cld
Hello there,

I have a button to load more news in web. And i need to hide that button after all the news from database is loaded. But i only got that button hidden after one press which is not all news or i cant hide it at all with my if functions. Heres my functions i use codeigniter.

Controller:

Code: Select all

 public function get_more_news($menu_item_id, $total){
        $this->data['all_items']= $this->{$this->model}->get_items_all($menu_item_id, $this->lang_id);
        $this->data['total'] = $total + $this->items_per_page;
        $current_page = $this->input->get('page') ? (($this->input->get('page') - 1) * $this->items_per_page) - 1 : 0;    
       $this->data['items'] = $this->{$this->model}->get_items($menu_item_id, $this->lang_id, $this->data['total'], $current_page);
        if ($all_items > $this->data['items'])
        {
            $this->data['hide_button'] = 1;
        }    
        else
        {
            $this->data['hide_button'] = 0;
        }
        
        $this->data['use_thumbnails'] = $this->{$this->model}->get_variables('use_thumbnails');
$this->data['thumbnail_prefix'] = $this->{$this->model}->get_variables('thumbnail_prefix');
$this->data['directory'] = $this->{$this->model}->get_variables('directory');
        $this->data['meta_data'] = $this->welcome_model->get_item_meta_data($menu_item_id, $this->lang_id, true);
        $this->load->view($this->theme . '/menu_modules/' . $this->module . '/loader', $this->data);
    }
 
}

Loader.php

Code: Select all

<div id="button_count" data-id="<?= $hide_button; ?> "style="display:none;"></div>
<script>
    
     total= $('#masonry li').length;
        var button = $('#button_count').attr('data-id'); 
        var button1 = Number(button);
        if(button1 === 0){
            $('#LoadMoreNewsButton').hide();
        }
    </script>

This script hides the button instantly. Help me please :)

Re: Help me with the button

Posted: Mon Sep 05, 2016 3:26 am
by cld
Hehe i made it myself simply adding a line in controller and changing the if sentence.

$this->data['items_now'] = count($this->data['items']);
if ( $this->data['all_items'] > $this->data['items_now'])

Re: Help me with the button

Posted: Mon Sep 05, 2016 6:52 pm
by Christopher
Not necessary, but it might make the code a little more readable to dereference the model object.

Code: Select all

 public function get_more_news($menu_item_id, $total)
 {
        $news_model = $this->{$this->model};		// get Model

        $this->data['all_items']= $news_model->get_items_all($menu_item_id, $this->lang_id);
        $this->data['total'] = $total + $this->items_per_page;
        $current_page = $this->input->get('page') ? (($this->input->get('page') - 1) * $this->items_per_page) - 1 : 0;    
        $this->data['items'] = $news_model->get_items($menu_item_id, $this->lang_id, $this->data['total'], $current_page);
        if ($all_items > $this->data['items'])
        {
            $this->data['hide_button'] = 1;
        }    
        else
        {
            $this->data['hide_button'] = 0;
        }
        
        $this->data['use_thumbnails'] = $news_model->get_variables('use_thumbnails');
        $this->data['thumbnail_prefix'] = $news_model->get_variables('thumbnail_prefix');
        $this->data['directory'] = $news_model->get_variables('directory');
        $this->data['meta_data'] = $this->welcome_model->get_item_meta_data($menu_item_id, $this->lang_id, true);
        $this->load->view($this->theme . '/menu_modules/' . $this->module . '/loader', $this->data);
    }
 
Even better to move all these data assignments into the View or just get the variables directly from the Models in the View:

Code: Select all

 public function get_more_news($menu_item_id, $total)
 {
       $this->load->view($this->theme . '/menu_modules/' . $this->module . '/loader', $this->{$this->model}, $this->welcome_model);
    }