Welcome Guest, Not a member yet? Register   Sign In
how to i change variable in view from $data on controller with button
#1

Help me master.

in my controller
Code:
 public function trending() {
   $data = array(
      'all'      => $this->library_trending->all(),
     'movie'     => $this->library_trending->movie(),
      'tv'     => $this->library_trending->tv(),
   );

   $this->load->view('templates/header', $data);
   $this->load->view('content/trending', $data);
   $this->load->view('templates/footer');

 }

in my view
Code:
   <button type="button" name="all"></button>
    <button type="button" name="movie"></button>
    <button type="button" name="tv"></button>

<? foreach ($all->results as $v){ ?>
     //code
<? } ?>

how to i change variable $all to $movie or $tv with button?
Reply
#2

Quote:how to i change variable $all to $movie or $tv with button?

It depends. Is it OK if the page is being reloaded? Then, change your controller function:
PHP Code:
public function trending($category="all") {
 
 
   
switch ($category) {
 
    case "tv" :
 
     $data['records'] = $this->library_trending->tv(); 
 
     break;
 
   case "movie" :
 
      $data['records'] = $this->library_trending->movie(); 
 
      break;
 
   default:
 
       $data['records'] = $this->library_trending->all();       
   
}
 
  $this->load->view('templates/header'$data);
 
  $this->load->view('content/trending'$data);
 
  $this->load->view('templates/footer');

 } 

Make sure the buttons in your view call the correct url, like: controllername/trending/tv

If you don't want a page refresh, then output all data to your view, but hide the "movie" and "tv" tables initially. Use jQuery to toggle between hidden and visible if you click one of the buttons.
Reply
#3

(10-04-2018, 05:58 AM)Wouter60 Wrote:
Quote:how to i change variable $all to $movie or $tv with button?

It depends. Is it OK if the page is being reloaded? Then, change your controller function:
PHP Code:
public function trending($category="all") {
 
 
   
switch ($category) {
 
    case "tv" :
 
     $data['records'] = $this->library_trending->tv(); 
 
     break;
 
   case "movie" :
 
      $data['records'] = $this->library_trending->movie(); 
 
      break;
 
   default:
 
       $data['records'] = $this->library_trending->all();       
   
}
 
  $this->load->view('templates/header'$data);
 
  $this->load->view('content/trending'$data);
 
  $this->load->view('templates/footer');

 } 

Make sure the buttons in your view call the correct url, like: controllername/trending/tv

If you don't want a page refresh, then output all data to your view, but hide the "movie" and "tv" tables initially. Use jQuery to toggle between hidden and visible if you click one of the buttons.

If i want to use jQuery ajax. what you have referensi for that? or can you give me example?
Reply
#4

No, you don't need AJAX.

Code:
<button id="show_all">All</button>
<button id="show_movie">Movie</button>
<button id="show_tv">TV</button>

<div id="all">
This div contains the records from your "all" category.
It is visible when you load the page
</div>

<div id="movie" style="display: none;">
This div contains the records from your "movie" category
It is hidden when you load the page
</div>

<div id="tv" style="display: none;">
This div contains the records from your "movie" category"
This one is also hidden when you load the page
</div>

<script>
$(document).ready(function(){
  $('#show_all').click(function(){
     $('#all').show();
     $('#movie').hide();
     $('#tv').hide();
  });

  $('#show_movie').click(function(){
     $('#all').hide();
     $('#movie').show();
     $('#tv').hide();
  });

  $('#show_tv').click(function(){
     $('#all').hide();
     $('#movie').hide();
     $('#tv').show();
  });
});
</script>
Reply
#5

(10-04-2018, 08:40 AM)Wouter60 Wrote: No, you don't need AJAX.

Code:
<button id="show_all">All</button>
<button id="show_movie">Movie</button>
<button id="show_tv">TV</button>

<div id="all">
This div contains the records from your "all" category.
It is visible when you load the page
</div>

<div id="movie" style="display: none;">
This div contains the records from your "movie" category
It is hidden when you load the page
</div>

<div id="tv" style="display: none;">
This div contains the records from your "movie" category"
This one is also hidden when you load the page
</div>

<script>
$(document).ready(function(){
  $('#show_all').click(function(){
     $('#all').show();
     $('#movie').hide();
     $('#tv').hide();
  });

  $('#show_movie').click(function(){
     $('#all').hide();
     $('#movie').show();
     $('#tv').hide();
  });

  $('#show_tv').click(function(){
     $('#all').hide();
     $('#movie').hide();
     $('#tv').show();
  });
});
</script>

how to adding active button in this code?
Reply
#6

The html button element doesn't have an "active" attribute. I guess you want to add a css class named "active" right?
Example:

Code:
 $('#show_all').click(function(){
    $('#all').show();
    $('#movie').hide();
    $('#tv').hide();
    $('#show_all').addClass('active');
    $('#show_movie').removeClass('active');
    $('#show_tv').removeClass('active');
 });
Reply




Theme © iAndrew 2016 - Forum software by © MyBB