select max codeigniter - php

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();

Related

Codeigniter Select and Count MySQL Records

Using Codeigniter 3, I would like to display all the records from a table in a MySQL database. I'd also like to include the number of records selected.
For example;
Showing x number of records;
record 1
record 2
record 3
etc
Currently I have the following (which works);
// select all records
public function selectRecords() {
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
return $query->result_array();
}
// count all records
public function countRecords() {
$this->db->select('count(*) as count');
$this->db->from('records');
$query = $this->db->get();
return $query->row();
}
My question is do I need two separate queries in order to achieve this (select and count)?
Is there a more efficient way of achieving what I want?
You can do something like this :
public function selectRecords()
{
$query = $this->db->get('records');
if ($query->num_rows() > 0 )
{
$records = $query->result_array();
$data['count'] = count($records);
$data['all_records'] = $records;
return $data;
}
}
Pass it to the view from your controller :
$data = $this->model_name->selectRecords();
/*print_r($data) to see the output*/
$this->load->view('your_view',$data);
In view :
<?php echo $count .' number of records';?>
you can do only:
public function selectRecords() {
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
return $query->result_array();
}
and
$records = $this->selectRecords();
$count = count($records);
In The first function itself you can get the count using $query->num_rows() function
public function selectRecords() {
$return = array();
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
$return['count'] = $query->num_rows();
$return['records'] = $query->result_array();
return $return;
}
try this
it will help you to provide pagination for records
public function selectRecords($params = array(), $count = false) {
$offset = isset($params['offset']) ? $params['offset'] : '';
$limit = isset($params['limit']) ? $params['limit'] : '';
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
if ($count) {
return $this->db->get()->num_rows();
}
if (empty($offset) && !empty($limit)) {
$this->db->limit($limit);
}
if (!empty($offset) && !empty($limit)) {
$this->db->limit($limit, $offset);
}
$result = $this->db->get()->result();
return $result;
}

Json returns a duplicate value using codeigniter

I am trying to fetch a data for each chkey with a related value as a group in codeigniter. When there are a 2 or more chkey then code is work properly.
But when there is a only 1 chkey then it shows value of that chkey but it is shows extra chkey:null,value:null in json.
Related json for chkey is 1 is as follows'
[{"unit_id":"8","CHGRAPHUpdatetime":{"time":"2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00"},"channelGraph":[{"chkey":"ch1","list":"2,-30,12,20"},{"chkey":"null","list":"null"}]}]
Expected is,
[{"unit_id":"8","CHGRAPHUpdatetime":{"time":"2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00"},"channelGraph":[{"chkey":"ch1","list":"2,-30,12,20"}]}]
Could you please help me to resolve this issue.
model code-
public function chGraph($unitid){
$this->db->select("unit_id");
$this->db->from("device_unit");
$this->db->where('unit_id', $unitid);
$query = $this->db->get();
$unit = $query->result_array();
$j = 0;
foreach($unit as $row) {
$unit[$j]['CHGRAPHUpdatetime'] = $this->UnitCHGRAPHUpdatetime($row['unit_id']);
$unit[$j++]['channelGraph'] = $this->UnitCHGRAPHDetails1($row['unit_id']);
}
return $unit;
}
public function UnitCHGRAPHDetails1($unit_id)
{
$this->db->select('distinct(chkey)','chvalue');
$this->db->from('channel_info');
$query = $this->db->get();
$channelgraphData = $query->result_array();
$m = 0;
foreach($channelgraphData as $row) {
$chvalueQuery = 'SELECT chkey, GROUP_CONCAT(chvalue)as list FROM channel_info where chkey= "'. $row['chkey'] .'" and unit_id='. $unit_id .'';
$response = $this->db->query($chvalueQuery)->row();
$channelgraphData[$m++] = $response;
}
return $channelgraphData;
}
controller code-
public function chGraph()
{
$unitid = $this->uri->segment('3');
$GraphDetails = $this->device_model->chGraph($unitid);
echo json_encode($GraphDetails);
}
You could add a GROUP BY at the end of the statement to eliminate the null results :
$chvalueQuery = 'SELECT chkey, GROUP_CONCAT(chvalue)as list FROM channel_info where chkey= "'. $row['chkey'] .'" and unit_id='. $unit_id .' GROUP BY chkey';

ERROR Object of class CI_DB_mysqli_result could not be converted to int

I want to get value from my SQL and then show it to my view and still getting error could not be converted to int
public function v_tambah_barang_dipinjam() {
$nama_barang['nb'] = $this->input->post('nama_barang');
$jsa = $this->db->query("SELECT jumlah_barang FROM stok WHERE nama_barang='$nama_barang'");
$data['stok'] = $this->M_admin->lihat_barang()->result();
$this->load->view('v_tambah_barang_dipinjam', $data + $nama_barang + $jsa);
}
I think you're trying to add your mysqli result object $jsa on this line so php is trying to convert it to an int.
$this->load->view('v_tambah_barang_dipinjam', $data + $nama_barang + $jsa);
You should pass $data only. And this is invalid $data + $nama_barang + $jsa
. You cannot add this because $data is not an int.
Change your code from this:
$this->load->view('v_tambah_barang_dipinjam', $data + $nama_barang + $jsa);
To this:
$this->load->view('v_tambah_barang_dipinjam', $data);
Whole Code
public function v_tambah_barang_dipinjam() {
$nama_barang['nb'] = $this->input->post('nama_barang');
$jsa = $this->db->query("SELECT jumlah_barang FROM stok WHERE nama_barang='$nama_barang'")->result();
//This will check first if $jsa is empty
if(!empty($jsa)){
$jsa = $jsa[0]->jumlah_barang;
}else{
$jsa = '';
}
$data['stok'] = $this->M_admin->lihat_barang()->result();
$data['nama_barang'] = $nama_barang;
$data['jsa'] = $jsa;
$this->load->view('v_tambah_barang_dipinjam', $data);
}
Just call $nama_barang $jsa in your view

Undefined Offset: 0 in Codeigniter

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;
}

Query Improvement: foreach

I have the following model function and I was wanting to know how I could improve it and also pull the needed $row out when needed so that I dont get a PHP error.
How I am pulling the required data:
$data['companyName'] = $this->core_model->companyDetails('coreCompanyName');
Errors:
Undefined property: stdClass::$coreContactName
Undefined property: stdClass::$coreContactEmail
Model:
function companyDetails()
{
$this->db->select('coreCompanyName, coreContactName, coreContactEmail');
$this->db->from('core');
$whereData = array('coreCompanyName' => $companyName, 'coreContactName' => $companyContactName, 'coreContactEmail' => $companyContactEmail);
$this->db->where($whereData);
$query = $this->db->get();
$result = '';
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$result .= $row->coreCompanyName;
$result .= $row->coreContactName;
$result .= $row->coreContactEmail;
}
}
return $result;
}
It should be like this
$this->db->select('coreCompanyName, coreContactName, coreContactEmail');
not
$this->db->select('coreCompanyName','coreContactName','coreContactEmail');
See reference http://codeigniter.com/user_guide/database/active_record.html#select
To limit to a specific set of records, use the where() function. e.g.
$this->db->where('companyId', $companyId);
<?php
/**
*
* WHERE ARE : $companyName, $companyContactName, $companyContactEmail being defined ?
* WHY return 'companyName' WHEN You are quering based on the '$companyName' ???
* Your query wil fail with out it, that that's all your using ?
*
*/
function companyDetails() {
$query = $this->db->query("SELECT companyName FROM core WHERE coreCompanyName = ? AND coreContactName = ? AND coreContactEmail = ? LIMIT 1",
array($companyName, $companyContactName, $companyContactEmail));
return ($query->num_rows() == 1) ? $query->row()->companyName : FALSE;
}
Change:
$this->db->select('coreCompanyName','coreContactName','coreContactEmail');
To:
$this->db->select('coreCompanyName, coreContactName, coreContactEmail');
Now, you said this pulls all the data, but you aren't actually filtering the result. I wonder what the required data is

Categories