Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7603191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:36:13+00:00 2026-05-30T23:36:13+00:00

I’m working on a new php project with CodeIgniter and I have few questions

  • 0

I’m working on a new php project with CodeIgniter and I have few questions about it.

1 – I created news script, for now I done backend and I’m working on frontend. At frontend I already set last news, but I want to show how many comments at one news there are. I used foreach, but I don’t know what’s the best method to do next in MVC framework.

<?php
foreach ($query as $news)
{
    echo $news->title . "<br>" . $news->content . "<br>";
    $commentnum = $this->db->select('id_comment, id_news')->where('id_news', $news->id)->get('comments');
    echo $commentnum->num_rows();
}
?>

Okay.. that’s some piece of code which I use in other projects. What’s the best way to use that code in MVC pattern?

2 – At this news script I have some controller which is named News and in it are functions post, cats and tags. And code which I’m using now, but not works like I want:

$route['news/(:any)'] = "news/post/$1";

So how to exclude post, cats and tags from the route -> If I type news/tags/first, to get the tag first and not error, that the post doesn’t exist. How to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:36:15+00:00Added an answer on May 30, 2026 at 11:36 pm

    If you want to stick to the MVC pattern (and I would suggest you do as it makes things easier in the long run of managing your application, or updating its looks) then do the following:

    Instead of echoing out all your ‘news’ info, store it into an array, and pass that array to the VIEW. Let the VIEW then just go through that array (of news & related comments) and output it.

    That way your logic for assembling this news / comment structure remains in the controller, and the view just spits out the data, and does not ITERATE through the $query object.

    This creates a clean separation of controller, model & view.

    Example:

    foreach ($query as $news)
    {
        $comments = $this->db->select('id_comment, id_news')->where('id_news', $news->id)->get('comments');        
        $data['news'][$news->id]['title'] = $news->title;
        $data['news'][$news->id]['content'] = $news->content;
        $data['news'][$news->id]['num_comments'] = $comments->num_rows();
    
        if($data['news'][$news->id]['num_comments'] > 0){
            foreach ($comments as $entry){
                  $data['news'][$news->id][$entry->id]['comment'] = $entry->content;  
            }
        }
    }
    

    As far as your routing issue is concerned you need to properly route the URI to the METHOD in your controller:

    $route['tags/(:any)'] = "news/tags/$1";
    $route['(:any)'] = "news/posts/$1";
    

    The $route['(:any)'] I haven’t personally tried, but I would suggest you format your URI to be something like http://domain.com/post/name-of-post as opposed to http://domain.com/name-of-post, it will just be cleaner for routing keeping it seperate, because the above route $route['(:any)'] forces you to create CUSTOM routes (a whitelist) for everything else like tags / links / users / etc;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.