How to use two tables data in a view in Codeigniter - php

I have two tables, first table is about topic name and second table shows sub-topics. I want to show from first table topic name then search their corresponding sub-topic and show it in view and so on till topic names are found in first table. But the problem is i can only get one table data in view i.e. main topic table. I have no idea how to get full data of both table and use it.
Database Structure:
Model:
<?php
class Topics_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
function get_all_topics()
{
$this->load->database();
$this->db->select("*");
$this->db->from("topics");
$query=$this->db->get();
return $query->result();
}
}
?>
Controller :
<?php
class drag_drop_course_material extends CI_Controller {
function drag_drop_course_material()
{
parent::__construct();
}
function index()
{
$this->load->model('topics_model');
$data['query']=$this->topics_model->get_all_topics();
$this->load->helper('url');
$this->load->view('drag_drop_course_material', $data);
}
}
?>
View:
<?php
foreach ($query as $row) {
?>
<li> $row->topic_name </li>
<? } ?>
Required Output:

Try this query. We do left join of both tables.
$CI->db->select('*');
$CI->db->from('topic');
$CI->db->join('sub-topics', 'topic.id = sub-topics.sub_topic_id', 'left');
$query = $CI->db->get();
$result = $query->result();
You will get result in your model. return this result from model and access it in controller. Once you print return result in controller you will get idea that how to render it because you already did it above.

Your controller is perfect, your model is perfect just change your view:
In view:
foreach ($query as $row)
{ ?>
<ul>
<li><?php echo $row['topic_name'];?>
<ul>
<?php $this->db->where('sub_topic_id',$row['id'])
$r = $this->db->get('subtopics');
if($r->num_rows()>0)
{
foreach ($r -> result_array() as $row) {
$data1[] = $row;
}
}
foreach($data1 as $sub){?>
//print <li><?php echo $sub['subtopic_name']?></li>
<? }?>
</ul>
</li>
<? }?>
</ul>

Related

Getting Error : Trying to get property of non object

I am a beginner in the CodeIgniter. Working on one small basic project, while I am working on the List view I get an error " Trying to get property of non-object.
Please help me!
Here the screen shots.
Error
My code
Here is my view :
<ul id="actions">
<h4>List Actions</h4>
<li> Add Task</li>
<li> Edit List</li>
<li><a onclick="return confirm('Are you sure?')" href="<?php echo base_url();?>lists/delete/<?php echo $list_detail->id;?>"> Delete List</a></li></ul>
<h1><?php echo $list_detail->list_name; ?></h1>
Created on : <strong><?php echo date("n-j-Y",strtotime($list_detail->create_date)); ?></strong>
<br/><br/>
<div style="max-width:500px;"><?php echo $list_detail->list_body; ?></div>
<!-- <?php //echo print_r($query); ?>
-->
<br/><br/>
Here is Controller
public function show($id){
$data['list_detail'] = $this->List_model->get_lists($id);
$data['main_content']='lists/show';
$this->load->view('layout/main',$data);
}
Here is model
public function get_list($id){
$query=$this->db->get('lists');
$this->db->where('id', $id);
return $query->row();
}
This error is generating because you are trying to access an array in class object style.
Ex:
$data['list_details'] = 'some value';
and accessing it like:
$data->list_details; // it will generate the error "Trying to get property of non object"
After viewing your code. I think you have written wrong in your model function
Your model function must be like below
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Also in your view before you print the value please check it with the condition of !empty($list_details). so if value is not there still it will not throw any error.
I hope this will help you.
From your controller you are calling the model function as get_lists().
But there is no function as get_lists in model. U need to change the function name in controller to get_list()
When your using $query->row() on your model function then on controller
Model Function
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Controller
public function show($id){
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data->id;
$data['create_date'] = $list_data->create_date;
$data['list_body'] = $list_data->list_body;
$this->load->view('someview', $data);
}
When your using $query->row_array(); on your model function then on controller
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row_array();
}
Controller
public function show($id) {
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data['id'];
$data['create_date'] = $list_data['create_date'];
$data['list_body'] = $list_data['list_body'];
$this->load->view('someview', $data);
}
On view then you can access
<?php echo $id;?>
<?php echo $create_date;?>
<?php echo $list_body;?>
Update for multiple data
public function get_list() {
$query = $this->db->get('lists');
return $query->result();
}
Controller
$results = $this->model_name->get_list();
// Or
//$results[] = $this->model_name->get_list();
$data['list_detail'] = $results;
$this->load->view('someview', $data);
View
<?php foreach ($list_detail as $list) {?>
<?php echo $list->id;?>
<?php echo $list->create_date;?>
<?php echo $list->list_body;?>
<?php }?>

CodeIgniter limit posts per page

I am creating a blog with CodeIgniter. My homepage shows posts that are coming from the database. I want to limit the posts for a page and create a link at the bottom to bring the next posts.
Please someone teach me how to do it with some example.
Thanks.
controller code
<?php
class Mysite extends CI_Controller
{
function index()
{
$this->load->model('posts_model');
$data['result']=$this->posts_model->getAll();
$this->load->helper('html');
$this->load->view("header");
$this->load->view("home",$data);
$this->load->view("footer");
}
?>
model code
<?php
class Posts_model extends CI_Model{
public function getAll(){
$this->load->database();
$query = $this->db->get('posts');
return $query->result();
}
}
?>
view code
<?php
foreach($result as $row) {
$a_url="mysite/single/".$row->id;
echo "<div id="."'postdiv'".">";
echo "<h1>".anchor($a_url,$row->title)."</h1>";
echo "<p>".$row->content."</p>";
echo "<p>Posted by : ".$row->author."</p>";
echo "<br>";
echo "</div>";
}
?>
Here is a nice blog showing load more pagination using jquery and codeigniter.
http://www.thetutorialblog.com/php/twitter-like-pagination-using-codeigniter-and-jquery/
Sample code. Try yourself
function getcountryByname($limit=NULL, $start=NULL) //select country
{
if($limit!=NULL) $this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from('country');
$this->db->order_by("country_name", "ASC");
$query = $this->db->get();
return $query->result_array();
}

How to show category name in Product Page in Codigniter?

I have two table in database ::: 1. tbl_category 2. tbl_product.
Showing all category list in home page and when user clicks on signle category then it goes to product page(from tbl_product) by category id (included category id in tbl_product)
Issue ::: I want to show the category Name and Image Here. So that user can see in which category they clicked on (from tbl_category ) ::: how can I get them ??? please help me
Controller ::::
class Home extends CI_Controller{
//put your code here
public function __construct() {
parent::__construct();
$this->load->model('home_model');
}
public function index(){
$data=array();
$data['result']=$this->home_model->selectCategory($config['per_page'], $this->uri->segment(3));
$data['maincontent']=$this->load->view('home_message',$data,TRUE);
$this->load->view('home', $data);
}
public function category($category_id){
$data=array();
$data['result']=$this->home_model->selectProductByCategoryId($category_id);
$data['maincontent']=$this->load->view('category_detail',$data,TRUE);
$this->load->view('home', $data);
}
Model::::
class Home_Model extends CI_Model{
//put your code here
public function selectCategory($per_page, $offset)
{
$this->db->select('*');
$this->db->from('tbl_category');
$this->db->order_by("category_id", "desc");
$query = $this->db->get('', $per_page, $offset);
foreach ($query->result() as $row)
$data[] = $row;
return $data;
}
public function selectProductByCategoryId($category_id)
{
$this->db->select('*');
$this->db->from('tbl_product');
$this->db->where('category_id',$category_id);
//$this->db->order_by("product_id", "desc");
$query_result= $this->db->get();
$result=$query_result->result();
return $result;
}
View::::
I want to show the category Name and Image Here. So that user can see in which category they clicked on ::::
<?php
foreach ($result as $values)
{
?>
<div>
<div>
<img src="<?php echo base_url();?><?php echo $values->product_image ?>" width="90%" height="220" />
</div>
<div>
<b><?php echo $values->product_name ?></b> <br>
<b>Price: <?php echo $values->product_price ?></b> <br>
Order Now
</div>
</div>
<?php } ?>
Controller
public function category($category_id){
$data=array();
$data['result'] = $this->home_model->selectProductByCategoryId($category_id);
// Added this line
$data['category'] = $this->home_model->selectCategoryByd($category_id);
$data['maincontent'] = $this->load->view('category_detail',$data,TRUE);
$this->load->view('home', $data);
}
Model_Home
public function selectCategoryById($category_id)
{
$result = $this->db->select('*')
->from('tbl_category')
->where('id',$category_id)
->get()
->result();
return $result;
}
Then echo some of those in your view.
You are using a Model_Home for other models functions?
Models are for data and everything what has to do with that data. You should have a Model_Category with a selectById function and a Model_Product with a selectByCategoryId AND a selectById function. Models are NOT used to describe pages.
Please read about MVC and CodeIgniters implementation of it. Then refactor your code and properly use codeigniters features which, if implemented correctly, allow you to not even have to write these functions yourself! Note: I do not know CI very well, so I don't know about the specific implementations of e.g. MVC, ORM, and the like

how to pass two result in same view in codeigniter

i am new with codeigniter.I have written controller,model.view.
I want to execute the another query in the model which having the "ID" from first model query in the where condition and pass the seperate results for both the both query in same view.
i have explain it in model and view.
how can i do this.need some syntax how can i do this.
or is this correct way or do somwthing??
Controller
class demo extends CI_Controller
{
function getRecords()
{
$offset = trim($this->input->get('off'));
$this->load->model('demo_model');
$data = $this->demo_model->get_messages($offset);
$return = $this->load->view('demo_view',array('records'=>$data),true);
die($return);
}
}
Model
class demo_model extends CI_Model
{
function get_messages($offset = 0)
{
$q = $this->db->query(" select Id,cloum2 from table");
// want to use ID from above query in to second query
// Like sql="select * from table where id='$id'" ;
// and how i pass the separate result for both query inthe view
return $q->result_array();
}
}
View demo_view;
<?php foreach($records as $row) :?>
<div> do something with $row </div>
<div> //want use second query foreach here </div>
<?php endforeach ;?>
Both queries will return PHP arrays so you can do something like.
MODEL
function get_messages($offset = 0)
{
$q = $this->db->query(" select Id,cloum2 from table");
return $q->result_array();
}
function get_id($id){
$q = $this->db->query("select * from table where id='$id'");
return $q->result_array();
}
CONTROLLER
class demo extends CI_Controller{
function getRecords(){
$offset = trim($this->input->get('off'));
$this->load->model('demo_model');
$data = $this->demo_model->get_messages($offset);
for($c=0;$c<count($data);$c++){
$data[$c]['innerRow']=$this->demo_model->get_id($data[$c]['id']);
}
$return = $this->load->view('demo_view',array('records'=>$data),true);
die($return);
}
VIEW
<?php foreach($records as $row) :?>
<div> do something with $row </div>
<div>
<?php foreach($row['innerRow'] as $innerrow) :?>
......
<?php endforeach ;?>
</div>
<?php endforeach ;?>
(But this isn't such a good idea as you will run a bunch of queries to the model. Its better to think of a nicer query)
function getRecords()
{
$offset = trim($this->input->get('off'));
$this->load->model('demo_model');
$data = $this->demo_model->get_messages($offset);
$secresult=array();
foreach($data as $msg)
{
$res = $this->demo_model->get_result($msg['id']); // pass id to query
$secresult[]=$res;
}
$return = $this->load->view('demo_view',array('records'=>$data, 'theresult'=>$secresult),true);
die($return);
}
In your view file, you can use theresult for the second loop.

Display multiple rows in codeigniter

I have query where I fetch the content of about 5 rows each with different content, but i dont now to display them individually in my views.
When I pass the the query->result() from my model back to my controller as $query, and then load a view with the $query data, I don't know how to echo out the e.g. content field of row 1.
I only know how to do this: foreach($query as $row) { echo $row->content; }
But this doesn't allow me to select what row content data (out of the 5 retrieved) I wanna echo out.
Could someone please explain how this is done?
The CodeIgniter documentation includes a section titled Generating Query Results which has some examples of what you are looking for.
Here's an (obviously contrived) example:
model:
class Model extends CI_Model
{
public function get_data()
{
return $this->db->query('...');
}
}
controller:
class Controller extends CI_Controller
{
public function index()
{
$data['query_result'] = $this->model->get_data();
$this->load->view('index', $data);
}
}
view:
<?php foreach ($query_result->result() as $row): ?>
<?php echo $row->field_1; ?>
<?php echo $row->field_2; ?>
<?php // and so on... ?>
<?php endforeach; ?>
function a(){
$this->db->select('*');
$this->db->from('table');
$this->db->where(array('batch_id'=>$batchid,'class_id'=>$classid));
$this->db->where(array('batch_id'=>$batchid,'class_id'=>$classid));
$query=$this->db->get();
for($i=1;$i<=$query->num_rows();++$i){
$data[$i]['task'] = $query->row($i)->task;
$data[$i]['subject_id'] = $query->row($i)->subject_id;
$data[$i]['postdate'] = $query->row($i)->postdate;
$data[$i]['cdate'] = $query->row($i)->cdate;
}
}

Categories