Welcome Guest, Not a member yet? Register   Sign In
Regarding pagination
#1

[eluser]Tanax[/eluser]
I'm really trying to figure out how it works!

I've got so far so it create the links and I can echo them out. The url works aswell, however it actually doesn't really write the pagenumber, rather then result.

http://localhost/product/category/9/60

So we're trying to create a pagination for the category function in the product class.
Basicly, this lists all the products inside the specified category, which in this case is 9. Then it's the pagination thingy added. I've specified that I want 15 results per page, so this is the page 5.
Page 1 = 0
Page 2 = 15
Page 3 = 30
Page 4 = 45
Page 5 = 60

How can I make it so it's pagenumbers instead of the resultnumbers?

That's just a minor detail.
My REAL concern is that, how do I limit the results per page, I mean.. ON THE PAGE?
Code:
$this->data['products'] = $this->Category->getProductsInCategory($id);
$this->load->view( 'products/categorylist', $this->data );

$this->load->library('pagination');
$pConfig['base_url'] = '/product/category/' . $id . '/';
$pConfig['total_rows'] = count($this->data['products']);
$pConfig['per_page'] = '15';
$pConfig['num_links'] = 5;
        
$this->pagination->initialize($pConfig);
$this->data['page'] = $this->pagination;

and the view:
Code:
<?=$page->create_links()?>
                
                <div id="products">
                
                &lt;?php if($products != ''): foreach($products as $key => $product): ?&gt;
                    
                    <div class="product&lt;?=$css[$key]?&gt;">
                    
                        <div id="product-title">&lt;?=$product->prod_name?&gt;</div>
                        
                        <div id="product-image">&lt;?php if($product->prod_frontimgid != ''): ?&gt;<img >prod_frontimgid?&gt;" width="150px" />&lt;?php endif; if($product->prod_frontimgid == ''): ?&gt;NO IMAGE&lt;?php endif; ?&gt;</div>

                        <div id="product-price-excl">&lt;?php if(!empty($product->prod_price_excl)): ?&gt;Pris: &lt;?php echo $product->prod_price_excl; endif; ?&gt;</div>
                        <div id="product-price-incl">&lt;?php if(!empty($product->prod_price_incl)): ?&gt;Pris inkl moms: &lt;?php echo $product->prod_price_incl; endif; ?&gt;</div>
                        
                        <div id="read-more">&lt;?=anchor('product/id/' . $product->prod_id, 'Läs mer')?&gt;</div>
                    
                    </div>
                    
                    &lt;?php
                    endforeach; endif;
?&gt;

Right now, it foreaches the $products, which is the total number of products IN THAT CATEGORY. But how do I make it to foreach the number of products in that category ON THAT PAGE ?

Thanks in advance, and sorry for huge segment of code ;<

EDIT: The view code got pretty messed up, I dno why. It left out some " chars and I dunno what else. But I think you still can get a pretty good overview of the code.
#2

[eluser]theprodigy[/eluser]
in the url http://localhost/product/category/9/60, 60 is the offset. That is the record in the database that MySQL starts with before selecting from that record forward.

in your product controller, category function, it looks like you have two parameters being passed in (category_id, offset). You need to pass both of the parameters, as well as your limit per page, to your model to be used in the query.

CI's pagination class doesn't do anything for you other than format the links. It doesn't restrict the query, pull records out of a full query or anything like that. It's strictly a "format" class. It creates and formats the links for you.

Hope this helps.
#3

[eluser]Tanax[/eluser]
Alright! Thanks for that!

It works now to step through the pages.
Code:
public function category($id, $offset = NULL) {
$this->data['all_products'] = $this->Category->getProductsInCategory($id);
        
$this->load->library('pagination');
$pConfig['total_rows'] = count($this->data['all_products']);
$pConfig['per_page'] = '15';
$pConfig['num_links'] = 5;
$pConfig['base_url'] = '/product/category/' . $id . '/';
$this->pagination->initialize($pConfig);
$this->data['page'] = $this->pagination;
        
$this->data['products'] = $this->Category->getProductsInCategory($id, $pConfig['per_page'], $offset);

However!
1 small problem though.
The "current" page doesn't seem to be updating.
When I first go to the category, the page 1 is bold. When I click page 2, the results are updated and it shows the page 2 of the results, however, in the links, the page 1 link is still bold, and page 2 link is not :S
#4

[eluser]theprodigy[/eluser]
can you please show your controller, model, and view? I wanna take a look at all your code so I can see what's going on.

Thanks
#5

[eluser]Tanax[/eluser]
Alright. Bare with me then, long code segments comming up!

Product controller:
Code:
class Product extends Controller
{
    
    public $data = array();
    
    public function __construct()
    {
        
        parent::Controller();
        
        $this->data['site'] = $this->Site_config->getConfig();
        $this->data['site'] = $this->data['site'][0];
        $this->data['categories'] = $this->Site_config->getCategories();
        
        $this->load->view( 'header', $this->data );
        
    }
    
    public function category($id, $offset = NULL)
    {
        
        $this->data['category'] = $this->Category->getCategoryInfo($id);
        $this->data['all_products'] = $this->Category->getProductsInCategory($id);
        
        $this->load->library('pagination');
        $pConfig['total_rows'] = count($this->data['all_products']);
        $pConfig['per_page'] = '15';
        $pConfig['num_links'] = 5;
        $pConfig['base_url'] = '/product/category/' . $id . '/';
        $this->pagination->initialize($pConfig);
        $this->data['page'] = $this->pagination;
        
        $this->data['products'] = $this->Category->getProductsInCategory($id, $pConfig['per_page'], $offset);
        
        if(is_array($this->data['products']))
        {
            
            $i = 1;
            for($x = 0; $x < count($this->data['products']); $x++)
            {
                        
                if($i == 5 || $this->data['products'][$x] == end($this->data['products']))
                {
                            
                    if($i == 1) $margin = 'one';
                    elseif($i == 2) $margin = 'two';
                    elseif($i == 3) $margin = 'three';
                    elseif($i == 4) $margin = 'four';
                    elseif($i == 5) { $margin = 'five'; $i = 1; }
                    
                    $this->data['css'][$x] = ' last ' . $margin;
                    
                }
                    
                else
                {
                        
                    $i++;
                    
                    $this->data['css'][$x] = '';
                    
                }
                
            }
            
        }
        
        $this->load->view( 'products/categorylist', $this->data );
        $this->load->view( 'footer' );
        
    }
    
    public function id($id)
    {
        
        $this->data['product'] = $this->Category->getProductWithId($id);
        
        $this->load->view( 'products/product', $this->data );
        $this->load->view( 'footer' );
        
    }
    
}

Category model:
Code:
class Category extends Model
{
    
    
    public function __construct()
    {
        
        parent::Model();
        
    }
    
    public function getCategoryInfo($id)
    {
        
        $this->db->from('categories')->where('cat_visible', 1)->where('cat_id', $id)->limit(1);
        $query = $this->db->get();
        
        if($query->num_rows() > 0)
        {
            
            foreach($query->result() as $row)
            {
                
                return $row;
                
            }
            
        }
        
        return false;
        
    }
    
    public function getProductsInCategory($id, $limit = NULL, $offset = NULL)
    {
        
        $this->db->from('products')->where('prod_visible', 1)->where('prod_categoryid', $id)->order_by('prod_order', 'ASC');
        if($offset != NULL)
        {
            
            $this->db->limit($limit, $offset);
            
        }
        $query = $this->db->get();
        
        if($query->num_rows() > 0) {
            
            return $query->result();
            
        }
        
        return false;
        
    }
    
    public function getProductWithId($id)
    {
        
        $this->db->from('products')->where('prod_visible', 1)->where('prod_id', $id);
        $query = $this->db->get();
        
        if($query->num_rows() > 0)
        {
            
            return $query->result();
            
        }
        
        return false;
        
    }
    
}
#6

[eluser]Tanax[/eluser]
Exceeding limit, so new post here with the rest!


categorylist view:
Code:
<div id="cat-title">&lt;?=$category->cat_name?&gt;</div>

                <div id="cat-desc">&lt;?=$category->cat_desc?&gt;</div>
                
                &lt;?=$page->create_links()?&gt;
                
                <div id="products">
                
                &lt;?php if($products != ''): foreach($products as $key => $product): ?&gt;
                    
                    <div class="product&lt;?=$css[$key]?&gt;">
                    
                        <div id="product-title">&lt;?=$product->prod_name?&gt;</div>
                        
                        <div id="product-image">&lt;?php if($product->prod_frontimgid != ''): ?&gt;<img >prod_frontimgid?&gt;" width="150px" />&lt;?php endif; if($product->prod_frontimgid == ''): ?&gt;NO IMAGE&lt;?php endif; ?&gt;</div>

                        <div id="product-price-excl">&lt;?php if(!empty($product->prod_price_excl)): ?&gt;Pris: &lt;?php echo $product->prod_price_excl; endif; ?&gt;</div>
                        <div id="product-price-incl">&lt;?php if(!empty($product->prod_price_incl)): ?&gt;Pris inkl moms: &lt;?php echo $product->prod_price_incl; endif; ?&gt;</div>
                        
                        <div id="read-more">&lt;?=anchor('product/id/' . $product->prod_id, 'Läs mer')?&gt;</div>
                    
                    </div>
                    
                    &lt;?php
                    endforeach; endif;
                    if($products == ''):
                    ?&gt;
                    
                    <p id="no-products">Ojdå.. För tillfället kan vi inte hitta några produkter i denna kategori. Antingen så finns det inga
                    produkter här än, eller så har något blivit fel när vi hämtade produkterna!</p>
                    
                    &lt;?php endif; ?&gt;
                
                </div>

EDIT: And again.. the view code gets.. weird. It misses out " on places when I copy-paste. Mainly it's just HTML code that's get left out(I think! - I hope!).
Hopefully once again, you will be able to read it through anyways!
#7

[eluser]theprodigy[/eluser]
well, the first thing I see is your getProductsInCategory($id) is returning a $query->result(), but your trying to treat it as an array. Try changing that function to return $query->result_array(). It's possible that the pagination isn't getting the proper number.

Other than that, I can't really see anything at first glance. Do you have the site live anywhere that I can see how it's acting?

I'll take a little more look at it later on, but let me know how that change up above changes your output.
#8

[eluser]Tanax[/eluser]
[quote author="theprodigy" date="1238133929"]well, the first thing I see is your getProductsInCategory($id) is returning a $query->result(), but your trying to treat it as an array. Try changing that function to return $query->result_array(). It's possible that the pagination isn't getting the proper number.

Other than that, I can't really see anything at first glance. Do you have the site live anywhere that I can see how it's acting?

I'll take a little more look at it later on, but let me know how that change up above changes your output.[/quote]

I'm not treating it as an array. I'm putting the results inside an array, to be able to pass it to the view.
Code:
$this->data['products'] = $query->result(); //basicly
$this->load->view('view', $this->data);

Then inside the view I'm treating it as an object result:
Code:
&lt;?=$products->prod_name?&gt;

And no, I can't really find any other errors either..
I'll try to get a demo up, but see if you can see any other errors meanwhile!

Thanks again!
#9

[eluser]TheFuzzy0ne[/eluser]
I don't think there's any simple solution really other than to extend the pagination library, or write your own.
#10

[eluser]theprodigy[/eluser]
the code
Code:
$this->data['products'] = $this->Category->getProductsInCategory($id, $pConfig['per_page'], $offset);
        
if(is_array($this->data['products']))

is what I was talking about when I said you were treating it like an array. You have a large block of code being based on if $this->data['products'] is an array:

Code:
if(is_array($this->data['products']))
        {
            
            $i = 1;
            for($x = 0; $x < count($this->data['products']); $x++)
            {
                        
                if($i == 5 || $this->data['products'][$x] == end($this->data['products']))
                {
                            
                    if($i == 1) $margin = 'one';
                    elseif($i == 2) $margin = 'two';
                    elseif($i == 3) $margin = 'three';
                    elseif($i == 4) $margin = 'four';
                    elseif($i == 5) { $margin = 'five'; $i = 1; }
                    
                    $this->data['css'][$x] = ' last ' . $margin;
                    
                }
                    
                else
                {
                        
                    $i++;
                    
                    $this->data['css'][$x] = '';
                    
                }
                
            }
            
        }




Theme © iAndrew 2016 - Forum software by © MyBB