Welcome Guest, Not a member yet? Register   Sign In
Pagination
#11

[eluser]Fero[/eluser]
but then, when I get to www.fotoramik.sk/blog/kategoria/clanky I get following error:

Code:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1,3' at line 1
SELECT id,odkaz,title,picture,text,date FROM articles WHERE kategoria='clanky' ORDER BY id DESC LIMIT -1,3

which means that I should hardcode /1 variable as the last segment, right?
#12

[eluser]TheFuzzy0ne[/eluser]
Not necessarily. Just ensure that if the last segment is not set, that the default for the variable you pass to the model is 1.
Code:
$start_result = $this->uri->segment(3);
$start_result = ( ! is_numeric($start_result) || $start_result < 1) ? 1 : $start_result;

Something like that should do it, and prevent your app from breaking if the URI doesn't contain a page number in it (if it's been tampered with by the user).
#13

[eluser]Fero[/eluser]
Hmm I still have some weird results, as the test article doesn't show even after this

+ when I click the 2, the thirt article shows ONLY and I can't comeback to number 1 as if the pagination lib. was broken or something..


And, is this OK? (limit)
Code:
$data['query']=$this->db->query("SELECT id,odkaz,title,picture,text,date FROM articles WHERE kategoria='".$page."' ORDER BY id DESC LIMIT ".$start_result.",3");
#14

[eluser]TheFuzzy0ne[/eluser]
OK, please repost your controller method and your model method.
#15

[eluser]Fero[/eluser]
Controller (blog.php):
Code:
function kategoria()
  {
      $this->load->model('article','',TRUE);
      $this->article->header();
      $this->article->menu();
        $this->article->body($this->uri->segment(3),"kategoria");
        $this->article->footer();
  }


Model article.php:

Code:
function body($page,$category) //page-WHERE klauzula ; kategoria - category pointer
  {
  $data['links']='';    
    
    
    If (($page!="") AND ($category=="kategoria")):
    
    // set pagination parameters
    
    $config['base_url']=site_url('blog/kategoria/'.$this->uri->segment(3)); //don't know what to put here
    $this->db->where('kategoria',$page); //counts only articles of category specified in $page var
    $config['total_rows']=$this->db->count_all_results('articles');
    $config['per_page']='3';
    $config['full_tag_open']='<div id="paginglinks">';
    $config['full_tag_close']='</div>';
    $this->pagination->initialize($config);

// create pagination links

$data['links']=$this->pagination->create_links();

$start_result = $this->uri->segment(4);
$start_result = ( ! is_numeric($start_result) || $start_result < 1) ? 1 : $start_result;


     $data['query']=$this->db->query("SELECT id,odkaz,title,picture,text,date FROM articles WHERE kategoria='".$page."' ORDER BY id DESC LIMIT ".$start_result.",3");
      endif;
    
    
    $this->load->view('body',$data);
  }
#16

[eluser]TheFuzzy0ne[/eluser]
I'm quite confused by your code. Please give me a little while to study it and figure out what's what.
#17

[eluser]TheFuzzy0ne[/eluser]
Sorry, but I am still totally confused by your code. There is a lot you can do to make things more simple for yourself. I'm going to attempt a rewrite for you, but I can't make any promises as I don't fully understand what's happening in your code. However, I would like to point out that whilst you're free to do as you want with your model, the model in your case is acting as a controller which may make things difficult for you in the future. A model should ideally just return data. It should not load views.
#18

[eluser]Fero[/eluser]
I see, anyway, I have edited my last (codings) post so I deleted everything you wouldn't need.

Thank you Fuzzy for trying to help me
#19

[eluser]TheFuzzy0ne[/eluser]
OK, here are my two proposed replacement functions:
Code:
&lt;?php
function kategoria($category="", $start_result=FALSE)
{
    $this->load->model('article','',TRUE);
    $this->article->header();
    $this->article->menu();
    $this->article->body($category, $start_result);
    $this->article->footer();
}

function body($category="", $start_result=FALSE) //page-WHERE klauzula ; kategoria - ide o vypis vsetkych clankov v kategorii?
  {
    // Sanitize the start_result
    $start_result = ( ! is_numeric($start_result) || $start_result <= 1) ? 0 : $start_result - 1;
    
    // Set the fields to select.
    $this->db->select('id, odkaz, title, picture, text, date');

    // If $category is a number, the it must be the start result
    if (is_numeric($category))
    {
        $start_result = $category; // Set the correct start result.
        $category = ""; // Set category to "".
    }

    // If we have a category, set the where clause
    if ($category)
    {
        $this->db->where('kategoria', $category);
    }
    
    // Set the order
    $this->db->order_by('date', 'desc');
    
    // If a start result has been specified, start from there, otherwise start from the beginning.
    $this->db->limit($start_result, 3);
    
    // Get the total results found
    $total_results = $this->db->count_all_results('articles');
    
    // set pagination parameters
    $this->pagination->initialize(
        array(
            'base_url'          => site_url('blog/kategoria/'.$this->uri->segment(3)),
            'total_rows'        => $total_results,
            'per_page'          => '3',
            'full_tag_open'     => '<div id="paginglinks">',
            'full_tag_close'    => '</div>'
        ));
        
    $data['query']       = $this->db->get('articles');        
    $data['links']       = $this->pagination->create_links();
    
    //Logo
    $data['logo']        = "Fotoramik.sk";
    $data['logo_url']    = "http://www.fotoramik.sk";
    $data['popis_logo']  = "Všetko o digitálnych fotorámikoch";

    //Reklama
    $data['ad_300']      = '';
    
    $this->load->view('body', $data);
}

It may need a little bit of tweaking, but hopefully you can see that it's a bit easier to follow. The Database query is built as we progress down the function. I hope this helps, and this is at least close to what you were after.

EDIT: The code needed changing to order articles by date, rather than id, which I've now done.

EDIT2: Also, it looks like it will break when you click on the pagination if there is no category specified. I've added some code which should deal with this.
#20

[eluser]Fero[/eluser]
ok read your edits, goin to try




Theme © iAndrew 2016 - Forum software by © MyBB