Welcome Guest, Not a member yet? Register   Sign In
Pagination possible in this case?
#1

[eluser]simonCI[/eluser]
Hello. I'm about to try to make a simple community, I'm a newbie so bear with me.

This is my current code:

My controller
Code:
public function display_profile($id)
{
  $this->load->model('profile_model');
  $data['profile'] = $this->profile_model->get_profile_by_id($id);
  $data['board'] = $this->profile_model->get_board($id);
  
  $this->load->library('form_validation');
  $this->form_validation->set_rules('message', 'Message', 'required|length[3]');

  if( $this->form_validation->run() !== false)
  {
   $data['message'] = $this->input->post('message');
   $this->load->model('board_model');
   $this->board_model->insert_post($id, $data['message']);
  
   $this->load->view('common/header.php');
   $this->load->view('common/nav.php');
   $this->load->view('display_profile', $data);
   $this->load->view('common/footer.php');
  }
  else
  {
   $this->load->view('common/header.php');
   $this->load->view('common/nav.php');
   $this->load->view('display_profile', $data);
   $this->load->view('common/footer.php');
  }
}

Model:
Code:
class Board_model extends CI_model
{
//insert post
function insert_post($id, $message){
  
  $q = $this->db->get_where('users', array('id' => $id));
  if ($q->num_rows() > 0)
  {
     $data['account'] = $q->row_array();
  }

  $data = array(
     'author' => $email = $_SESSION['username'],
     'board_owner' => $data['account']['email_address'],
     'message' => $message
  );

  $this->db->insert('board_posts', $data);
  
}

}

view
Code:
<div id="body">
  <article>
   &lt;header&gt;
    <h2 class="inline">&lt;?php echo $profile['email_address'];?&gt; (&lt;?php echo $profile['age'];?&gt;)</h2>
   &lt;/header&gt;
   <a href="&lt;?php echo base_url() . 'images/original/' . $profile['photo_id'];?&gt;" target="_blank"><img id="profile_photo" src="&lt;?php echo base_url() . 'images/thumbs/' . $profile['photo_id'];?&gt;" alt="profile photo"/></a>
    <ul>
     <li>Age &lt;?php echo $profile['age'];?&gt;</li>
     <li>Profession &lt;?php echo $profile['profession'];?&gt;</li>
     <li>Goal &lt;?php echo $profile['goal'];?&gt;</li>
    </ul>
    <p>&lt;?php echo $profile['presentation'];?&gt;</p>
  </article>
  <div id="board">
   <ul>
   &lt;?php
   if ($board)
   {
    //if posts exists
    $counter = 1;
    foreach($board as $row)
    {
     echo '<li>';
     echo $row->date . ' - ';
     echo $row->author . br(1);
     echo $row->message . br(2);
     echo '</li>';
     $counter +=1;
     if ($counter == 10) exit;
    }
   }
   ?&gt;
   </ul>
  
   &lt;!-- insert post--&gt;
   &lt;?php
   $url = base_url() . substr($_SERVER['REDIRECT_QUERY_STRING'], 1);
   echo form_open($url);?&gt;
   <p>
    &lt;?php echo form_label('', 'message');?&gt;
    &lt;?php echo form_textarea('message', '', 'id="message"' );?&gt;
   </p>  
   <p>
    &lt;?php echo form_submit('submit', 'Post'); ?&gt;
   </p>
   <div class="errors">&lt;?php echo validation_errors(); ?&gt;</div>
   &lt;?php echo form_label();?&gt;
   &lt;?php echo form_close();?&gt;
  
  
  </div>&lt;!-- end board --&gt;
</div> &lt;!-- end body --&gt;

So the problem I encountered is that I already have an argument in the controller, called id, which decide which profile to display. If I now want to implement CI pagination class in the board, to display about 10 posts a time. How do I solve the controller with two arguments and the URL?

Thanks in advance /Simon
#2

[eluser]pickupman[/eluser]
You can have as many segments in a uri as you would like. With the exception of special routes, any segment greater than 2 will be passed as arguments to your controller/method. In your case the first would be your controller name, second method, third profile id, and forth (if shown) pagination offset. The [url="http://ellislab.com/codeigniter/user-guide/libraries/pagination.html"]pagination library[/url] allows the page offset to be in any location in the uri. You just have to set it, when it is not the 3rd segment.

Code:
$config['uri_segment'] = 4;
$config['base_url'] = site_url('user/display_profile/' . $id . '/');
#3

[eluser]simonCI[/eluser]
[quote author="pickupman" date="1341110296"]You can have as many segments in a uri as you would like. With the exception of special routes, any segment greater than 2 will be passed as arguments to your controller/method. In your case the first would be your controller name, second method, third profile id, and forth (if shown) pagination offset. The [url="http://ellislab.com/codeigniter/user-guide/libraries/pagination.html"]pagination library[/url] allows the page offset to be in any location in the uri. You just have to set it, when it is not the 3rd segment.

Code:
$config['uri_segment'] = 4;
$config['base_url'] = site_url('user/display_profile/' . $id . '/');
[/quote]
Thanks pickupman, that worked great!
#4

[eluser]simonCI[/eluser]
But how about this situation. I'm about to create a public wall for members to write in. I'm trying to change the database model so that the newest post will appear in the top:

Code:
class Wall_model extends CI_model
{
//insert post
function insert_post($data){
  $query = $this->db->insert('wall', array(
   'message' => $data['message'],
   'author' =>$data['author']
   ));
  return TRUE;
}
//get total rows
function get_all_posts($per_page, $offset){
  $this->db->from('wall', $per_page, $offset);
  $this->db->order_by('date', 'desc');
  $query = $this->db->get();  
  
  if ($query->num_rows()>0)
  {
   return $query->result();
  }
  else
  {
   return false;
  }
}

.
.
.
}

here i used order_by function, but i don't think it works with pagination in my controller:

Code:
public function index()
{
  $this->load->library('form_validation');
  $this->load->model('wall_model');
  $this->form_validation->set_rules('message', 'Message', 'max_length[500]|required|min_length[3]|htmlspecialchars');

  
  if($this->form_validation->run()!== false)
  {
   $data['message'] = $this->input->post('message');
   $data['author'] = $_SESSION['username'];
  
   $this->wall_model->insert_post($data);
   redirect('wall');
  }
  else
  {
   $this->load->library('pagination');
   $config['uri_segment'] = 3;
   $config['base_url'] = site_url('wall/index/');
   $config['total_rows'] = $this->wall_model->get_total_rows();
   $config['per_page'] = 10;
   $config['full_tag_open'] = '<div id="pagination">';
   $config['full_tag_close'] = '</div>';
   $this->pagination->initialize($config);

   $data['board'] = $this->wall_model->get_all_posts($config['per_page'], $this->uri->segment(3));
   $this->load->view('common/header.php');
   $this->load->view('common/nav.php');
   $this->load->view('wall', $data);
   $this->load->view('common/footer.php');
  }
}

Now the pagination doesn't work quite right, all posts are published in offset 10, and offset 20 and so on.

So my question is, how do I make the posts to appear in right order with pagination?

/Simon
#5

[eluser]simonCI[/eluser]
Any idea?




Theme © iAndrew 2016 - Forum software by © MyBB