Hi i'm trying to get the pagination right with code igniter but it seems that it doesn't want to work correctly. I get the second page, but there the pagination disappears, I still got the right table though and I have 2 errors:
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: models/evaluation_model.php
Line Number: 37
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/evaluation_model.php
Line Number: 37
The controller function:
function showEvaluations($offset = 0)
{
if($this->login->is_logged_in())
{
$limit = 5;
$result = $this->evaluation_model->getAllEvaluations($limit, $offset);
if ($this->session->userdata('type') == 'admin')
{
$data['evaluations'] = $result['evaluations'];
$data['total'] = $result['num_rows'];
$data['notallowed'] = false;
$config = array();
$config['base_url'] = base_url("evaluation/showEvaluations/");
$config['total_rows'] = $data['total'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('allevaluations_view', $data);
}
else
{
$data['notallowed'] = true;
$this->load->view('allevaluations_view', $data);
}
//$this->load->view('allevaluations_view', $data);
}
else
{
$this->load->view('login_view');
}
}
and the Model:
function getAllEvaluations($limit, $offset)
{
$q = $this->db->select('tblPunten.PK_PuntID, tblPunten.Titel, tblPunten.Score, tblVakken.Vak, tblUsers.username, tblUsers.Voornaam, tblUsers.Achternaam')
->from('tblPunten')
->join('tblVakken', 'tblPunten.FK_VakID = tblVakken.PK_VakID')
->join('tblUsers', 'tblPunten.FK_UserID = tblUsers.PK_UserID')
->limit($limit, $offset);
$query['evaluations'] = $q->get()->result();
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('tblPunten')
->limit($limit, $offset);
$tmp = $q->get()->result();
$query['num_rows'] = $tmp[0]->count;
return $query;
}
Line 37: $query['num_rows'] = $tmp[0]->count;
Found it, the second query:
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('tblPunten')
->limit($limit, $offset);
must be without the limit:
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('tblPunten')
In codeigniter, ->result() an object.
if you want in that form of array then we can use ->result_array() so that we can get the result set in the form of array.
$tmp = $q->get()->result_array();//made changes
$query['num_rows'] = $tmp[0]['count'];//will work
Related
I am new in PHP and learning Codeigniter. I have an issue to put where condition with my search query. My controller is like below
public function index(){
$data = array();
// Get messages from the session
if($this->session->userdata('success_msg')){
$data['success_msg'] = $this->session->userdata('success_msg');
$this->session->unset_userdata('success_msg');
}
if($this->session->userdata('error_msg')){
$data['error_msg'] = $this->session->userdata('error_msg');
$this->session->unset_userdata('error_msg');
}
// If search request submitted
if($this->input->post('submitSearch')){
$inputKeywords = $this->input->post('searchKeyword');
$searchKeyword = strip_tags($inputKeywords);
if(!empty($searchKeyword)){
$this->session->set_userdata('searchKeyword',$searchKeyword);
}else{
$this->session->unset_userdata('searchKeyword');
}
}elseif($this->input->post('submitSearchReset')){
$this->session->unset_userdata('searchKeyword');
}
$data['searchKeyword'] = $this->session->userdata('searchKeyword');
// Get rows count
$conditions['searchKeyword'] = $data['searchKeyword'];
$conditions['returnType'] = 'count';
$rowsCount = $this->member->getRows($conditions);
// Pagination config
$config['base_url'] = base_url().'members/index/';
$config['uri_segment'] = 3;
$config['total_rows'] = $rowsCount;
$config['per_page'] = $this->perPage;
// Initialize pagination library
$this->pagination->initialize($config);
// Define offset
$page = $this->uri->segment(3);
$offset = !$page?0:$page;
// Get rows
$conditions['returnType'] = '';
$conditions['start'] = $offset;
$conditions['limit'] = $this->perPage;
$data['members'] = $this->member->getRows($conditions);
$data['title'] = 'Members List';
// Load the list page view
//$this->load->view('templates/header', $data);
$this->load->view('members/index', $data);
//$this->load->view('templates/footer');
}
and my model is like below
function getRows($params = array()){
$this->db->select('*');
$this->db->from($this->table);
//$where = "is_verified = 1";
//$this->db->where($where);
if(array_key_exists("conditions", $params)){
foreach($params['conditions'] as $key => $val){
$this->db->where($key, $val);
}
}
if(!empty($params['searchKeyword'])){
$search = $params['searchKeyword'];
$likeArr = array('fname' => $search, 'lname' => $search, 'gran_id' => $search);
$this->db->or_like($likeArr);
}
if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
$result = $this->db->count_all_results();
}else{
if(array_key_exists("user_id", $params)){
$this->db->where('user_id', $params['user_id']);
$query = $this->db->get();
$result = $query->row_array();
}else{
$this->db->order_by('fname', 'asc');
if(array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit'],$params['start']);
}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit']);
}
//$query = $this->db->get();
$query = $this->db->where('is_verified', '1')->get();
$result = ($query->num_rows() > 0)?$query->result_array():FALSE;
}
}
// Return fetched data
return $result;
}
I want to fetch only records where
is_verified = 1
and so I have put it with my query and its working fine without search query. If I search its showing record even if_verified have another value. I am not getting idea which another place I should put this where condition so when I search, it can show only required records.
Thanks!
This will happen when using or_like in Codeigniter you should use group_start and group_end function to separate your search with where condition
if(!empty($params['searchKeyword'])){
$search = $params['searchKeyword'];
$likeArr = array('fname' => $search, 'lname' => $search, 'gran_id' => $search);
$this->db->group_start(); // this will make brackets for your search query
$this->db->or_like($likeArr);
$this->db->group_end();
}
You are not filtering the query when a search is performed you're only doing it outside of a search.
Try: Find this block
if(!empty($params['searchKeyword'])){
$search = $params['searchKeyword'];
$likeArr = array('fname' => $search,'lname' => $search, 'gran_id' => $search);
$query = $this->db->where('is_verified', '1');
$this->db->or_like($likeArr);
}
controller: HomeController.php
public function index()
{
// home page redirect query
$data['redirect'] = SiteSettings::where('name','default_home')->get();
$data['popular_rooms'] = Rooms::where('status','Listed')->get();
$data['property_rooms'] = PropertyRooms::select("property_room.*","rooms_photos.name", "rooms_price.night")
->join("rooms_photos","rooms_photos.room_id","=","property_room.room_id")
->join("rooms_price","rooms_price.room_id","=","property_room.room_id")
->where('rooms_photos.featured','Yes')
->get();
$data['city_count'] = HomeCities::all()->count();
$data['result'] = ThemeSettings::get();
$data['browser'] = '';
if(isset($_SERVER['HTTP_USER_AGENT']))
{
$agent = $_SERVER['HTTP_USER_AGENT'];
if(strlen(strstr($agent,"Chrome")) > 0 )
{
$data['browser'] = 'chrome';
}
}
$data['home_page_media'] = SiteSettings::where('name', 'home_page_header_media')->first()->value;
$data['home_page_sliders'] = Slider::whereStatus('Active')->orderBy('order', 'asc')->get();
$data['home_page_bottom_sliders'] = BottomSlider::whereStatus('Active')->orderBy('order', 'asc')->get();
$data['host_banners'] = HostBanners::all();
$data['home_city'] = HomeCities::all();
$data['languagess'] = Language::where('default_language', '1')->first()->value;
$data['bottom_sliders'] = BottomSlider::whereStatus('Active')->orderBy('order', 'asc')->get();
$data['our_community_banners'] = OurCommunityBanners::limit(3)->get();
//home page two data start
$data['reservation'] = Reservation::orderBy('id', 'desc')->where('status','Accepted')->groupBy('room_id')->limit(10)->get();
$data['view_count'] = Rooms::orderBy('views_count', 'desc')->where('status','Listed')->groupBy('id')->get();
$data['recommented'] = Rooms::orderBy('id', 'desc')->where('recommended','Yes')->where('status','Listed')->groupBy('id')->get();
$data['res_count'] = count($data['reservation']);
$data['room_view_count'] = count($data['view_count']);
$data['room_recommented_view'] = count($data['recommented']) ;
//redirect home page
if($data['redirect'][0]->value == 'home_two')
{
$data['default_home'] = 'two' ;return view('home.home_two',$data);
}
else
{
return view('home.home', $data);
}
}
route:
Route::group(['middleware' => ['install','locale']], function () {
Route::get('/', 'HomeController#index');
});
In this code I have to run my index file inside the controller but when I server php artisan it throw an offset error i.e. ErrorException in Collection.php line 1043: Undefined offset: 0. I don't know why where I am doing wrong? Please help me.
Thank You
You need to check that the index is present before accessing it.
isset($data['redirect'][0])
if(isset($data['redirect'][0])) {
if($data['redirect'][0]->value == 'home_two') {
$data['default_home'] = 'two';
return view('home.home_two',$data);
}
}
In foreach loop i am getting warning Invalid argument supplied for foreach() below is my loop code and modal code please let me know where i done wrong
Controller
$concond = array("con_id"=>1,"con_status"=>1);
$this->data['contact']=$this->Frontend_model->get_contact($concond);
$this->load->view('frontend/clienthome',$this->data);
foreach Loop
if($contact)
{
foreach($contact as $foot)
{
$footadr1 = $foot->con_addr_line_1;
$footadr2 = $foot->con_addr_line_2;
$footcity = $foot->con_city;
$footstate = $foot->con_state;
$footcountry = $foot->con_country;
$footpin = $foot->con_pincode;
$footp1 = $foot->con_phone_1;
$footp2 = $foot->con_phone_2;
$footp3 = $foot->con_phone_3;
$footp4 = $foot->con_phone_4;
$footemail = $foot->con_email_id;
}
}
modal
function get_contact($concond)
{
$this->db->select('*');
$this->db->from('is_addres_contact');
$this->db->where($concond);
$query = $this->db->get();
return $query->result();
}
Seems like I got your problem!
You should try to do it like this:
$data['contact'] = $this->Frontend_model->get_contact($concond);
$this->load->view('frontend/clienthome', $data);
Try in model
function get_contact($con_id, $con_stat)
{
$this->db->where('con_id', $con_id);
$this->db->where('con_status', $con_stat);
$query = $this->db->get('is_addres_contact');
return $query->result();
}
Controller
$con_id = '1';
$con_stat = '1';
$this->data['contact'] = $this->frontend_model->get_contact($con_id, $con_stat);
$this->load->view('frontend/clienthome',$this->data);
Can you just try this,
In model,
function get_contact($concond)
{
$this->db->select('*');
$this->db->from('is_addres_contact');
$this->db->where($concond);
$query = $this->db->get();
return $query->result_array();
}
In View
if($contact)
{
foreach($contact as $foot)
{
$footadr1 = $foot['con_addr_line_1'];
$footadr2 = $foot['con_addr_line_2'];
$footcity =$foot['con_city'];
$footstate = $foot['con_state'];
$footcountry = $foot['con_country'];
$footpin = $foot['con_pincode'];
$footp1 = $foot['con_phone_1'];
$footp2 = $foot['con_phone_2'];
$footp3 = $foot['con_phone_3'];
$footp4 = $foot['con_phone_4'];
$footemail = $foot['con_email_id'];
}
}
When I sen a request this page , I got an error like this
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: models/mproduk.php
Line Number: 116
This is my controller
public function update($id=''){
$data = array();
$data['size'] = $this->msize->tampil();
$data['eta']=$this->mproduk->berapakah_stoknya($id);
$data['cats'] = $this->mkategori->tampil();
$data['prd'] = $this->mproduk->get_produk_detail($id);
$data['human'] = $this->mhuman->tampil();
$html = array();
$html['header'] = $this->load->view('secuser/header',null,true);
$html['kiri'] = $this->load->view('secuser/kiri',null,true);
$html['content'] = $this->load->view('secuser/product/update',$data,true);
$this->load->view('secuser/template',$html);
}
This is my model
public function berapakah_stoknya($kodepro){
$this->load->model('msize');
$hahaha=$this->msize->tampil();
foreach($hahaha as $wkwkwkw){
$apa= $wkwkwkw['id'];
$sql= "select ukr_$apa from produk where kodeproduk = '".$kodepro. "'";
$ggg= $this->db->query($sql)->result_array();
$return[]=array('stok'=>$ggg[0]["ukr_$apa"],'ukuran'=>$wkwkwkw['ukuran_produk'],'id_'=>$apa);
}
return $return;
}
If I use error_reporting(0) the page will be looping and show to be some page
How can i fix this error?
Your DB query retrieves no results and therefore $ggg is empty and has no 0 index.
Change your model to check if any results were found. If found, add to $return array.
public function berapakah_stoknya($kodepro){
$this->load->model('msize');
$hahaha = $this->msize->tampil();
$return = array();
foreach($hahaha as $wkwkwkw){
$apa= $wkwkwkw['id'];
$sql= "select ukr_$apa from produk where kodeproduk = '".$kodepro. "'";
$ggg= $this->db->query($sql)->result_array();
if(count($ggg)>0){
$return[]=array('stok'=>$ggg[0]["ukr_$apa"],'ukuran'=>$wkwkwkw['ukuran_produk'],'id_'=>$apa);
}
}
return $return;
}
It seems your are expectin to retrieve only one row. So you could use row_array.
public function berapakah_stoknya($kodepro){
$this->load->model('msize');
$hahaha = $this->msize->tampil();
$return = array();
foreach($hahaha as $wkwkwkw){
$apa= $wkwkwkw['id'];
$sql= "select ukr_$apa from produk where kodeproduk = '".$kodepro. "'";
$query = $this->db->query($sql);
if($query->num_rows() > 0){
$ggg = $query->row_array();
$return[]=array('stok'=>$ggg["ukr_$apa"],'ukuran'=>$wkwkwkw['ukuran_produk'],'id_'=>$apa);
}
}
return $return;
}
Im trying to get an max value with codeigniter from an table but it isnt working. This is the error i get:
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to
string
Filename: database/DB_active_rec.php
Line Number: 427
This is my function:
public function getPeriodeNummer($bedrijf_id) {
$this->db->select_max('id');
$this->db->where('bedrijf_id', $bedrijf_id);
$result = $this->db->get('rapporten');
$this->db->select('periode_nummer');
$this->db->where('rapporten_id', $result);
$query = $this->db->get('statistieken_onderhoud');
$data = $query + 1;
return $data;
}
What im trying to do is as followed:
Select the highest id where bedrijf_id = $bedrijf_id from rapporten.
Select the periode_nummer from statistieken_onderhoud where rapporten_id = the highest id i got from step 1.
Add 1 to the periode_nummer i got from step 2 and return that number.
Thanks in forward for your help!
Try
public function getPeriodeNummer($bedrijf_id) {
$this->db->select_max('id');
$this->db->where('bedrijf_id', $bedrijf_id);
$res1 = $this->db->get('rapporten');
if ($res1->num_rows() > 0)
{
$res2 = $res1->result_array();
$result = $res2[0]['id'];
$this->db->select('periode_nummer');
$this->db->where('rapporten_id', $result);
$query = $this->db->get('statistieken_onderhoud');
if ($query->num_rows() > 0)
{
$row = $query->result_array();
$data['query'] = 1 + $row[0]['periode_nummer'];
}
return $data['query'];
}
return NULL;
}
Try this:
$this->db->select_max('display_sequence');
$this->db->from('acl_menu');
$query = $this->db->get();
$r=$query->result();
Display Sequence is your column name & acl_menu is your table name.
You can't use an object as a string. Use this:
public function getPeriodeNummer($bedrijf_id) {
$this->db->select_max('id');
$this->db->where('bedrijf_id', $bedrijf_id);
$result = $this->db->get('rapporten');
$this->db->select('periode_nummer');
$this->db->where('rapporten_id', $result);
$query = $this->db->get('statistieken_onderhoud');
// fetch first row in object
$result = $query->row();
$data = $result + 1;
return $data;
}
$this->db->select_max('id', 'max_id');
$query = $this->db->get('video_processing');
return $query->row();
try the above:
I think the $query variable is holding a mysql result resource and it cannot be used as a String or in this case an Integer.
You could try this way:
$data = mysql_result($query,0) + 1;
Shortest:
$this->db->select_max('id', 'max_id')->get('video_processing')->row();