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();
}
Related
I am using CodeIgniter, I am returning more than one value in the model so I did like
return array('true' =>1,'customer_id' => $result->customer_id);
and In the controller, I am displaying like
$true=$result['true'];
$custId=$result['customer_id'];
So there is no issue in this.
Now let's talk about in details
In the model, I have this logic
//sql query
if ($result) {
if ($result->password_change == 0) {
return array('true' =>1,'customer_id' => $result->customer_id);//multiple returns
}
else{
return $result;
}
}
else{return false;}
In the controller
$result=$this->Member_model->check_data(parameters);
$true=$result['true'];
$custId=$result['customer_id'];
if ($result) {
if($true == 1){
//some code here
}
else{
//some code here
}
}
else{
}
This is my second code.
if ($result) {
if ($result - > password_change == 0) {
if ($result - > id == $id) {
return array('true' => 1, 'customer_id' => $result - > customer_id); //return more then 2
} else {
return false;
}
} else {
if ()) // some condition
{
return (array) $result;
} else {
return false;
}
}
} else {
return false;
}
I am getting the error that
"Message: Cannot use object of type stdClass as array"
Because when returning if condition(from the model) then it's working but when it returns else (I mean $result from the model) then I am getting the error because it is not getting the $result['true'].
Hope you can understand my issue. Would you help me out on this issue?
You have two options. Either: ALWAYS return an array, or return an ArrayObject:
Alway return an Array
In your else, cast $result as an array:
//sql query
if ($result) {
if ($result->password_change == 0) {
return array('true' =>1,'customer_id' => $result->customer_id);//multiple returns
}
return (array) $result;
}
return false;
Always return an ArrayObject
Not knowing if this model is returning the result anywhere else in you application, I have no idea if other code expects $result to be an object. Casting as an array might break your code elsewhere. Let's convert $result from stdClass to an ArrayObject:
//sql query
if ($result) {
if ($result->password_change == 0) {
return array('true' =>1,'customer_id' => $result->customer_id);//multiple returns
}
return new \ArrayObject($result, \ArrayObject::ARRAY_AS_PROPS);
}
return false;
This approach will allow you to call $result['test'] AND $result->test.
Yeah, ok this is solution for multiple conditions :
$this->db->select('*'); // Here your column name
$this->db->from('members'); // Table name
$this->db->where($login_access); // Here your condition
$query = $this->db->get();
$result = $query->result_array();
$query = $this->db->get('customer'); // Your code above if condition
$result = $query->result_array(); // Get data in array Format using **result_array()**
if ($result) {
if ($query->num_rows() > 0) { // You have to change condition from **$result['password_change ']** To **$query->num_rows() > 0**
if ($result - > id == $id) { // Also change here condition from **$result - > id** To $result['id']
return array('true' => 1, 'customer_id' => $result['customer_id']); //Also change here from **$result - > customer_id** To $result['customer_id']
} else {
return false;
}
} else {
if () // some condition
{
return $result; // Also Change here from **return (array) $result** To $result
} else {
return false;
}
}
} else {
return false;
}
This question already has answers here:
getting the value of the single field output using the codeigniter active record
(5 answers)
Closed 2 years ago.
I am working with CodeIgniter and I want to get the highest ID out of a table. This i realize with a function in the Model. But I don't get the result, which is a single value, as value back.
Code:
function get_highest_answer_id(){
$this->db->query('SELECT MAX(id) AS answerid FROM pa_it_answer;');
if ($query = $this->db->get()) {
if ($query->num_rows() > 0) {
return $query->result();
} else {
return array();
}
} else {
return FALSE;
}
}
How do I have to Change the code that when I call this function in my Controller I get the single value?
Below code is works fine for me.
$MAXID = 0;
$row = $this->db->query('SELECT MAX(id) AS answerid FROM pa_it_answer')->row();
if ($row) {
$MAXID = $row->answerid;
}
Change your query in codeigniter like this
function get_highest_answer_id(){
$this->db->select('MAX(id) AS answerid');
if ($query = $this->db->get('pa_it_answer')) {
if ($query->num_rows() > 0) {
return $query->result();
} else {
return array();
}
} else {
return FALSE;
}
}
You just have to simplify your method with row()
function get_highest_answer_id(){
$record = $this->db->query('SELECT MAX(id) AS answerid FROM pa_it_answer;')->row();
if ($record) {
return $record->answerid;
}
else {
return FALSE;
}
}
Try this:
function get_highest_answer_id(){
$this->db->query('SELECT MAX(id) AS answerid FROM pa_it_answer;');
if ($query = $this->db->get()) {
if ($query->num_rows() > 0) {
return $query->row(); // here!!!
} else {
return array();
}
} else {
return FALSE;
}
}
I recommended use the querybuilder of CI (EDITED)
$this->db->select_max('id', 'answerid');
$query = $this->db->get('pa_it_answer');
var_dump( $query );
var_dump( $query->answerid );
die();
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/
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)
This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
I have below recursive function in my model with CI, working.
In my controller, i need to check if function worked correctly like:
if($this->my_model->level_corrector($id_page,$level)) echo 'Levels are corrected';
But as the function always return false (to end the recursion), I couldn't figure out how to achieve my goal.
function level_corrector($id_page_of_parent,$level_of_parent)
{
$sql = "
SELECT id_page, id_parent, level
FROM page
WHERE id_parent = $id_page_of_parent";
$query = $this->db->query($sql);
if($query->num_rows() > 0)
{
$result = $query->result_array();
foreach ($result as $r)
{
$data = array('level'=>$level_of_parent+1);
$this->db->where('id_page', $r['id_page']);
if($this->db->update('page', $data))
{
$this->level_corrector($r['id_page'],$level_of_parent+1);
}
else
{
// let me handle it what to do
return false;
}
}
}
else
{ // again let me handle it to log a message or sth
return false; // (2)
}
return true; // (3) means it all gone right, so I can move on.
}
You need to do two things:
If there is an error, return false. Otherwise, at the end of the function, return true by default
If, when you call the function recursively, an error occurred, return false.
Edit: Based on your answers to my questions in the comments, what you want is this:
function level_corrector($id_page_of_parent,$level_of_parent)
{
$sql = "
SELECT id_page, id_parent, level
FROM page
WHERE id_parent = $id_page_of_parent";
$query = $this->db->query($sql);
if($query->num_rows() > 0)
{
$result = $query->result_array();
foreach ($result as $r)
{
$data = array('level'=>$level_of_parent+1);
$this->db->where('id_page', $r['id_page']);
if($this->db->update('page', $data))
{
// no error. return error code from recursive call
return $this->level_corrector($r['id_page'],$level_of_parent+1);
}
else
{
// error occured
return false;
}
}
}
// $query->num_rows <= 0. This is not an error, so return true:
return true;
}