Geting a maximum value in code iginiter function - php

I'm using this function to recover a maximum value in a table in code igniter. But it's returning no value. Actually it's recovering a null value from the table, and I think it is not dealing with a variable as integer. Here is my code:
public function MaxWork($n)
{
$this->db->select_max('w_no');
$this->db->where('n_no', $n);
$query = $this->db->get('t_detail');
foreach ($query->result() as $row)
{
$no = $row->w_no;
}
if($no == '')
$no = 1;
return($no);
}
}

public function MaxWork($n)
{
$this->db->select_max('w_no');
$this->db->where('n_no', $n);
$this->db->group_by('n_no');
$query = $this->db->get('t_detail');
if($query->num_rows() > 0)
return $query->row()->w_no;
else
return 1; // default 1 as per your requirement for null records
}
no need for foreach

Related

How to check 2 values in 1 table using PHP, jQuery?

Is there a way to check 2 exists values in 1 table ?
It was work just fine when i use only 1 value 'beezName' but when i added another value 'divId' it didn't work, they only check whether the 'beezName' or 'divId' exist in table. I'm trying to get :
if beezName and divId exist then return true else false
How can i make this work? Thanks in advance
Here's my controller : Beez
function checkBeezExists()
{
$beezId = $this->input->post("beezId");
$beezName = $this->input->post("beezName");
$divId = $this->input->post("divId");
if(empty($beezId)){
$result = $this->beez_model->checkBeezExists($beezName, $divId);
} else {
$result = $this->beez_model->checkBeezExists($beezName, $divId, $beezId);
}
if(empty($result)){ echo("true"); }
else { echo("false"); }
}
And this is my model : beez_model
function checkBeezExists($beezName, $divId, $beezId= 0)
{
$this->db->select("beezName, divId");
$this->db->from("tbl_beez");
$this->db->where("beezName", $beezName);
$this->db->where("divId", $divId);
$this->db->where("isDeleted", 0);
if($beezId != 0){
$this->db->where("beezId !=", $beezId);
}
$query = $this->db->get();
return $query->result();
}
You want to check beezName and divId exist but in your model you have wrong condition for beezId
try below code:
function checkBeezExists($beezName, $divId, $beezId= 0)
{
$this->db->select("beezName, divId");
$this->db->from("tbl_beez");
$this->db->where("beezName", $beezName);
$this->db->where("divId", $divId);
$this->db->where("isDeleted", 0);
if($beezId != 0){
$this->db->where("beezId", $beezId); //<--------remove !=
}
$query = $this->db->get();
return $query->result();
}

Codeigniter returns wrong "pagination" results

I have this helper method which job should be to prepare pagination data in order to retrieve it on controller...
Basically this is the code which happens in helper
if (!isset($_GET['page'])) {
$_GET['page'] = 1;
}
if (!isset($_GET['per_page'])) {
$_GET['per_page'] = 5;
}
$results = $ci->$model->get('', $_GET['per_page'], $_GET['page']);
And this is my model which should return data
public function get($tableName = "", $limit = null, $start = null)
{
if ($tableName == "") {
$tableName = $this->table;
}
if ($limit != null && $start != null) {
// problem is here with limit and start which returns wrong data
$this->db->limit($limit, $start);
$query = $this->db->get($tableName);
var_dump($query->result());
die();
}
} else {
$query = $this->db->get($tableName);
}
return $query->result();
}
Problem is that data returned from model isn't correct and i can't figure out how to get properly data based on page number and items per page....
So in my case if i request data with paramas $_GET['page'] = 1 and $_GET['per_page'] = 5 it will return 5 records, but starting with record 2 till record 6. So my question is how to properly request give me let say 5 records on starting page 1 and then give me another 5 records on page 2 ETC....
If you need any additional information please let me know and i will provide. Thank you
The problem lies within your $start variable. You should remember that when using getting the first the 5 records, you should use an offset 0 instead 1. Counting starts from 0 remember :)
The code should be
public function get($tableName = "", $limit = null, $start = null)
{
if ($tableName == "") {
$tableName = $this->table;
}
if ($limit != null && $start != null) {
// problem is here with limit and start which returns wrong data
//$this->db->limit($limit, $start);
// use this instead
$this->db->limit($limit, ( $start - 1 ) * $limit );
$query = $this->db->get($tableName);
var_dump($query->result());
die();
}
} else {
$query = $this->db->get($tableName);
}
return $query->result();
}
This is working example try to do like this: https://www.formget.com/pagination-in-codeigniter/

Call MySql Select Query in 2 different php function

Please help me with my problem.. can i call once Mysql Select Query from different function from two different function too... Sorry i don't know how to explain.. but below my sample what i want to archive..
I want use single query for 2 function.. so maybe i have to write code like below?
function sqlSelect ($db, $id, $id2) {
$sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
$row = mysqli_num_rows($sql);
$field = mysqli_fetch_object($sql);
return $row;
}
but how to use $field inside 2 diff function?
function aaa ($a,$b,$c) {
if($row >= 1){
//Do something with $a $b and $c
//$reget field with $field['column'];
$field['column']; //???
}
return $result;
}
function bbb ($a,$b,$c) {
if($row >= 1){
//Do something with $a $b and $c
//get field with $field['column'];
$field['column']; //???
}
}
what i do right now is
function aaa ($db,$id,$id2,$a,$b,$c) {
$sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
$row = mysqli_num_rows($sql);
$field = mysqli_fetch_object($sql);
if($row >= 1){
//Do something with $a $b and $c
//get field with $field['column'];
}
}
function bbb ($db,$id,$id2,$a,$b,$c) {
$sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
$row = mysqli_num_rows($sql);
$field = mysqli_fetch_object($sql);
if($row >= 1){
//Do something with $a $b and $c
//get field with $field['column'];
}
}
But if i use what i write and code right now i think it's have to call same query twice. I cannot use query outside function so i have to write query on each function but i want to just write query once and can use in two different function..
Sorry for stupid explanation..
Thank you for help
One implementation can be as follow by passing an array as argument,
<?php
$arg['db']="database";
$arg['tabe']="table";
$arg['search']['id1']="id1";
$arg['search']['id2']="id2";
$arg['do_something']['a']="a";
$arg['do_something']['b']="b";
$arg['do_something']['c']="c";
function searchAndDoSomethingAndReturnResult($arg)
{
$return = NULL;
$query="SELECT * FROM ".$arg['table'];
$flag=false;
foreach($arg['search'] as $key=>$value)
{
if($flag)
$query.=" AND ";
else
$flag=true;
$query.= $key." = '".$value."' ";
}
$row = mysqli_num_rows($query);
$field = mysqli_fetch_object($query);
if($row >= 1)
{
foreach($arg['do_something'] as $job=>$value)
{
// $return[] = "some result"
// do something
}
}
return $return;
}
?>
let me know if this solve your problem
You can either pass in the $field variable as a function argument
<?
$field = ...;
function sql1($field,...,...){
// Use $field here
}
function sql2($field,...,...){
// Use $field here
}
?>
Alternatively, you can use the global keyword to access variables from outside of the function
<?
$field = ...;
function sql1(){
global $field;
// Use $field here
}
function sql2(){
global $field;
// Use $field here
}
?>

Recursive function return null value [duplicate]

This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
I have Used recursive function to get the category full path like Access Control/CARDS/FOBS from above table structure but my recursive function return null value.
function xyz($id,$parent) {
if($parent == '0') {
//my code working fine
//return
}
else
{
$catid = $id; //here 25 coming
$cat_array = array();
$category_array = $this->Recursive($catid,$cat_array);
//echo $category_array;exit; getting Null result
return $category_array ;
}
}
function Recursive($catid,$cat_array)
{
$sql = mysql_query("Select bg_category_id,parent_id,title from categories_list
Where bg_category_id = '".$catid."'");
$result = mysql_fetch_array($sql);
array_push($cat_array,$result['title']);
if($result['parent_id'] != '0') {
$this->Recursive($result['parent_id'],$cat_array) ;
} else {
if(count($cat_array) > 0){
$k = implode("/",$cat_array);
}
//echo $k;exit; getting desired result FOBS/CARDS/Access Control
return $k;
}
}
You need to return the recursive function when you recurse, otherwise it will return nothing.
if($result['parent_id'] != '0') {
return $this->Recursive($result['parent_id'],$category_array) ;
}

Codeigniter active record statement not running when sql query doesn't return anything

I seem to be having a problem with the below code. I cannot get the insert query to run when the query returns nothing. But when there is a row in the db, the else statement runs correctly. Does anybody know why this could be happening?
$query5 = $this->db->get_where('loggedstatus', array('userid_loggedstatus' => $userid));
if ($query5->num_rows() < 0) {
$data1 = array('isLoggedIn_loggedstatus' => 'yes', 'userid_loggedstatus' => $userid);
$this->db->insert('loggedstatus', $data1);
} else {
$data = array('isLoggedIn_loggedstatus' => 'yes');
$this->db->where('userid_loggedstatus', $userid);
$this->db->update('loggedstatus', $data);
}
Have you tried changing this if ($query5->num_rows() < 0) { to something like if ($query5->num_rows() <= 0) {, just a thought. It would make a difference because your telling it to only execute if its less than zero however it might be equal to zero and you would skip to the else statement.
And just for CodeIgniter num_rows() reference:
/**
* Number of rows in the result set
*
* #return int
*/
public function num_rows()
{
if (is_int($this->num_rows))
{
return $this->num_rows;
}
elseif (count($this->result_array) > 0)
{
return $this->num_rows = count($this->result_array);
}
elseif (count($this->result_object) > 0)
{
return $this->num_rows = count($this->result_object);
}
return $this->num_rows = count($this->result_array());
}
Changing if condition shown below should fix the problem.
if ($query5->num_rows() == 0)

Categories