how to store database query result in a variable in view - php

I want to store my database query result in my view section in a variable.I am trying but not work.
My View code
$cd=''.base_url().'/video/series/';
$count = count($cd);
for ($i = 0; $i < $count; $i++)
{
print'"'.$cd[$i][1].'",';
}
My Controller code
public function series() {
$result= $this->video_model->series_list();
return $result;
}
My Model Code
function series_list()
{
$string = trim($this->input->get_post('term'));
$query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");
return $query->row_array();
}
$cd=''.base_url().'/video/series/'; not get any array data ,only get blank data

You can get post variable value in controller only. But you trying to get it in model. That's wrong.
Controller:
public function series() {
$string = trim($this->input->get_post('term'));
$data['result']= $this->video_model->series_list($string);
$this->load->view('folder/filename', $data);
// in your case i think folder= video and filename = series
// in this way you can pass value from controller to view
}
Model:
function series_list($string = null)
{
if($string != ''){
$query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");
return $query->row_array();
}
else
return false;
}
View:
<?php
var_dump($data);
?>
you can get your resultset in view and can play with it as you want.

You need to pass variable inside some array(in view method) so that all variables inside common array would be accessible on view page
Controller code would be
public function series()
{
$inputData =trim(strip_tags($this->input->get_post('term')));
$data['result']= $this->video_model->series_list($inputData);
$this->load->view('directory/viewpage',$data); //$data is array with all variables inside it
}
Model code would be
function series_list($data)
{
$string = $data;
$query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");
if( $query->num_rows>0)
{
return $query->row_array();
}
else
{
return false;
}
}
View code would be
<?php
if(isset($result)&&($result!=''))
{
echo "<pre/>";
print_r($result); // Or var_dump($result);
}
?>

Related

how can we use where condition if data is stored as implode

Here my table looks like this
i want to get all the departments if location_id is 3 that means if location id 3 is used i need to get the department_names of cardiology and hair so that i had done my code like this
My controller looks like this
public function get_location_departments()
{
$d = $_POST['location'];
$data['departments'] = $this->Hospital_model->get_location_departments($d);
$this->load->view('frontend/ajax_get_departments',$data);
}
My model looks like this
public function get_location_departments($d)
{
$this->db->where_in('location_id',$d);
$query=$this->db->get('department')->result();
return $query;
}
Please help me to solve my problem
Hope this will help you :
You have to query first to get the department and get location_id into an array using explode
Your model get_location_departments method should be like this :
public function get_location_departments($d)
{
if (! empty($d))
{
$query = $this->db->get('department');
if ($query->num_rows() > 0 )
{
foreach ($query->result() as $location)
{
$location_ids = explode(',',$location->location_id);
if (in_array($d, $location_ids))
{
$data[] = $location;
}
}
}
return $data;
//print_r($data);
}
}
imho the correct answer would be
public function get_location_departments($d)
{
$query = $this->db
->where('FIND_IN_SET('.$this->db->escape($d).', location_id)', NULL, false)
->get('department')
->result();
return $query;
}
There is a function called FIND_IN_SET for that purpose. You can find it in the mysql Documentation under https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set

Invalid argument supplied Codeigniter

My program is not working properly, i do not know what should i do :S
I got this error message:
Take a look at this:
Here is my code:
My controller file (Home):
<?php
class Home extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("Crudmodel");
}
public function index(){
# get all data in Study table
$selectStudys = $this->Crudmodel->selectStudys();
foreach ($selectStudys as $key => $study)
{
# get UserNames
$user = $this->Crudmodel->getName($study['user_id']);
#get Subject Names
$subject = $this->Crudmodel->getSubName($study['subject_id']);
#append both NEW VALUES to same array
if(!empty($user[0]['username'])){
$data[$key]['user_id'] = $user[0]['username'];
// your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
}else{
$data[$key]['user_id'] = ''; // or anything as your else condition you can use as error handler
}
if(!empty($subject[0]['name'])){
$data[$key]['subject_id'] = $subject[0]['name'];
// your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
}else{
$data[$key]["subject_id"] = "";
// or anything you can use as error handler
}
}
$data['records'] = $selectStudys;
$this->load->view('home', $data);
}
}
?>
Crudmodel:
class Crudmodel extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
function selectStudys()
{
$query= $this->db->query("SELECT * FROM cursadas");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = "";
// or anything you can use as error handler
return $result;
}
}
function getName($name)
{
$query= $this->db->query("SELECT username FROM usuarios WHERE id = $name ");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = "";
// or anything you can use as error handler
return $result;
}
}
Dont know what to do now :(
Hope you can help me :S
The problem is in the model. You only return something inside the else. Easy fix, move the return.
You should probably return an empty array if there are no rows. Then the foreach will still have something to work with - even if it is empty. foreach will choke if given something that cannot be used in a loop - a string for instance.
function selectStudys()
{
$query= $this->db->query("SELECT * FROM cursadas");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = array();
}
return $result;
}

Cannot use object of type QueryResult as array, Using Salesforce library with CI

I got an error
"Cannot use object of type QueryResult as array"
I am simply trying to take state name from the records array but, I only manage to get this error "Cannot use object of type QueryResult as array"
Model.php
public function getstatename()
{
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
return $statename;
}
Controller.php
public function index() {
$data['getstatename'] = $this->model->getstatename();
$this->load->view('footer', $data);
echo "<pre>";
print_r($data['getstatename']['records']);
}
Actually $statename is result-set object not an array.
So you need to do like below:-
Model.php:-
public function getstatename(){
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
$state_array = array(); // create an array
foreach ($statename as $row){
$state_array[] = $row->State__c; // assign value to array
}
return $state_array; // return array
}
Controller.php:-
public function index() {
$data['getstatename'] = $this->Footer_model->getstatename();
echo "<pre/>";print_r($data['getstatename']); // check this and accordingly change you code further
$this->load->view('footer', $data);
}
Here is the solved code Thanks to #Anant
Model.php
public function getstatename()
{
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
$state_array = array();
foreach ($statename as $row){
$state_array[] = $row->State__c;
}
return $state_array;
}
Controller.php
public function index() {
$data['getstatename'] = $this->Footer_model->getstatename();
$this->load->view('footer', $data);
}
See if you can do
$data['getstatename']->fetchRow();

return multiple values inside function method in codeigniter

This is my query counting rows in tblapplication
public function countallrecord() {
$query = $this->db->get('tblapplication');
return $query->num_rows();
}
and this is the function to get all the data
public function getdata() {
$query = $this->db->get('tblapplication');
return $query->result();
}
Is there any way I can make this code on one function
I'm trying to pass it here:
public function Countandviewallrecord() {
// returns both rows and count
}
Just return it as an array. Include the results and count in their respective indices:
public function get_records()
{
$result = $this->db->get('tblapplication');
$data['results'] = $result->result();
$data['count'] = $result->num_rows;
return $data;
}
When accessing the model method in your controller, the usual:
$data = $this->model_name->get_records();
echo $data['count']; // whatever number this is
if($data['count'] > 0) {
foreach($data['results'] as $row) {
echo $row->column_name; // etc blah blah ..
}
}
this Countandviewallrecord will display data as well count total records
public function Countandviewallrecord(){
$TotalRecords= $this->countallrecord();
$totalData= $this->getdata();
}

multiple delete with checkbox in ci

I know it has already been asked, but I'm not able to solve this. Below is my control module
public function ahome()
{
$this->load->model('model');
$data['user'] = $this->model->selall('user');
$this->load->view('ahome', $data);
if ($this->input->post('del'))
{
$did = $this->input->post('chk');
// print_r($did);
for ($i = 0; $i < count($did); $i++)
{
$multi = $did[$i];
print_r($multi);
$this->model->delall("user", $multi);
// redirect("control/ahome");
}
}
}
and this is my model module
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in('$wh')");
}
now problem is that it's only delete single row not multiple rows
In your query string, try removing the quotes around the $wh variable.
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in ($wh)");
}
That should get it to work; however, you should be using CodeIgniter's query bindings. Something like this:
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in (?)", array($wh));
}
Refer to this answer for more details.
Try this,
controller,
public function ahome()
{
$this->load->model('model');
$data['user'] = $this->model->selall('user');
$this->load->view('ahome', $data);
if ($this->input->post('del'))
{
$did = $this->input->post('chk');
// print_r($did);
foreach ($did as $d_id)
{
$this->model->delall("user", $d_id);
// redirect("control/ahome");
}
}
}
Model:
public function delall($tb,$wh)
{
$this->db->where('id',$wh);
$this->db->delete($tb);
return true;
}

Categories