CodeIgniter Forums
pagination help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: pagination help (/showthread.php?tid=26657)

Pages: 1 2


pagination help - El Forum - 01-19-2010

[eluser]sasori[/eluser]
this is my controller function
Code:
function index()
    {
      $data['base'] = $this->config->item('base_url');
      $data['css'] = $this->config->item('css');
      $this->load->model('stock_model');
      $stocks = $this->stock_model->getStocks();

      $this->load->library('pagination');
      $config['base_url'] = 'http://myapp/welcome/stocks/';
      $config['total_rows'] = '20';
      $config['per_page'] = '10';
      $this->pagination->intialize($config);

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

and this is my views code
Code:
<table border="1">
<tr class="heading">
    <td>Price&nbsp;&nbsp;&nbsp;Percent_change</td>
    <td>&nbsp;Company Name</td>
    <td>&nbsp;Company Code</td>
    <td>&nbsp;Volume</td>
</tr>
&lt;?php

foreach($stocks->result() as $row)
{
  echo "<tr>";
  echo "<td>".preg_replace("/\s+/","&nbsp;&nbsp;&nbsp;",$row->pricepercent)."</td>";
  echo "<td>&nbsp;".$row->compname."</td>";
  echo "<td>&nbsp;".$row->compcode."</td>";
  echo "<td>&nbsp;".$row->volume."</td>";
}

?&gt;
</table>
&lt;?php echo $this->pagination->create_links(); ?&gt;

I am getting this error
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$pagination

Filename: views/stocks_view.php

Line Number: 31

and this one
Quote:Fatal error: Call to a member function create_links() on a non-object in C:\xampp\htdocs\myapp\system\application\views\stocks_view.php on line 31

where line 31 is this one

Code:
&lt;?php echo $this->pagination->create_links(); ?&gt;


what should i do ?


pagination help - El Forum - 01-19-2010

[eluser]maria clara[/eluser]
try to put the $this->pagination->create_links() in your controller


pagination help - El Forum - 01-19-2010

[eluser]sasori[/eluser]
I did, but it didn't work..actually I copied that one from your thread ?

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

then i called it on my view file like this

Code:
echo $pagination;

i got
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: pagination

Filename: views/stocks_view.php

Line Number: 31
even if 'pagination' was already added in the $data array variable from the controller function


pagination help - El Forum - 01-19-2010

[eluser]maria clara[/eluser]
how do you call your $pagination in your view??


pagination help - El Forum - 01-19-2010

[eluser]theprodigy[/eluser]
it looks like:
Code:
$this->pagination->intialize($config);
is spelled wrong. Try:
Code:
$this->pagination->initialize($config);
(there is an 'i' between the 'n' and 't')


pagination help - El Forum - 01-19-2010

[eluser]sasori[/eluser]
controller ( i corrected the misspelled word ) and still it doesn't work
Code:
$this->load->library('pagination');
      $config['base_url'] = 'http://myapp/welcome/stocks/';
      $config['total_rows'] = '20';
      $config['per_page'] = '10';
      $this->pagination->initialize($config);
      $data['pagination'] = $this->pagination->create_links();
      $this->load->view('stocks_view',$data);

view
Code:
&lt;?php echo $pagination;  ?&gt;



pagination help - El Forum - 01-19-2010

[eluser]maria clara[/eluser]
can you please try this:
Code:
$this->pagination->initialize($config);

echo $this->pagination->create_links();

or
echo print_r($data); //for debugging purpose


pagination help - El Forum - 01-19-2010

[eluser]maria clara[/eluser]
try adding this:

Code:
$per_page = 10;
   $total = $this->stock_model->count_posts();  //total post
   $data['posts'] = $this->stock_model->get_posts($per_page, $this->uri->segment(3));


  $config['uri_segment'] = '3';

can you post your MODEL??


pagination help - El Forum - 01-19-2010

[eluser]sasori[/eluser]
here's my model
Code:
class Stock_model extends Model
{

    function Stock_model()
    {
      parent::Model();
    }

    function getStocks()
    {
      $data = array();
      $data = $this->db->get('stocks');
      return $data;
    }

        function putData($txt)
    {
        $handle = file_get_contents($txt);
        $var_array = explode(";",$handle);
        $a = array_chunk($var_array,4);

            for($i=0;$i<sizeof($a);$i++)
            {
                if(isset($a[$i][1]))
                    $a[$i][1] = preg_replace("/\s\%/","%",$a[$i][1]);

                if(isset($a[$i][0]))
                    $data['compcode'] = $a[$i][0];

                if(isset($a[$i][1]))
                    $data['pricepercent'] = $a[$i][1];

                if(isset($a[$i][2]))
                    $data['compname'] = $a[$i][2];

                if(isset($a[$i][3]))
                    $data['volume'] = $a[$i][3];

                #print_r($data);
                $this->db->insert('stocks',$data);
            }

        return $this->db->affected_rows();
    }

}

here's the controller function for the supposed to be pagination thing
Code:
function index()
    {
      $data['base'] = $this->config->item('base_url');
      $data['css'] = $this->config->item('css');
      $this->load->model('stock_model');
      $stocks = $this->stock_model->getStocks();

      $this->load->library('pagination');
      $config['base_url'] = 'http://myapp/welcome/stocks/';
      $config['total_rows'] = '20';
      $config['per_page'] = '10';
      $this->pagination->initialize($config);
      $data['pagination'] = $this->pagination->create_links();
      $this->load->view('stocks_view',$data);
    }



pagination help - El Forum - 01-20-2010

[eluser]maria clara[/eluser]
can you please try and analyze this in your CONTROLLER:
Code:
function index()
{
  $data['base'] = $this->config->item('base_url');
   $this->load->model('stock_model');
   $per_page = 10;
   $total = $this->stock_model->count_posts();  //total post
   $data['stocks'] = $this->stock_model->getStocks($per_page, $this->uri->segment(3));

   $this->load->library('pagination');
   $config['base_url'] = 'http://myapp/welcome/stocks/';
   $config['total_rows'] = $total;
   $config['per_page'] = $per_page;
   $config['uri_segment'] = '3';

   $this->pagination->initialize($config);
   $this->load->view('stocks_view', $data);
}

model:
Code:
function getStocks($limit = NULL, $offset = NULL)
{
  $this->db->limit($limit, $offset);
  return $this->db->get('stocks');
}

function count_posts()
{
  return $this->db->count_all_results('stocks');
}