Model returning null values - php

I'm a new bee to web development. I need to get data (some columns) from the job table and filter it according to the user name in the session and a status value (status is a TINYINT there for contains 0 and 1).
Here is my model:
public function show_work() {
$user_name = $this->session->userdata('Name');
$this->load->database();
$this->db->select('jbStageID, Description, StartDate, SpecialDetails, EstimateTime, Instrauctions');
$this->db->where('Name',$user_name);
$this->db->where('status','0');
$rset=$this->db->get('job');
$result=$rset->result_array();
}
Here is my controller:
public function employees()
{
$this->load->model('show_details');
$result= $this->show_details->show_work();
$data = array();
$data['inbox'] = $this->show_details->show_work();
var_dump($result);
echo "<pre>";
print_r($data);
echo "</pre>";
die();
}
The issue is I don't get values from the database but value null with empty array.
The result is like this:
Array(
[inbox] =>
)

You need to use the return to return the data as below in the model's last line:
$result=$rset->result_array();
return $result;

you missed $this->db->from
that means from table in sql query.

Related

Find values ​in the database starting from a session id

I need display the value in the ga845_clients table ​​in the atacado column, that have 's' or 'n' recorded, but they return blank.
Then when I call <?php echo $this->session->userdata('usu_id');?> in view, the ID is displayed.
I have checked that usu_id is the session ID and the same value from id of table ga845_clientes.
I created the following codes:
Model (Cliente_model.php):
public function getAtacadista($id) {
$this->db->select("atacadista");
$this->db->where('id', $this->session->userdata("usu_id"));
$query = $this->db->get('ga845_clientes');
return $query->result();
}
Controller (Cliente.php):
public function getAtacadista() {
$this->load->model('cliente_model');
$this->load->view("carrinho", $data);
$data['atacadista'] = $this->cliente_model->getAtacadista($id);
}
View (carrinho.php)
echo 'test1:' .$data['atacadista'];
echo 'test2:' .$atacadista;
Remember, $data['atacadista'] dont exists on view, only $atacadista
Also move down the " load view " line, as everton suggested, they are swapped and $data need to be before.
And lastly, as a tip, session userdata can contain a bunch of info, and you should pass the name of the session, not of the value, maybe usu_id is a value inside an array, not the array itself.
You can try the following in your controller
public function getAtacadista() {
$this->load->model('cliente_model');
$data['atacadista'] = $this->cliente_model->getAtacadista($id);
$this->load->view("carrinho", array("data"=>$data));
}
You should place the $data variable assign before the load view.
Because you are not sending your data to the view. change the sequence of your statements.
public function getAtacadista() {
$this->load->model('cliente_model');
$data['atacadista'] = $this->cliente_model->getAtacadista($id);
$this->load->view("carrinho", $data);
}

Before fetching data from DB checking row null or not

I fetching data from DB and passing to Google Map. But sometimes in address table, lat and lng rows are being empty. So if it happens and I use that half empty array Google Map is crushes. So when I fetching data in Controller is there any option like; if row empty go next row... For example:
public function index()
{
$Data = DB::table('allestates')->whereNotNull('lat')->whereNotNull('lng')->get();
return view('home', 'Data');
}
Of course this is not proper code but, I just want to show what I am trying. Is this possible?
You can use whereNotNull:
public function index()
{
$Data = DB::table('allestates')->whereNotNull('lat')->whereNotNull('lng')->get();
if (!empty($Data)){
}
}
The filter you used (where('lat','lng')) actually means "records where the field lat has the value 'lng'".
you can add check if a field is not null by using below code:
$Data = DB::table('allestates')
->whereNotNull('lat')
->whereNotNull('lng')
->get();
if (!empty($Data)){
}

How to add a new key in result of codeigniter model return result()

I have a function in my model that get all company details I am fetch this
I have to add a new key city_name in my return result() how can I add ?? I am very confused but I am not get any useable example.
function xxxx($search=array(),$page,$limit){
$this->db->select("*");
$this->db->from("xxx");
$this->db->join("xx","xx.xx=xxx.xx");
$this->db->limit($limit,$page);
$company_data = $this->db->get();
if($company_data->num_rows()){
return $company_data->result();
}
else{
return 0;
}
}
Change the following line:
return $company_data->result(); // It return Stc Class Object
to
return $company_data->result_array(); // Now it returns an array
Now you can use array_push() function, it inserts one or more elements to the end of an array.
Or simply use:
$company_data['index'] = value;
Keep your model same return $company_data->result().
In your controller, you need to iterate it for the number of results you get and add new key to your objects.(result() method returns Standard Class Object).
function yourControllerMethod()
{
$company_data = $this->your_model->xxx(search_array, $page, $limit);
if(!empty($company_data)
{
foreach($company_data as $cd)
{
$cd->city_name = "pune"; // or anything you want
}
}
var_dump($company_data);
}
Try this to add new key to your results.
Edit:
You asked that you don't have city_name key in your object.
Yes, you don't have. The above code adds a key in your object.
Just like array.
$temp = array();
$temp["id"] = 1;
In this code, initially the array is empty, the next statement add the id as key in the array.

Why my fields are not getting added to the database?

I am trying to add some fields to the column of my table. When I check my table, I see that the column could be created but the fields could not be added. However, I get no error or anything.
public function isset_row($target, $sender_table, $receiver_table, $sender_row) {
$this->load->model('Connection_model');
if ($this->Connection_model->get_custom_db($target)->get($sender_table)) {
// Add column(s)
$this->myforge = $this->load->dbforge($this->Connection_model->get_custom_db('receiver'), TRUE);
$fields = array(
$sender_row => array('type' => 'TEXT')
);
$this->myforge->add_column($receiver_table, $fields);
$query = $this->Connection_model->get_custom_db('sender')->get($sender_table);
foreach ($query->result() as $row) {
echo $row->$sender_row . '<br>'; // Returns fields from a table (string)
echo $receiver_table; // Returns table name (string)
$this->Connection_model->get_custom_db('receiver')->update($receiver_table, $row->$sender_row);
}
}
}
Edit1:
var_dump($this->Connection_model->get_custom_db('receiver')->update($receiver_table, $row->$sender_row));
This line returns bool(false)
Edit2:
With the update() function I am just saying to add the fields to the table but I am not saying to which column of the table. I think that must be the point but how can I specify the column when trying to update?
Complete your model get_custom_db(). Use get() query builder method in model. So your condition should like this
if ($this->Connection_model->get_custom_db($table_name)) {
...
}
and use get() in model
public function get_custom_db($table_name){
....
$test = $this-db->get($table_name);
return $test->result();
}
OR provide the model code, I'll figure out it for you. Thanks

Model on cakephp return me Array

Im working in the model on cakephp with this method
public function updateUserStatus($uus){
if(isset($calor["valor1"])){
$query1= $this->query("SELECT time from users where time = "'.$valor["valor1"].'";");
return "test".$query1;
}
}
But when I return "test".$query1 ; the view show me testArray
My question is How can i Get the value time from the query?
thank for your help
That's because you're concatenating a string with an array, and PHP will change the array to a string, which will be Array. $query1 is an array containing your data, so just do:
return $query1;
And access your fields with the variable that will store the returned value.
If you want 'test' to be associated with $query1, then it's better to:
return array('test' => $query1);

Categories