riya riya - 4 months ago 77
PHP Question

Advanced search Using Pagination in codeigniator

I want to set all the advanced search parameter using session how to set all the parameter at time.

I am using following function but it only set one parameter at time how to set all the parameter at time

public function searchterm_handler($searchterm)
{
if($searchterm)
{
$this->session->set_userdata('searchterm', $searchterm);
return $searchterm;
}
elseif($this->session->userdata('searchterm'))
{
$searchterm = $this->session->userdata('searchterm');
return $searchterm;
}
else
{
$searchterm ="";

return $searchterm;
} }

Answer

Method one (recommended)

So for pagination in CodeIgniter, you have 3 main variables you must set and a configuration method to call. You also have a library you must load.

The library is $this->load->library('pagination');

The 3 variables and configuration look like this:

//This next line is used mainly so the page number links on your pagination work.
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = $NumberOfRecords;
$config['per_page'] = 20;

$this->pagination->initialize($config);

If you are using MVC then this is quite simple. You would use the above code in your controller, grab the data you want to display starting at the nth row, where n is the page number * $config['per_page'], and ending at ((page number * $config['per_page']) + $config['per_page'])-1.

After getting the necessary data you would return that and the link code to your view. The link code is $this->pagination->create_links();

So your return might look something like this:

$data["results"] = $this->MyModel->MySqlMethod($config["per_page"], $CurrentPage);
$data["links"] = $this->pagination->create_links();

Then in your view you would loop through the $data["results"] and after the loop you would display the $data["links"]

This would give you your data displayed then the pagination at the bottom would look something like

enter image description here

So your controller all together should look like:

$config['base_url'] = 'http://example.com/index.php/controllerName/ViewName/';
$config['total_rows'] = $NumberOfRecords;
$config['per_page'] = 20;

$this->pagination->initialize($config);
$data["results"] = $this->MyModel->MySqlMethod($config["per_page"], $CurrentPage);
$data["links"] = $this->pagination->create_links();

return $this->load->view("ViewName", $data);

Method Two (NOT recommended)

Now you mentioned something about storing that data in Session Variables. I mean if you want you can do this. If you are going to use that method, then that tell you are not using MVC. CodeIgniter is meant for MVC. If you are not using MVC then you probably do not need CodeIgniter. If you are comfortable using CodeIgniter and do not want to try and implement the MVC, by all means go ahead.

To do the CodeIgniter Pagination in this method, you would change your public searchterm_handler($searchterm) function. The thing with session variables is that they are stored on the users browser so that way you, the programmer, can access them anywhere on your site without having to return and pass them from class to class or method to method. If you set a session variable then you return it, that is redundent and unnecessary.

You don't really need this method, it is unnecessary, but you could do something like this:

public function searchterm_handler($searchterm) { 
   $result = mysqli_query("SELECT count(*) FROM User_info");
   $row = mysqli_fetch_row($result);
   $TotalDataCount = $row[0];
   $this->session->set_userdata("TotalDataCount", $TotalDataCount);
   $this->session->set_userdata("RecordsPerPage", 20);
   $this->session->set_userdata("BaseURL", www.example.com/link/to/your/page.php);
   $this->pagination->initialize($config);

   if($searchterm) {
        $this->session->set_userdata('searchterm', $searchterm);
        //Unnecessary
        //return $searchterm;
    } else {
       $this->session->set_userdata('searchterm', "");
        //return $searchterm;
    }
}

Then in the code that called searchterm_handler($searchterm), you would do this:

searchterm_handler($input);
$searchterm = $this->session->userdata('searchterm');
$dataToReturn = array();
if($searchterm!="") {
   $result = mysqli_query("SELECT * FROM table WHERE field LIKE '%$this->session->userdata('searchterm')%'");
   if($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
echo $this->pagination->create_links();

LET ME WORN YOU

This second method, is gross and ugly and yucky and very badly written. There is no real good way to write what you want to write. The purpose of using CodeIgniter is for MVC and built in CodeIgniter functionality, which you lose almost all of it when you get rid of MVC.

I know there is a chance I misunderstood what you are trying to do, but this was my best guess. My best advice for you is to use MVC in CodeIgniter.

Here are some sources that may help you if you use the first method:

https://www.sitepoint.com/pagination-with-codeigniter/

https://www.codeigniter.com/userguide3/libraries/pagination.html

I hope this helps, I spent a lot of time writing it...


Update - Method 3

I tried looking at your question again and maybe this will help

public function searchterm_handler($searchterm)
  { 
   if($searchterm && $this->session->userdata('email'))
    { //user logged in
        $this->session->set_userdata('searchterm', $searchterm);
        $array = array(
                "searchterm" => $searchterm,
                "email" => $this->session->userdata('email'),
                "username" => $this->session->userdata('username') 
        );
        return $array;
    } 
    else if($searchterm && !$this->session->userdata('searchterm'))
    { //user not logged in
        $this->session->set_userdata('searchterm', $searchterm);
        return $searchterm;
    }
    elseif($this->session->userdata('searchterm') && $this->session->userdata('searchterm')) 
    { //user logged in
         $searchterm = $this->session->userdata('searchterm');
        $array = array(
                "searchterm" => $searchterm,
                "email" => $this->session->userdata('email'),
                "username" => $this->session->userdata('username') 
        );
        return $array;
    }
    elseif($this->session->userdata('searchterm') && !$this->session->userdata('searchterm')) 
    { //user not logged in
        $searchterm = $this->session->userdata('searchterm');
        return $searchterm;
    }
    else
    {
        $searchterm ="";

        return $searchterm;
    } }

sorry if this is may, I did it on my phone

Comments