-
asifagaria
Junior Member
-
Posts: 12
Threads: 4
Joined: Jan 2015
Reputation:
0
Controller
Code: public function category() {
//Get Article View
$data['articles'] = $this->Article_model->get_categories_t('id','DESC');
//Get Articles
//Load View
$this->load->view('cate', $data);
}
Model
Code: public function get_categories($order_by = null, $sort = 'DESC', $limit = null, $offset = 0) {
$this->db->select('a.*, c.name as category_name');
$this->db->from('articles as a');
$this->db->join('categories AS c', 'a.category_id = c.id ','LEFT');
if($limit != null) {
$this->db->limit($limit, $offset);
}
if($order_by != null) {
$this->db->order_by($order_by, $sort);
}
$query = $this->db->get();
return $query->result();
}
View
Code: <?php foreach ($articles as $article) : ?>
<div class="blog-grid-col"> <!--movie video block-->
<div class="blog-grid-content">
<div class="head-tags red-bg">
<span class="head-title"><?php echo $article->category_name; ?></span>
</div>
<div class="blog-grid-img block-height">
<a href="<?php echo base_url (); ?>articles/view/<?php echo $article->id; ?>">
<a href="<?php echo base_url (); ?>articles/view/<?php echo $article->id; ?>"><img src="assets/movies/<?php echo $article->image; ?>" /></a>
</a>
</div>
<div class="figcaption dark-bg"> <!--movie-video-block-hover-->
<div class="bg-dark-shadow"></div>
<div class="head-small-tags red-small-bg">
<i class="fa fa-video-camera fa-lg"></i>
</div>
<div class="text-blog-grid-2 block-height">
<a href="<?php echo base_url (); ?>articles/view/<?php echo $article->id; ?>"><h5><?php echo $article->title; ?></h5></a>
<p class="readmore">
<a href="<?php echo base_url (); ?>articles/view/<?php echo $article->id; ?>" type="button" class="btn btn-lg btn-primary btn-block">Read More</a>
</p>
<div class="movies-foot">
<ul>
<li class="rate">
<a href="#"><i class="fa fa-star"></i></a>
<a href="#"><i class="fa fa-star"></i></a>
<a href="#"><i class="fa fa-star"></i></a>
<a href="#"><i class="fa fa-star"></i></a>
<a href="#"><i class="fa fa-star"></i></a>
</li>
<li class="like-rate">
<a href="#"><i class="fa fa-heart"></i></a>
<a href="#"><i class="fa fa-share-square"></i></a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
please solve this problem
display all categories
not for selected category
-
Avenirer
Senior Member
-
Posts: 419
Threads: 15
Joined: Oct 2014
Reputation:
21
-
asifagaria
Junior Member
-
Posts: 12
Threads: 4
Joined: Jan 2015
Reputation:
0
-
mwhitney
Posting Freak
-
Posts: 1,101
Threads: 4
Joined: Nov 2014
Reputation:
95
It would probably help if you explained:
- What you expected to happen
- What is actually happening
To me, it looks like you are getting a list of articles, including the category of each article. If you want a list of possible categories, you need to select the categories from the database. If you just want a list of categories used by the articles in the list, you should be able to create that list from the data you already have.
-
asifagaria
Junior Member
-
Posts: 12
Threads: 4
Joined: Jan 2015
Reputation:
0
01-16-2015, 02:20 AM
(This post was last modified: 01-16-2015, 02:24 AM by asifagaria.)
i say how i get posts on categories like a
----->arcade games
categories ->
----->pc games
i want get only pc games
it's show all on categories like pc games and arcade game
it's show on display posts
so how solve this
i dont understand this
-
Rufnex
Chuck Norris Mode - Former CI Council
-
Posts: 511
Threads: 7
Joined: Oct 2014
Reputation:
35
You have to add a where clause to your model like
PHP Code: public function get_categories($category_name = false, $order_by = null, $sort = 'DESC', $limit = null, $offset = 0) { $this->db->select('a.*, c.name as category_name'); $this->db->from('articles as a'); $this->db->join('categories AS c', 'a.category_id = c.id ','LEFT'); if ($category_name) { $this->db->where('categories.name', $category_name); } if($limit != null) { $this->db->limit($limit, $offset); } if($order_by != null) { $this->db->order_by($order_by, $sort); } $query = $this->db->get(); return $query->result(); }
Or you check the where with the category id ..
-
asifagaria
Junior Member
-
Posts: 12
Threads: 4
Joined: Jan 2015
Reputation:
0
(01-16-2015, 03:40 AM)Rufnex Wrote: You have to add a where clause to your model like
PHP Code: public function get_categories($category_name = false, $order_by = null, $sort = 'DESC', $limit = null, $offset = 0) { $this->db->select('a.*, c.name as category_name'); $this->db->from('articles as a'); $this->db->join('categories AS c', 'a.category_id = c.id ','LEFT'); if ($category_name) { $this->db->where('categories.name', $category_name); } if($limit != null) { $this->db->limit($limit, $offset); } if($order_by != null) { $this->db->order_by($order_by, $sort); } $query = $this->db->get(); return $query->result(); }
Or you check the where with the category id ..
This Give Error Code: A Database Error Occurred
Error Number: 1054
Unknown column 'categories.name' in 'where clause'
SELECT `a`.*, `c`.`name` as category_name FROM (`articles` as a) LEFT JOIN `categories` AS c ON `a`.`category_id` = `c`.`id` WHERE `categories`.`name` = 'id' ORDER BY `DESC` DESC
Filename: /var/www/html/page/models/article_model.php
Line Number: 88
-
Rufnex
Chuck Norris Mode - Former CI Council
-
Posts: 511
Threads: 7
Joined: Oct 2014
Reputation:
35
01-16-2015, 08:35 AM
(This post was last modified: 01-16-2015, 08:35 AM by Rufnex.)
My code was a pseudo code .. i dont know what table fields you have. You have to ajust this with you needs for e.g. with a proper catogry id like
PHP Code: public function get_categories($category_id = false, $order_by = null, $sort = 'DESC', $limit = null, $offset = 0) { ... if ($category_name) { $this->db->where('categories.id', $category_id); } ...
This means, that your categories table as a column name id or in the example before a column named name.
-
asifagaria
Junior Member
-
Posts: 12
Threads: 4
Joined: Jan 2015
Reputation:
0
01-16-2015, 01:45 PM
(This post was last modified: 01-16-2015, 02:29 PM by asifagaria.)
This Time Error Change
my table fields is
categories table and ( id pkey ) ( name var )
and articles table ( id pkey ) ( title var ) ( category_id int )
Code: A PHP Error was encountered
Severity: Notice
Message: Undefined variable: category_name
Filename: models/article_model.php
Line Number: 83
PHP Code: A Database Error Occurred
Error Number: 1054
Unknown column 'DESC' in 'order clause'
SELECT `a`.*, `c`.`name` as category_name FROM (`articles` as a) LEFT JOIN `categories` AS c ON `a`.`category_id` = `c`.`id` ORDER BY `DESC` DESC
Filename: /var/www/html/post/models/article_model.php
Line Number: 92
This Time Error Change
Please Solve My Problem
my table fields is
categories table and ( id pkey ) ( name var )
and articles table ( id pkey ) ( title var ) ( category_id int )
|