$this->pagination->create_links(); returning nothing - codeigniter

Link for pagination not working
This is my controller.
Here Is my code. What's wrong in this.
Here links are not Initialize. Tell me the correct solutions
The create_links(); the function seems to not be working. I don't get any errors, but it just returns a blank string. I'm aware the documentation says http://codeigniter.com/user_guide/libraries/pagination.html The create_links() function returns an empty string when there is no pagination to show. but so how do I fix that? Thanks you!
public function Index()
{
$this->load->model('Testmodel');
$conf = array();
$conf['base_url'] = base_url()."index.php/user/view";
$conf['total_rows'] = $this->db->get('user')->num_rows();
$conf['per_page'] = 5;
$conf['use_page_numbers'] = TRUE;
$conf['num_links'] = 2;
$conf['cur_tag_open'] = ' <a class="current">';
$conf['cur_tag_close'] = '</a>';
$conf['next_link'] = 'Next';
$conf['prev_link'] = 'Previous';
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 0;
}
$data['records'] = $this->Testmodel->fetch_data($conf['per_page'], $page);
$this->pagination->initialize($conf);
$data['links'] = $this->pagination->create_links();
}
Model Code:
public function fetch_data($limit, $start) {
$this->db->select('user.*,country.countryName,state.stateName,city.cityName');
$this->db->from('user');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;

Related

Requesting solution for laravel 8 project

I'm an elementary web developer, I'm facing a problem. Please help me solve this. I'm creating both row and column dynamic. for save() is fine. But for update, only name is updated and other item
quantities cannot. Here is my codes.
//for save()
public function stockAdd(Request $request)
{
$validator = validator(request()->all(), ['stock_name' => 'required']);
if($validator->fails()) {
return back()->withErrors($validator);
}
$user_id = Auth::user()->user_id;
$stock_id = random_int(100000, 999999);
$stocks = new Stocks();
$stocks->stock_id = $stock_id;
$stocks->owner_id = $user_id;
$stocks->stock_name = request()->stock_name;
$stocks->my_stock = 0;
$stocks->disable = 0;
$stocks->save();
$brands = Brands::all();
$products = Products::all();
foreach ($brands as $brand){
foreach ($products as $product){
if($product['brand_id'] == $brand['id']){
$product_id = $product->product_id;
$stockitems = new StockItems();
$stockitems->stock_id = $stock_id;
$stockitems->product_id = $product->product_id;
$stockitems->owner_id = $user_id;
if(request()->$product_id > 0){
$stockitems->count = request()->$product_id;
}else{
$stockitems->count = 0;
}
$stockitems->save();
}
}
}
return redirect()->back()->with('successAddMsg','Successfully Added');
}
//for update()
public function stockUpdate(Request $request, $id)
{
$validator = validator(request()->all(), ['stock_name' => 'required']);
if($validator->fails()) {
return back()->withErrors($validator);
}
$stock = Stocks::findOrFail($id);
$stock->stock_name = request()->stock_name;
$stock->my_stock = 0;
$stock->disable = 0;
$stock->update();
$brands = Brands::all();
$products = Products::all();
foreach ($brands as $brand){
foreach ($products as $product){
if($product['brand_id'] == $brand['id']){
$product_id = $product->product_id;
//dd($product_id);
$stockitemitems = DB::table("stock_items")->select("*")->where("owner_id",Auth::user()->user_id)->where("stock_id",$stock->stock_id)->get();
$stockitems->count = $request->input($product_id);
$stockitems->update();
}
}
}
return redirect(route('user.stock-list'))->with('successAddMsg','Successfully Added');
}
The problem shows like this "Creating default object from empty value"

Codeigniter-passing value from controller to model

My English is not good so sorry for any mistake. I'm new to Codeigniter, I am trying to search record by using this code, this is my controller code:
public function search() {
if(isset($_POST['search']))
{
$s = $_POST['search'];
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
$this->db->where('name =' ,"{$s}" );
$query = $this->db->get();
$res = $query->result();
$data['user'] = null ;
if($res)
{
$data['user'] = $res ;
}
$this->load->view('filter' , $data);
}
}
It is working fine but I want to write this code in separate Model.
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
$this->db->where('name =' ,"{$s}" );
$query = $this->db->get();
$res = $query->result();
But i don't know how to pass this variable value $s = $_POST['search']; to my model. Any suggestion?
you can try the following
//in your application/models Folder put a File called UserSearch_Model.php
class UserSearch_Model extends CI_Model
{
public function search($strSearch)
{
$query = $this->db
->select('u_id,name,email,phone')
->from('user')
->where('name' ,$s)
->get();
return $query->result();
}
}
//and in your controller
public function search()
{
$strSearch = $this->input->post('search');
if (!empty($strSearch))
{
$data = [];
$this->load->model("UserSearch_Model");
$data['user'] = $this->UserSearch_Model->search($strSearch);
if (is_array($data['user']) && count($data['user']) > 0)
{
$this->load->view('filter' , $data);
}
}
}
You can try this solution for your problem :
public function search() {
$search_value = $this->input->post('search'));
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
if (isset($search_value) && !empty($search_value)) {
$this->db->where('name',$search_value);
// OR
$this->db->like('name', $search_value);
}
$query = $this->db->get();
$res = $query->result();
$data['user'] = null;
if ($res) {
$data['user'] = $res;
}
$this->load->view('filter', $data);
}
I hope it will help.
Controller
public function search() {
$search = $this->input->post('search');
$this->load->model('your_model'); //<-- You can load the model into "__construct"
$search_result = $this->your_model->your_search_function($search); //<-- This is how you can pass a variable to the model from controller
$this->load->view('your_view',['search_result'=>$search_result]); //<-- This is how you can load the data to the view. Hear your search result array is loaded on your view
}
Model
public function your_search_function($search) {
//You will receive this search variable ^^ here in your model
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
$this->db->where('name',$search);
$query = $this->db->get();
$result = $query->result_array();
if(isset($result) && !empty($result))
{
return $result;
} else
{
return FALSE;
}
}

pagination not working in codeigniter

my controller page
public function index() {
$data['error']="";
$data['h']="";
$this->load->library('pagination');
$config['base_url'] = base_url().'Imagec/index';
$config['total_rows'] = 10;
$config['per_page'] = 2;
$config["uri_segment"] = 4;
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data['h']=$this->Inserts_model->select($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('select_view', $data);
}
my mode page
public function select($limit, $start)
{
$this->db->limit($limit, $start);
$query = $this->db->get('student');
return $query->result();
}
my view page
<p><?php echo $links; ?></p>
here all my code here when click on links the NOT FOUND error occur's
Try this 100% will work
Controller :
public function index() {
$data['error'] = "";
$data['h'] = "";
$this->load->library('pagination');
$data['h'] = $this->inserts_model->select();
$config['base_url'] = base_url() . '/index.php/imagec/index';
$config['total_rows'] = count($data['h']);//it will give you total no of records
$config['per_page'] = 2;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['h'] = $this->inserts_model->select($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
// echo "<pre>";print_r($data);die;
$this->load->view('select_view', $data);
}
Model :
public function select($limit=0, $start=0) {
if (empty($start) && !empty($limit)) {
$this->db->limit($limit);
}
if (!empty($start) && !empty($limit)) {
$this->db->limit($limit, $start);
}
$query = $this->db->get('student');
return $query->result();
}

how to link the pages using codeigniter pagination?

how to link the pages using codeigniter pagination?
the problem is with the variable $start its supposed to pass the start value to the database limit query . but its not.
Model
public function fetch_countries($limit,$start) {
$this->db->limit($limit, $start);
//$query ="SELECT * FROM mail_to Order By id Desc LIMIT ".$limit.",".$start;
$query = $this->db->get("mail_to");
//$qry=$this->db->query($query);
var_dump($query->result_array());
if($query->num_rows()>0){
return $query->result_array();
}
else
{
return false;
}
}
Controller
public function example1() {
$config = array();
$config["base_url"] = base_url() . "index.php/admin/user/example1";
$config["total_rows"] = $this->mdl_user->record_count();
$config["per_page"] = 5;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
//var_dump($config['per_page']);
$data["results"] = $this->mdl_user->fetch_countries($config["per_page"],$page);
//var_dump($data['results']);
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
}
URI Segment should be
$config["uri_segment"] = 4;
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
In Model
public function fetch_countries($limit, $page)
{
$query = $this->db->query("SELECT * FROM mail_to LIMIT $page, $limit");
$result = $query->result_array();
return $result;
}
In Controller
public function example1() {
$config["base_url"] = base_url() . "index.php/admin/user/example1";
$config["total_rows"] = $this->mdl_user->record_count();
$config["per_page"] = 5;
$config['uri_segment'] = 4;
$limit = $config['per_page'];
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data["results"] = $this->mdl_user->fetch_countries($page $limit);
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
}

CodeIgniter Pagination

I after some help with codeIgniter pagination (fixign a bug).. I can see the number of records and the pagination link in the view but when click on the next link, it does not show the next/previous 10 records. Can someone please help me with this?
I have the following code in my controller
function customerlist_pagination()
{
$search = $this->input->post('search');
$data['title'] = "Customer Clist";
$data['heading'] = "List of customers";
$data['result'] = $this->customers_model->getAllcustomers_pagination($search_para, FALSE, "limit_rows");
$data['num_recs'] = $this->customers_model->getAllcustomers_pagination($search_para, FALSE, "num");
$config['base_url'] = base_url().'index.php/customer/customerlist_pagination/';
$config['total_rows'] = $data['num_recs'];
$config['per_page'] = 10;
$config['uri_segment'] = 3;
$config['next_link'] = 'Next >';
$config['prev_link'] = '< Previous ';
$this->pagination->initialize($config);
$this->load->view('customer_view_table',$data);
}
In the model, I have the following code:
function getAllSeizures_pagination($search_para, $archive_search = FALSE, $result_type)
{
$search_para = $search_para."%";
$selected_fields = "customer.firstName
customer.lastName, customer.address,
customer.city, customer.postcode";
$from = "customer";
$where = "customer.deleted=0 ";
if ($archive_search == TRUE) {
$where .= "AND customer.archived= 1 ";
}else{
$where .= "AND customer.archived= 0 ";
}
$where .= "AND (customer.firstName LIKE '$search_para' OR customer.postcode LIKE '$search_para' OR customer.city LIKE '$search_para')";
$query = $this->db->select($selected_fields, false)
->from($from)
->where($where)
->order_by('customer.idcustomer', 'desc');
if($result_type == "limit_rows")
{
$query = $this->db->limit(10, 0)
->get();
$query = $query->result();
}
if($result_type == "num") {
$query = $this->db->get();
$query = $query->num_rows();
}
return $query;
}
Thanks alot
Regards
Prats
The pagination class passes the offset to you in the uri_segment specified. Since you set it to 3, you should capture that info in the first variable of your controller method, like so:
function customerlist_pagination( $offset )
{
}
Then, when you call your $this->customers_model->getAllcustomers_pagination(), you have to pass it the $offset, so that later on in your code, where you limit your results:
$query = $this->db->limit(10, 0)->get();
you should instead use this $offset to limit your results:
$query = $this->db->limit(10, $offset)->get();

Resources