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
}
}
Related
I need a little guidance here. I'm using Codeigniter 3.
Since it is MVC fw with segment routing, I want to know how is possible to create custom route which will show just a name of record (it can be category, product, post etc..) returned from database instead of id/name when I need id segment to identify which record from database I need to return or preview by that id.
I will post some code example:
Controller
class Categories extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('CategoryM');
}
public function index(){
$data = array(
'category_list' => $this->CategoryM->listAll()
);
$this->load->view('test/category_list', $data);
}
public function preview()
{
$categoryId = $this->uri->segment(2);
$data = array (
'previewByCategory' => $this->CategoryM->previewByCategory($categoryId)
);
$this->load->view('public/preview_by_category', $data);
}
Model
<?php
class CategoryM extends CI_Model {
public function listAll() {
$query = $this->db
->select('*')
->from('categories')
->where('parentid', NULL, TRUE)
->order_by('name', 'asc')
->get();
if($query->num_rows() > 0) {
return $query->result();
}else{
return false;
}
public function previewByCategory($categoryId=''){
$query = $this->db
->select( /* posts and categories data */ )
->from('categories')
->join('posts', 'posts.categoryID = categories.id', 'left')
->where('categories.id', $categoryId)
->get();
if( $query->num_rows() > 0 ){
return $query->result();
}else{
return false;
}
}
}
View
<?php if($category_list):?>
<?php foreach($category_list as $category):?>
<h3>
<a href="<?php echo base_url() . strtolower(url_title($category->name) . '/' . $category->id);?>">
<?php echo $category->name;?>
</a>
</h3>
<?php endforeach;?>
<?php endif;?>
My Routes
// Category routes
$route['categories'] = 'categories/index';
$route['(:any)/(:num)'] = 'categories/preview/$1';
What I want is instead of routes
www.mywebsite/categoryname/categoryid/
to display just
www.mywebsite/categoryname
which will list all products from that category group. But how to do this without id in url. I'm sorry if my question is too broad. Thank's in advance.
Codeigniter does not allow you to get database access in routes.
but you can create the database instance manually.
routes.php
$slug= ($this->uri->segment(1)) ? $this->uri->segment(1) : false;
if($slug){
//include your database
require_once(BASEPATH."/database/DB.php");
$db=& DB();
$category = $this->db
->select('name')
->from('categories')
->where('parentid', NULL, TRUE)
->where('name',$slug)
->get()->row();
if($category){
//$category->name must be unique in database
$route[$category->name] = 'categories/index';
}
}
In Categories controller
class Categories extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('CategoryM');
}
public function index(){
echo $category_name=$this->uri->segment(1);
}
}
URL
www.mywebsite/categoryname
//if categoryname exist in category
output :
categoryname
First you have to add column to your category table say url_title, which is unique and validate as a URI. Then you have to modify your route.php
// Category routes
$route['categories'] = 'categories/index';
$route['(:any)'] = 'categories/$1'; // $1 pass the utl_title value to Categories/preview($url_title)
and your controller
public function preview($url_title=null) {
$categoryId = $this->uri->segment(2);
$data = array (
'previewByCategory' => $this->CategoryM->previewByCategory($url_title)
);
$this->load->view('public/preview_by_category', $data);
}
and your model
public function previewByCategory($url_title=''){
$query = $this->db
->select( /* posts and categories data */ )
->from('categories')
->join('posts', 'posts.categoryID = categories.id', 'left')
->where('categories.url_title', $url_title)
->get();
if( $query->num_rows() > 0 ){
return $query->result();
}else{
return false;
}
}
I hope this would work for you. Please be carefull my posted code will have syntax errors because I did not tested it.
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();
}
I have function to fetch the products from ci_products table
the code is as follows
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$products = $this->db->get();
return $products;
}
now I want to fetch from where user_id is current user_id
I have tried few things but not working
what I have tried`
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$this->db->where('user_id',$this->session->userdata('user_id'));
$products = $this->db->get();
return $products;
}
$data = $this->db->get_where('product_ids',$product_ids);
$this->db->where_in('product_ids',$data);
$this->db->where('user_id',$this->session->userdata('user_id'));
$query = $this->db->get();
return $query->result_array();
Try this one :
In your first code:
function get_product_selections($product_ids)
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('product_id', $product_ids);
$products = $this->db->get();
return $products->result_array();
}
what do you mean when you put array() in the parameter? you want to retrieve an array() of products?
function get_product_selections()
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('user_id',$this->session->userdata('user_id')); // the user who is currently login
$products = $this->db->get();
return $products->result_array(); // return the results
}
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();
}
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