Welcome Guest, Not a member yet? Register   Sign In
database record matching pagination pages
#1

[eluser]zeromechanic[/eluser]
Hello all,

Im a little stuck on the next thing.

I want to create a news module for CI.

All is working but not what i want with pagination.

eg.

a user clicks on a menu item say ..../news/4
this is row 4 of my table in dbase.
and goes to the page displaying the news.

What i want is the following, when the user comes to this page, there has to be pagination directing to older an newer messages.

How can i get CI to match this /news/4 with the corresponding page that the pagination class creates.
eg news/4 is page 3 in the pagination.

viewing the news item is working, and my pagination is working.
sow how to combine this two

or is there an extention to use dbase rows as links??

can't find any on google (prob wrong seach words)

thnx zeromechanic
#2

[eluser]CI_adis[/eluser]
Hi zeromechanic,

did you check this tut??

http://net.tutsplus.com/articles/news/co...agination/

regards,
Adis
#3

[eluser]zeromechanic[/eluser]
Yes checked it out.
As most of the nettut tutorials (i think they are great)
in a few day got the basics of codeigniter, which i started to "love".

I already solved the issue.
It is a few weeks ago, so i have to look in it to post what i did.
(order for script is pushed to end of march sow didn't work on it for a while)

thnx for your reply
#4

[eluser]Wayne Smallman[/eluser]
[quote author="CI_adis" date="1293503867"]Hi zeromechanic,

did you check NetTut?[/quote]
All versions are either jamming up or coming up with 404 errors. Do you have another example?
#5

[eluser]zeromechanic[/eluser]
I checked it.

I did an arraysearch to get the uri_id's matching

This is in my controller :
(i use HMVC modulair extentions for my codeigniter)
Al basic stuff, as also explained in the tut
Code:
function index() {
        $this->load->library('pagination');
        $config['base_url'] = base_url().'/nieuws/index/';
        $config['total_rows'] = $this->db->get('addon_nieuws')->num_rows();
        $config['per_page'] = '2';
        $config['num_links'] = '5';
        $this->pagination->initialize($config);
        $data = array();
        $this->load->helper('date');
        $this->load->helper('text');
        $this->load->model('Nieuws_model', 'nieuws');
        $data['nieuws'] = $this->nieuws->getPage($config['per_page'], $this->uri->segment(3));
        $data['link'] = $this->nieuws->getLinks();
        
        $this->load->view('index', $data);
    }

function lees() {
        
       $this->load->model('Nieuws_model', 'nieuws');
        $this->load->library('pagination');      
        $config['base_url'] = base_url()."/nieuws/lees/";
        $config['total_rows'] = $this->db->get('addon_nieuws')->num_rows();
        $config['uri_segment'] = 3;
        $config['per_page'] = '1';
        $config['num_links'] = '5';
        $config['next_link'] = 'ouder>';
        $config['prev_link'] = '<nieuwer';
        $this->pagination->initialize($config);
        $this->load->helper('date');
        $this->load->helper('text');
        
        
         $data['bericht'] = $this->nieuws->getData($this->uri->segment(3));
         $this->load->view('bericht', $data);

    }

The problem in my case was that the pagination(anchors) in the index() was not matching the id's in de lees() function.
the pagination in "lees" was messedup, wrong pages eg. wrong pagination, stayed on same pagenumer etc

What i did was create an array ($data['link'])
as a basic database request from dbase:
Code:
function getLinks() {
        $this->db->order_by('datum', 'desc');
        $q = $this->db->get('addon_nieuws');
       if($q->num_rows() > 0) {
           $data = $q->result_array();
           return $data;
           }


    }
ran this outcome through an arraysearch function in my model:
Code:
function _arraySearch($needle, $haystack) {
         if (empty($needle) || empty($haystack)) {
            return false;
        }

        foreach ($haystack as $key => $value) {
            $exists = 0;
            foreach ($needle as $nkey => $nvalue) {
                if (!empty($value[$nkey]) && $value[$nkey] == $nvalue) {
                    $exists = 1;
                } else {
                    $exists = 0;
                }
            }
            if ($exists) return $key;
        }

        return false;
    }

And did this to my view(it is not supposed to be in an viewfile....... for testing)
to get the "read more" anchor to match with the pagination in the read view (lees())
So it wil open the newsmessage and gives the pagination the right page.

first page has no uri id in the pagination.
therefore the $key is set to empty ($key = ''Wink


Code:
<?php

$this->load->view('header');
$datestr = "%d-%m-%Y";
echo $this->pagination->create_links();
?>
<?php foreach ($nieuws as $r) : ?>
<?php
$key = $this->nieuws->_arraySearch($r, $link);
if ($key == '0') $key = '';
?>
<h2>&lt;?php echo $r->titel; ?&gt;</h2>
&lt;?php echo word_limiter($r->text, 40); ?&gt;<br />
&lt;?php echo anchor("nieuws/lees/$key", 'Lees verder'); ?&gt;<br />
<sup>geplaatst op : &lt;?php echo mdate($datestr, $r->datum); ?&gt;</sup>
<br/>
&lt;?php


?&gt;
&lt;?php endforeach; ?&gt;
&lt;?php
$this->load->view('footer');

in the model getdata also , if no uri id is present, set to 0

Code:
function getData() {
      
        
       $item = $this->uri->segment(3);
       $this->db->order_by('datum', 'desc');
        $q = $this->db->get('addon_nieuws');
       if($q->num_rows() > 0) {
           //foreach($q->result_array() as $k=>$row) {
           //   $data[$k] = $row;

           $data = $q->result_array();
           if (empty($item)) {
               return $data['0'];
           }
           else {    
           return $data[$item];
           }
    
        
    }
    }

this is working for me.




Theme © iAndrew 2016 - Forum software by © MyBB