Pagination possible in this case? - El Forum - 06-30-2012
[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>
<header>
<h2 class="inline"><?php echo $profile['email_address'];?> (<?php echo $profile['age'];?>)</h2>
</header>
<a href="<?php echo base_url() . 'images/original/' . $profile['photo_id'];?>" target="_blank"><img id="profile_photo" src="<?php echo base_url() . 'images/thumbs/' . $profile['photo_id'];?>" alt="profile photo"/></a>
<ul>
<li>Age <?php echo $profile['age'];?></li>
<li>Profession <?php echo $profile['profession'];?></li>
<li>Goal <?php echo $profile['goal'];?></li>
</ul>
<p><?php echo $profile['presentation'];?></p>
</article>
<div id="board">
<ul>
<?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;
}
}
?>
</ul>
<!-- insert post-->
<?php
$url = base_url() . substr($_SERVER['REDIRECT_QUERY_STRING'], 1);
echo form_open($url);?>
<p>
<?php echo form_label('', 'message');?>
<?php echo form_textarea('message', '', 'id="message"' );?>
</p>
<p>
<?php echo form_submit('submit', 'Post'); ?>
</p>
<div class="errors"><?php echo validation_errors(); ?></div>
<?php echo form_label();?>
<?php echo form_close();?>
</div><!-- end board -->
</div> <!-- end body -->
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
Pagination possible in this case? - El Forum - 06-30-2012
[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 . '/');
Pagination possible in this case? - El Forum - 07-01-2012
[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!
Pagination possible in this case? - El Forum - 07-07-2012
[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
Pagination possible in this case? - El Forum - 07-10-2012
[eluser]simonCI[/eluser]
Any idea?
|