I'm new in CI and I want to fetch data from DB using codeigniter in MVC way.
I can't seem to find any solution.
model: company.php | dbname: company
public function getAllcompany(){
$query = $this->db->get('company');
if($query->num_rows() > 0)
{
return $query->result();
}
return array();
}
view: branch.php
<?php
echo "<pre>", print_r($records), "</pre>";
?>
controller: home.php
public function getCompany(){
$this->load->model('company','', TRUE);
$data['records'] = $this->company->getAllcompany();
$this->load->view('branch', $data);
}
You will have to change your model to :
public function getAllcompany(){
$query = $this->db->get('company');
return $query->result();
}
OR
public function getAllcompany(){
$query = $this->db->get('company');
return $query->result_array();
}
Related
I have a problem with my model. It's returning empty data.
Here is the controller.
public function getReport() {
$data = array();
$id = $this->input->post('id');
$this->auth->setId($id);
$data['instant_main']=$this->auth->getReportData();
$this->load->view('auth/report-item', $data);
}
and this is my Model
class Auth_model extends CI_Model {
// Declaration of a variables
private $_id;
public function setId($id) {
$this->_id = $id;
}
function getReportData()
{
$this->db->select('*');
$this->db->from('inbound_main');
$this->db->join('inbound_item', 'inbound_main.id = inbound_item.report_id');
$this->db->where('inbound_item.report_id', $this->_id);
$query = $this->db->get();
return $query->result();
}
}
If I have removed the WHERE condition from the query, everything was fine.
Your $this_id is empty..
You have folow the code like this
Controller
$data['instant_main']=$this->auth->getReportData($id);
Model
function getReportData($id)
{
$this->db->select('*');
$this->db->from('inbound_main');
$this->db->join('inbound_item', 'inbound_main.id = inbound_item.report_id');
$this->db->where('inbound_item.report_id', $id);
$query = $this->db->get();
return $query->result();
}
I want to create a join database. in the 'relation' table will show 'disease' which can add many 'symptoms'. How should I do it?
Here is my database:
disease : id_disease, name, details
symptoms : id_symptoms, name, details
relation : id_relation, id_disease, id_symptoms
and here is my model
function get_allrelation() {
$this->db->select('*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relastion.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms= relation.id_symptoms','left');
$query = $this->db->get();
return $query->result();
}
}
Please help me.
You had a typo: instead of relation you had relastion.id_disease.
Code below should work
function get_allrelation() {
$this->db->select('*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relation.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms= relation.id_symptoms','left');
$query = $this->db->get();
return $query->result();
}
The controller below is a example only in the select $this->db->select('relation.*, disease.*, symptoms.*');
Oon the controller you can pass the data to the view the create a foreach loop in the view.
<?php
class Somecontroller extends CI_Controller {
public function index() {
$data['allrelation'] = $this->allrelation();
$this->load->view('example', $data);
}
function get_allrelation() {
$this->db->select('relation.*, disease.*, symptoms.*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relastion.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms = relation.id_symptoms','left');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
}
return false
}
}
I'm new in codeigniter, I'm having a challenge with num_rows on view page
Here's the sample of the code - Updated
Model
public function __construct()
{
parent::__construct();
}
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}
public function index()
{
$this->load->helper('url');
$this->load->view('referrals_form_view');
}
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
}
View
<span><?php
echo $query->num_rows();
?></span>
when i run the code, it tell me undefined variable "query"
Please help.
Thank you
Please change your code as below.
MODEL:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
View :
<span>
<?php
echo $total_referred_in;
?>
</span>
As you can see in Controller line :
$this->load->view("referral_form_view", $data);
$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").
You aren't returning anything from your function, you're just echoing. Try this:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller:
public function referral_in()
{
$data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
And then in your view:
<span><?=$num_rows?></span>
I'm having a little trouble in retrieving data in multiple tables using codeigniter.
This is the code i'm using to retrieve data in my model which is working well.
function retrieve_experience($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
function retrieve_education($alumni_id)
{
$this->db->select('*');
$this->db->from('education');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
Now i tried using a simplified code but fails to display the data. here is the code in my model
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->join('education','education.alumni_id=experience.alumni_id');
$this->db->where('experience.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}
In my controller, i used this code to retrieving data in my model
function display()
{
$alumni_id = $this->session->userdata('alumni_id');
$data['all_data'] = $this->Alumni_model->retrieve_all_data($alumni_id);
$data['main_content'] = 'alumni_home';
$this->load->view('includes/template', $data);
}
and for the display i used this code
foreach($all_data as $results)
{
/** data from experience table **/
$results['company_name'];
$results['company_address'];
/** data from education table **/
$results['school_name'];
$results['field_of_study'];
}
I cant display anything at all. Please help
Try the below code,
I believe you'll want something like this:
function retrieve_all_data($alumni_id)
{
$this->db->select("e.*,edu.*");
$this->db->from("experience e");
$this->db->join("education edu", "edu.alumni_id = e.alumni_id",'left');
$this->db->where('e.alumni_id',$alumni_id);
$this->db->group_by('e.exp_id');
$query = $this->db->get();
return $query->result_array();
}
Hope below mentioned function should return data that you expected.
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience ex');
$this->db->join('education ed','ed.alumni_id=ex.alumni_id');
$this->db->where('ex.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}
this my
model
public function get_news($NewsId= FALSE)
{
if ($NewsId === FALSE)
{
$this->db->select('*');
$this->db->from('news');
$this->db->where('Status' , 1);
$this->db->where('Language' , 2);
return $query->result_array();
$config['per_page']; $this->uri->segment(3);
}
$query = $this->db->get_where('news', array('NewsId' => $NewsId));
return $query->row_array();
controller
public function single($NewsId)
{
$data['news_item'] = $this->news_model->get_news($NewsId);
if (empty($data['news_item']))
{
show_404();
}
$data['NewsId'] = $data['news_item']['NewsId'];
$this->load->view('news/single', $data);
}
view
<?php echo $news_item['TittleNews'];?>
i have other view that foreach all my news table and i want create read more the i have created on up my code it works fine
want to make read more with NewsId and one other column it mean first check the NewsId and then other colum and then show me on my view which is singly
you can use a model method like this:
function getBy($data){
foreach($data as $key=>$value){
$this->db->where($key,$value);
}
$query = $this->db->get('users_table');
return $query->row(); //or $query->result(); as you want
}
so then you call it with
$this->model->getBy(
array(
'username'=>'Mark',
'id'=>'90',
'email'=>'asd#asd.com'
);
);
obviously you can then extend the model method as much as you can