I have a database in mysql which has 2 tables one named category(cat_id primary key) and one named book(b_id as primary key) where cat_id is a foreign key for book.
I'm working in CI and here I output the categories in a view:
foreach($categories->result()as $row){
foreach($categories->result()as $row){
echo ''.$row->category.'<br>';
}
}
I want that when clicking to the link category_details to output just the books of that category.
Here I have this method in my controller:
public function category_details($data)
{
$data['cat_id'] = $this->home_model->output_cat_detail();
$data['category_detail'] = $this->home_model->output_cat_detail();//printon librat
$data['categories'] = $this->home_model->output_categories();
$this->load->view('header', $data);
$this->load->view('category_details', $data);
}
So the method in model which does that selection is this:
public function output_cat_detail(){
$condition = "cat_id =" . "'" . $data['cat_id'] . "'";
$this->db->select('*');
$this->db->from('book');
$this->db->where($condition);
$query = $this->db->get();
return $query;
But after I do this when I click in a category all the books appear not just the books of that category I have clicked. Can someone help me with the condition to select just the books of that category?
public function category_details($id){
$data = array();
$data['category_detail'] = "";
$query = $this->db->get_where('book', array('id' => $id));
// Your current model logic will not work for this
if($query->num_rows() > 0){
$data['category_detail'] = $query->result();
}
$this->load->view('header', $data);
$this->load->view('category_details', $data);
}
Related
Original function:
function getRows($id = ""){
if(!empty($id)){
$query = $this->db->get_where('orders', array('id' => $id));
return $query->row_array();
}else{
$query = $this->db->get('orders');
return $query->result_array();
}
}
I need to JOIN data from table order_shipping and order_products where order_id = order_id
I do:
/*
* Fetch user data
*/
function getRows($id = ""){
if(!empty($id)){
$query = $this->db->get_where('orders', array('id' => $id));
return $query->row_array();
}else{
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('');
return $query->result_array();
}
}
This above code working correct. But currently JOIN only after else. I need do the same way before else:
* Fetch user data
*/
function getRows($id = ""){
if(!empty($id)){
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('', array('id' => $id));
return $query->row_array();
}else{
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('');
return $query->result_array();
}
}
But this code before else not working correct for me and currently display wrong data. When I try fetch all orders, then second function display correct all orders. But when I try fetch example only order ID 2, then is used first function and display wrong data, example when I fetch order ID2, then always display only order with ID 1.
This function is for REST API fetch orders.
I'm making a blog in Codeigniter. Now on the blog post view page, I want the previous and next post link. This is my controller of blog details.
public function blog_details($slug = NULL){
//data
$data['blog_info'] = $this->Blog_model->get_posts($slug);
if(empty($data['blog_info'])){
show_404();
}
$data['post_name'] = $data['blog_info']['post_name'];//post name
$data['categories'] = $this->Blog_model->get_categories();
//meta title, discription, keywords
$data['meta_title'] = $data['blog_info']['post_name'];
$data['meta_description'] = $data['blog_info']['post_meta'];
$data['meta_keywords'] = $data['blog_info']['post_tags'];
//view
$data['main_content'] = 'blog_details';
$this->load->view('include/template',$data);
}
and my model is
public function get_posts($slug = FALSE, $limit = TRUE, $offset = FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->order_by('blogs.post_id', 'DESC');
$this->db->join('category', 'category.category_name = blogs.category');
$query = $this->db->get('blogs');
return $query->result_array();
}
$query = $this->db->get_where('blogs', array('post_slug' => $slug));
return $query->row_array();
}
How to create previous and next post links with post names? Thanks in advance.
you can use direct query method to run below query and just pass your blog id to get previus and next blog name
public function get_prewnext($id){
$query = $this->db->query("SELECT name,
(SELECT name FROM blog s1
WHERE s1.id < s.id
ORDER BY id DESC LIMIT 1) as previous_name,
(SELECT name FROM blog s2
WHERE s2.id > s.id
ORDER BY id ASC LIMIT 1) as next_name
FROM blog s
WHERE id = $id;");
if($query->num_rows() > 0){
$data = $query->result_array();
}
$query->free_result();
return $data;
}
Note: If value is not found then it will return null.
I have a problem with the database.
I have 3 tables.
a) notebook
b) category_notes
c) domains
I have domain.id in the 'domains' table.
In the 'notes' table, I have domain_id and notes_category_id.
In the 'notes_category' table, I have id, notes_category_name
Script action: Saves notes for a given domain.
Everything works correctly - the data is saved and read.
Now I would like to add a note type, I have already done this functionality - but I'm showing the ID of the note type and not its name.
Of course, I have a relationship that's dear.
Controller domains.php
public function notes($id)
{
$this->load->model('admin/notes_model');
$result = $this->notes_model->get_by_domain_id($id);
echo '{"records":' . json_encode( $result ) . '}';
}
Model - domains_category_model.php
public function get($id = false)
{
if ( $id == false) {
$q = $this->db->get('notes_category');
$q = $q->result();
}
else{
$this->db->where('id', $id);
$q = $this->db->get('notes_category');
$q = $q->row();
}
return $q;
}
Controller - notes_category.php
public function get($id = false)
{
$result = $this->notes_category_model->get($id);
echo '{"records":' . json_encode( $result ) . '}';
}
Controller - notes.php
public function get($id = false)
{
$result = $this->notes_model->get($id);
echo '{"records":' . json_encode( $result ) . '}';
}
Model - Notes_model.php
public function get( $id = false)
{
if ( $id == false ) {
$q = $this->db->get('notes');
$q = $q->result();
}
else{
$this->db->where('id', $id);
$q = $this->db->get('notes');
$q = $q->row();
}
return $q;
}
public function get_by_domain_id($id)
{
$this->db->where('id_domain_rel', $id);
$q = $this->db->get('notes');
$q = $q->result();
return $q;
}
If you want to receive the name without conflict , use alias in select.
$this->db->select('n.* , n.id id_noted')->from('notes n')->get()->result();
$this->db->select('*, id id_noted')->from('notes')->get()->result();
I hope I understand what you want to do.
$query = $this->db->get("courses");
$data['courses'] = $query->result();
foreach($courses as $c){
$this->db->select('*');
$this->db->from('subjects');
$this->db->where('id',$c->course);
$query = $this->db->get("subjects");
$data['subjects'] = $query->result();
foreach($subjects as $s){
$this->db->select('*');
$this->db->from('tests');
$this->db->where('id',$t->test);
$query = $this->db->get("tests");
$data['tests'] = $query->result();
}
}
I want to print tables of Courses having top first row with single column with data $c->course
and bellow rows with two column having data $s->subject and $t->test respectively...
you can use like this for first row for courses and next two rows for subjects and tests
$query = $this->db->get("courses");
$data['courses'] = $query->result();
foreach($courses as $key=>$c){
$this->db->select('*');
$this->db->from('subjects');
$this->db->where('id',$c->course);
$query = $this->db->get("subjects");
$data['subjects'][$key] = $query->result();
foreach($subjects as $key=>$s){
$this->db->select('*');
$this->db->from('tests');
$this->db->where('id',$t->test);
$query = $this->db->get("tests");
$data['tests'][$key] = $query->result();
}
}
Hi I am having an issue getting the view to show any results for the following code.
Controller:
$datedue = 2011-01-27;
$username = $this->tank_auth->get_username();
$sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?";
$tasks = $this->db->query($sql, array($username, $datedue));
$count = 0;
$taskdetails = array();
foreach($tasks->result() as $row)
{
$taskid = $row->taskid;
$subsql = "SELECT * FROM tasks WHERE id = ?";
$taskdetails[$count] = $this->db->query($subsql, array($taskid));
$count++;
}
$data['taskdetails'] = $taskdetails;
$data['total'] = $count;
$this->header();
$this->load->view('dashboard', $data);
View:
<?php
foreach($taskdetails as $entry)
{
foreach($entry->result() as $row)
{
echo $row->name;
}
}
?>
Any help would be nice thanks.
The reason why your view is not displaying something is because you're query is not completely correct.
$datedue = 2011-01-27;
should be enclosed in quotes, since the date is a string.
$datedue = "2011-01-27";
Also, you're not correctly following the concept of MVC. All the database querying and results should occur inside of the Model. And all of the data handling inside the Controller.
The Controller nor the View should handle any of the database connections, that is the duty of the Model. Therefore I would advise to create a Model and put all of the Database querying in one or two functions. The Controller would then call these functions and receive the data back. Then it should manipulate it and pass it to the View in a clean matter (by this I mean, that the View should NEVER call result()
Here is how you're code should be structured:
CONTROLLER
class Tasks extends Controller {
function Tasks ()
{
parent::Controller();
$this->load->model('tasks_model');
$this->load->database();
}
function index()
{
$datedue = "2011-01-27";
$username = $this->tank_auth->get_username();
$tasks = $this->tasks_model->getTasks($username, $datedue);
$count = count($tasks);
$data['taskdetails'] = $tasks;
$data['total'] = $count;
$this->header();
$this->load->view('dashboard', $data);
}
}
MODEL
class Tasks_model extends Model {
function Tasks_model()
{
// Call the Model constructor
parent::Model();
}
function getTasks($username, $datedue) {
$sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?";
$tasks = $this->db->query($sql, array($username, $datedue));
$taskdetails = array();
foreach($tasks->result() as $row){
$taskid = $row->taskid;
array_push( $taskdetails, $this->getTasksDetails( $taskid ) );
}
return $taskdetails;
}
function getTasksDetails($taskid) {
$subsql = "SELECT * FROM tasks WHERE id = ?";
$taskdetails = $this->db->query($subsql, array($taskid));
return $taskdetails->row();
}
}
VIEW:
foreach($taskdetails as $task)
{
echo $task->name;
}
verify your task table contain the name field
view
foreach($taskdetails as $entry)
{
foreach($entry->result() as $row)
{
echo $row[0];
}
}
why you are passing array in this line
$taskdetails[$count] = $this->db->query($subsql, array($taskid));
instead write
$taskdetails[$count] = $this->db->query($subsql, $taskid);