1

I am trying to change the format of a date that is being selected from a MySQL database but it is returning an error. I've looked online but I can't work our what I'm doing wrong.

The code that I am using in my model is as follows:

Public function get_news()
    {
        $this->db->select('id, text');
        $this->db->select("DATE_FORMAT(date,'%W %D %M %Y')", FALSE);
        $this->db->limit(10);
        $this->db->order_by('date', 'desc');
        $query = $this->db->get('news');

        $result = $query->result_array();
        return $result;
    }

The error is:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: date

Filename: views/news.php

Line Number: 22

Backtrace:

File: C:\xampp\htdocs\tsh\CI\application\views\news.php
Line: 22
Function: _error_handler

File: C:\xampp\htdocs\tsh\CI\application\controllers\home.php
Line: 99
Function: view

File: C:\xampp\htdocs\tsh\public_html\index.php
Line: 292
Function: require_once

View:

<?php foreach($news as $news_item) { ?>
<article class="article_text">
    <p class="segment_headding"><?php echo $news_item['date']; ?></p>
    <?php echo $news_item['text']; ?>
</article>
<?php } ?>

I'm sure it is just something silly that I've done wrong but I just can't see it.

1
  • You have an array that tries to use the 'date' index, which isn't defined. where is views/news.php and show the line in question. Commented Apr 17, 2015 at 20:06

1 Answer 1

3

Your view echos $news_item['date'], but in your query you have:

$this->db->select("DATE_FORMAT(date,'%W %D %M %Y')", FALSE);

I am not too familiar with codeigniter, but I think this is probably not producing something that can be retrieved by $news_item['date']. You could try aliasing the formatted date

$this->db->select("DATE_FORMAT(date,'%W %D %M %Y') AS fdate", FALSE);

and then using that in your view: $news_item['fdate'].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.