Using where condition getting blank page in codeigniter mysql - php

Hi i have used where condition in mysql after using that if i run the website getting the blank page.
Actually i am three department like QA Department,Development,Management.
IN Mysql i have used where condition to get only QA department list but getting blank page.
Here is my code:
Controller:
public function index()
{
$data['records2'] = $this->index_model->get_ourteammembers();
$data['mainpage'] = "our-team";
$this->load->view('templates/template',$data);
}
Model
function get_ourteammembers()
{
$this->db->select('T.*');
$this->db->from('ourteam AS T');
$this->db->where('T.department'='QA Department');
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}

Try This
function get_ourteammembers()
{
$this->db->select('T.*');
$this->db->from('ourteam AS T');
$this->db->where('T.department','QA Department');
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}

As Per CodeIgniter Document you have to use ',' instead of '='
$this->db->where('T.department'='QA Department');
To
$this->db->where('T.department','QA Department');

Related

Join 3 tables with codeigniter and display it

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
}
}

How to get all the data from database in session after getting logged-in in CodeIgniter?

I want to display all the data of a user from db by using session.
<?php
class login_model extends CI_model
{
function Can_login($email,$password)
{
$this->db->select('*');
$this->db->where('email',$email);
$this->db->where('password',$password);
$query = $this->db->get('registration');
if($query->num_rows() > 0)
{
return true;
}
else
{
return false;
}
}
}
?>
You should return actual data from model function
<?php
function Can_login($email,$password)
{
$query = $this->db
->where('email',$email)
->where('password', $password)
->get('registration');
if($query->num_rows() > 0)
{
return $query->result(); //or result_array() or other type
}
return false;
See documentation on Generating Query Results for details on the types of returns available.
You can do it the way you are doing it above but with a few changes
<?php
class login_model extends CI_model
{
function Can_login($email,$password)
{
$user = $this->db->select('*')
->where(['email'=>$email, 'password'=>$password])
->get('registration')->row();
//dont need to check for row, if user is not found it will be empty and trigger false part of your statement
//could even just return user in general and do this logic in controller
if($user)
{
$_SESSION['user'] = $user
return true;
}
else
{
return false;
}
}
}
?>

Codeigniter JOIN multiple tables

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();
}

How can i make to get not only by id but by one more column also

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

how can i fetch data from mysql database and display it to view using codeigniter .

hey guys i have a table in database and i want to retrieve data from it and display to view . . i have tried many methods but none of these working . . pls help . . this is my controller code:
function getname()
{
$this->load->model('user'); //load the user class
$data['member'] = $this->user->ar_getwhere();
$this->load->view('home_content_view', $data);
}
this is my model:
function ar_getwhere()
{
$this->db->select('');
$this->db->from('tbl_members');
$this->db->where('member_name',$this->input->post('member_name'));
$q = $this->db->get('');
if($q->num_rows() > 0)
{
$data = array();
foreach($q->result() as $row)
{
$data=$row;
}
return $data;
}
}
I don't know what errors you're experiencing but anyway
You are receiving post data in the model? You should receive post data in the controller and send it to the model, and then, catch its response... And you should send the whole response as well.
function getname()
{
$this->load->model('user'); //load the user class
$data['member'] = $this->user->ar_getwhere($this->input->post('member_name'));
$this->load->view('home_content_view', $data);
}
function ar_getwhere($memberName)
{
$this->db->select('*');
$this->db->from('tbl_members');
$this->db->where('member_name',$memberName);
$q = $this->db->get();
if($q->num_rows() > 0)
{
return $q->result();
}
}
$q = $this->db->select('*')->from('tbl_members')
->where('member_name', $this->input->post('member_name'))
->get();

Categories