Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Foreach in a View?
  • Is this the most efficient way of getting data for your FuelPHP project? I feel like I'm doing a lot of... not clean things for this project and I want to start off the right way while I'm still early in development.
    Anyway, I'm working on a news system panel pulling news post by month.

    Here is my controller:
    public function action_index()
    {
        $view = View::forge('news/index');
        $news_array = Model_News::get_news(date('m'));

        $view->set('news_array', $news_array, false);
        $this->template->content = $view;
    }

    Here is my Model get_news method:
    public static function get_news($month) {
        $news_all = array();

        $newsInfo = \DB::select('news_id', 'title', 'body', 'date', 'username')
                ->from('news')
                ->join('user')
                ->on('user.user_id', '=', 'news.user_id')
                ->where('date', 'LIKE', '____-'.$month.'-__ __:__:__')
                ->execute();
        foreach($newsInfo as $news) {
            $news['username'] = ucfirst($news['username']);
            $news['date'] = date('F j, Y g:i A', strtotime($news['date']));
            array_push($news_all, $news);
        }

        return $news_all;
    }

    Again I feel the array_push is not necessary :/ But I don't know anyway else to do it since this query builder pulls data awkwardly.

    Then here is my view:
    <?php foreach($news_array as $news) { ?>
    <div class="content_box clearfix">
            <div class="content_header">
                    <?php echo $news['title']; ?>
            </div>
            <div class="news_user">
                    <p><img src="#" height="60" width="60" border="1" /></p>
                    <p><a href="#"><?php echo $news['username']; ?></a></p>
            </div>
            <div class="news_content">
                    <?php echo $news['body']; ?>
                <p style="text-align:right;"><?php echo $news['date']; ?></p>
            </div>
    </div>
    <?php } ?>

    ANYWAY, if there is only one news posting available.. the foreach gets all F'ed up... but if there is more than one, it pulls fine. WHICH I KNOW WHY. But I find it unecessary to go through the array, check if there is more than one iteration, and then choose the correct loop/non-looping process WHILE still in the view... since the view should be free from PHP as much as possible. :/

    Can someone help me out here?
  • HarroHarro
    Accepted Answer
    You can make your model method a lot shorter by using execute()->as_array(). It will return the result in an array, no more need for conversions. I would do the date conversion in the view, that's clearly presentation code.

    I don't see anything wrong with the view. If you have an index page with a table-view (rows representing records), the only way to display them is to iterate.
  • I had as_array before... still doesn't do anything. With a foreach, if there is NO array.. it.. acts all weird. I mean there is an array, but then it gets turned into a multidimensional array when more than one record is pulled.. it doesn't iterate correctly with just one record. :/
  • HarroHarro
    Accepted Answer
    Afaik it always returns a multidimensional array, no matter how many records are returned.

    And that is by design, it would be foolish to have to check when running a query if the result was a single array or a multi-dimensional array.

    If you only want the first result, simply do

    $result = reset($result);
  • Oh wait... you're right... if you put as_array()... the foreach loop works regardless of the number of arrays returned. THANK YOU!!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion