Welcome Guest, Not a member yet? Register   Sign In
Trouble with paginating a page called by a form
#1

[eluser]Corbee[/eluser]
Hi,

I have a page(a) that calls another page(b) to display the data, the problem is that the data submitted by page(a) to page(b) can't be read by the paginated form. I tried to session the posted data, but it wasn't helpful at all.

Here is the code.
Controller
Code:
class Manage extends controller
{
        function index()
    {
        $data['title']='System Manager';
        $data['main'] = 'admin_manage_home';
        $data['carbrand']= $this->MManage->getcarbrand();
        $data['year']= $this->MManage->getcaryear();
        $this->load->view('dashboard',$data);
    }

    function vehicle()
    {
        $this->load->library('pagination');
        $data['title']    ='Vehicle Management';
        $data['main'] = 'admin_manage_cars';
    
        $config['base_url'] = base_url().'index.php/manage/vehicle';
            $config['total_rows'] = $this->db->count_all('carmodel');
                $config['per_page'] = '15';
            $config['full_tag_open'] = '<p>';
            $config['full_tag_close'] = '</p>';
        
        $this->pagination->initialize($config);

        //load the model and get results
        $data['vehicle'] = $this->MManage->getvehicle($config['per_page'],$this->uri->segment(3));
        
        $this->load->view('dashboard',$data);
    }
}

Model:
Code:
class MManage extends Model
{
    function __construct()
    {
        parent::Model();
    }    
    
    function getvehicle($num='',$offset='')
    {
        $_SESSION['year'] =$_POST['year'];
        $_SESSION['brand'] = $_POST['brand'];
        $year= $_SESSION['year'];
        $brandID = $_SESSION['brand'];
        $data = array();
        //$this->db->select('modelName,carbrand.brandName,carbrand.logo');
        $this->db->from('carModel');
        $this->db->join('caryear','caryear.modelID=carmodel.modelID','left');
        $this->db->join('carbrand','caryear.brandID=carbrand.brandID','left');
        $this->db->where('year',$year);
        $this->db->limit($num,$offset);
        $q = $this->db->get();    
        if ($q->num_rows() > 0)
        {
            foreach ($q->result_array() as $row)
            {
            $data[] = $row;
            }
        }
    $q-> free_result();
    return $data;
    }
    
    function getcarbrand()
    {
        $this->db->distinct();
        $this->db->from('carmodel');
        $this->db->join('carbrand','carbrand.brandID=carmodel.brandID','left');
        $this->db->where('carbrand.brandID IS NOT NULL');
        return $this->db->get();
    }
    
    function getcaryear()
    {
        return $this->db->get('caryear');
    }
    
}

Views:
page(a) - admin_manage_home
Code:
&lt;form action="manage/vehicle" method="post"&gt;
<fieldset>
  <legend>Administer Vehicle</legend>
  <p>
<div class="tab1">
    <label for="year">Year:</label>
    &lt;?php //populating the year
    if ($year->num_rows() > 0)
        foreach($year->result_array() as $rows)
        {
            $options[$rows['year']]=$rows['year'];
        }
        
    echo form_dropdown('year',$options);
    ?&gt;
    
  <label for="brand">Brand:</label>
    &lt;?php //populating the brand

    if ($carbrand->num_rows() > 0)
        foreach($carbrand->result_array() as $rows)
        {
            $options1[$rows['brandID']]=$rows['brandName'];
        }
        
    echo form_dropdown('brand',$options1);
    ?&gt;  
    </p>

<div class="tab"><div class="tab"><p>&lt;?php echo anchor('manage/addvehicle','Add a New Vehicle'); ?&gt; or &lt;input type="submit" value="Go"/&gt;&lt;/p></div></div></div>
</fieldset>
&lt;/form&gt;

page(b) - admin_manage_cars
Code:
&lt;?php
echo '<p>';
if ($this->session->flashdata('message')){
echo "<div class='message'>".$this->session->flashdata('message')."</div>";
}
if (count($vehicle))
    {
    echo "<table border='1' cellspacing='0' cellpadding='3' width='400' > \n";
    echo " <tr valign='top' > \n";
    echo " <th>Year</th><th>Model Name</th><th>Type</th><th>Actions</th>\n";
    echo " </tr>";
        foreach ($vehicle as $key => $list)
    {
    echo "<tr valign='top'>";
    echo "<td>".$list['year']."</td>";
    echo "<td> ".$list['modelName']."</td>";
    echo "<td> ".$list['carType']."</td>";
    echo "<td align='center'>";
    echo anchor('vehicle/edit/'.$list['caryearID'],'edit');
    echo "</td>\n";
    echo "</tr>\n";
    }
    echo "</table>";
}
    echo $this->pagination->create_links();
    echo '</p>';
?&gt;

By the way, here is the error output when you used the pagination links
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: year

Filename: models/mmanage.php

Line Number: 12
A PHP Error was encountered

Severity: Notice

Message: Undefined index: brand

Filename: models/mmanage.php

Line Number: 13
#2

[eluser]Georgi Budinov[/eluser]
Hello

First of all about the errors you are getting: these are notices, it is good to fix them by first checking with the isset function for existence of the needed key in an array. The alternative is switching of the notices by doing this in the index.php:
Code:
error_reporting(E_ERROR | E_WARNING | E_PARSE);

About your main problem: I would catch the input from the form on post and redirect to url that will hold as a segment the keywords and selections done from the form. When listing I will check for them and make the query accordingly. The pagination library then would work as expected.
#3

[eluser]Corbee[/eluser]
Thanks, I've tried your advice, unfortunately, I'm still having trouble passing the data.
Please check if you can find what's wrong with this.

Controller
Code:
class Manage extends controller
{
    function __construct()
    {
        parent::Controller();
        session_start();
    }
    
    function index()
    {
        $data['title']='System Manager';
        $data['main'] = 'admin_manage_home';
        $data['carbrand']= $this->MManage->getcarbrand();
        $data['year']= $this->MManage->getcaryear();
        $this->load->view('dashboard',$data);
    }

    function vehicle($year='',$brand='')
    {
        //$year='2006'; -> whenever I define it here, it works
        //$brand='13'; ->  but when I used $_Post, it doesn't work
        $year = $this->input->post('year');
        $brand = $this->input->post('brand');
        
        //echo $this->uri->segment(3);exit;

        $this->load->library('pagination');
        $data['title']    ='Vehicle Management';
        $data['main'] = 'admin_manage_cars';
    
        //$config['base_url'] = base_url().'index.php/manage/vehicle/' . $year . '/' . $brand;
        $config['base_url'] = base_url().'index.php/manage/vehicle';
        $config['total_rows'] = $this->db->count_all('carmodel');
        $config['per_page'] = '15';
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';
        
        $this->pagination->initialize($config);

        //load the model and get results
        $data['vehicle'] = $this->MManage->getvehicle($config['per_page'],$this->uri->segment(5),$year,$brand);
        
        $this->load->view('dashboard',$data);
    }

}

Model
Code:
class MManage extends Model
{
    function __construct()
    {
        parent::Model();
    }    
    
    function getvehicle($num='',$offset='',$year='',$brand='')
    {
        //$_SESSION['year'] =$_POST['year'];
        //$_SESSION['brand'] = $_POST['brand'];
        //$year= $_SESSION['year'];
        //$brandID = $_SESSION['brand'];
        $data = array();
        //$this->db->select('modelName,carbrand.brandName,carbrand.logo');
        $this->db->from('carModel');
        $this->db->join('caryear','caryear.modelID=carmodel.modelID','left');
        $this->db->join('carbrand','caryear.brandID=carbrand.brandID','left');
        $this->db->where('year',$year);
        $this->db->limit($num,$offset);
        $q = $this->db->get();    
        if ($q->num_rows() > 0)
        {
            foreach ($q->result_array() as $row)
            {
            $data[] = $row;
            }
        }
    $q-> free_result();
    return $data;
    }
    
    
    
    function getcarbrand()
    {
        $this->db->distinct();
        $this->db->from('carmodel');
        $this->db->join('carbrand','carbrand.brandID=carmodel.brandID','left');
        $this->db->where('carbrand.brandID IS NOT NULL');
        return $this->db->get();
    }
    
    function getcaryear()
    {
        return $this->db->get('caryear');
    }
    
}




Theme © iAndrew 2016 - Forum software by © MyBB