Jump to content

CI - Pagination


ReeceSayer

Recommended Posts

Hey, I'm trying to combine two of the tutorials i've been using and have gotten a little stuck.

 

I create an online cart with pagination links however the links are sending me to a 404 error page.

 

I've been playing around with it to try and get it to work but can't seem to see my problem so there might be things commented out.

 

My default url is (http://example.com/tutorial/shop), the pagination is trying to reach http://example.com/tutorial/shop/4 (this depends on how many results i show on the first page)

 

Controller (shop.php):

<?php
class Shop extends Controller {

//included for pagination
    public function __construct() {
        parent:: __construct();
        $this->load->helper("url");
        $this->load->model("products_model");
        $this->load->library("pagination");
    }

function index() {
	//star pagination
	$config = array();
        $config["base_url"] = base_url() . "shop";
        $config["total_rows"] = $this->products_model->record_count();
        $config["per_page"] = 4;
        $config["uri_segment"] = 3;

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["products"] = $this->products_model->
            fetch_products($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();
	//end of pagination

	$this->load->model('Products_model');

	//$data['products'] = $this->Products_model->get_all();

	$this->load->view('products', $data);


}

function add() {

	$this->load->model('Products_model');

	$product = $this->Products_model->get($this->input->post('id'));

	$insert = array(
		'id' => $this->input->post('id'),
		'qty' => 1,
		'price' => $product->price,
		'name' => $product->name
	);
	if ($product->option_name) {
		$insert['options'] = array(
			$product->option_name => $product->option_values[$this->input->post($product->option_name)]
		);
	}

	$this->cart->insert($insert);

	redirect('shop');

}

function remove($rowid) {

	$this->cart->update(array(
		'rowid' => $rowid,
		'qty' => 0
	));

	redirect('shop');

}

}

 

Model (product_model):

<?php
class Products_model extends Model {

    public function __construct() {
        parent::__construct();
    }


function fetch_products($limit, $start) {
//function get_all($limit, $start) {
    $this->db->limit($limit, $start);
	$results = $this->db->get('products')->result();

	foreach ($results as &$result) {

		if ($result->option_values) {
			$result->option_values = explode(',',$result->option_values);
		}

	}

	return $results;

}

function get($id) {

	$results = $this->db->get_where('products', array('id' => $id))->result();
	$result = $results[0];

	if ($result->option_values) {
		$result->option_values = explode(',',$result->option_values);
	}

	return $result;
}

//attempt at implementing pagination class
public function record_count() {
        return $this->db->count_all("products");
    }
/*
public function fetch_products($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get('products');
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $result[] = $row;

          }
            return $result;
        }
        return false;
   }
*/
}

 

Any help or points in the right direction would be awesome!

 

Thanks

Link to comment
Share on other sites

Just incase anyone's having trouble with this I did get it working on my host (HostGator) by changing the base url in the config.

 

Rather than http://example.com/tutorial/ which i initially had, i changed it to http://example.com/tutorial/shop

 

This works as expected but with a strange side effect of the URL being http://example.com/tutorial/shop/shop/4 when going through other pages.

 

Will mark as solved.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.