Welcome Guest, Not a member yet? Register   Sign In
categories display problem show all categories
#1

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
Reply
#2

Hello to you too...
Reply
#3

Any Solution Please
Reply
#4

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.
Reply
#5

(01-15-2015, 01:18 PM)mwhitney Wrote: 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.

Yeah, it really helps if you do more debugging yourself and refine your issue into a more simple question.  What data is your query returning? Is it the data you expect it to return?  Is your query working as intended but the view is not rendering as you expect?

It seems as if you are asking a very generic "how do I do this" question that is not specific to CodeIgniter and that language difficulties make it hard to understand exactly what that question is.
Reply
#6

(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
Reply
#7

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

Reply
#8

(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
Reply
#9

(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.

Reply
#10

(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 aLEFT JOIN `categories` AS c ON `a`.`category_id` = `c`.`idORDER BY `DESCDESC

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 )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB