Welcome Guest, Not a member yet? Register   Sign In
"Undefined index" issue.
#1

[eluser]kyleect[/eluser]
Here are the basics. I have two tables: posts and users. What I want to do is have both a raw timestamp and a formatted date. I figured the model is the best place for this data transformation which is why I used the AS keyword. However, I seem to be running in to some issues:

1) I'm getting this error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: datetime

Filename: models/Post.php

Line Number: 83

however this is is the array it's referring to:

Code:
Array
(
    [0] => Array
        (
            [id] => 1
            [body] => fasdfadf
            [user_id] => 1
            [datetime] => 1241405669
            [username] => dev
        )

    [1] => Array
        (
            [id] => 1
            [body] => asdfadsf
            [user_id] => 1
            [datetime] => 1241405666
            [username] => dev
        )

    [2] => Array
        (
            [id] => 1
            [body] => fdf
            [user_id] => 1
            [datetime] => 1241405663
            [username] => dev
        )

    [3] => Array
        (
            [id] => 1
            [body] => test
            [user_id] => 1
            [datetime] => 1241405104
            [username] => dev
        )

    [datetime] => 1969/12/31 @ 6:00:00 pm
)

These appear to have a datetime index.

2) What is with the actual index 'datetime' with a formatted date?

Any help would be greatly appreciated.

-------------------------------

Method from the Post model:

Code:
public function river($limit)
        {
            $query = $this->db->query("
                SELECT
                    p.id,
                    p.body,
                    p.user_id,
                    p.timestamp AS datetime,
                    u.id,
                    u.username
                FROM
                    posts p,
                    users u
                WHERE
                    u.id = p.user_id
                ORDER BY p.timestamp DESC
                LIMIT 0, {$limit}
            ");
            
            if ($query->num_rows() > 0)
            {
                $results = $query->result_array();
                
                $results['datetime'] = date('Y/m/d @ g:i:s a', $results['datetime']);
                
                return $results;
            }
            else
            {
                return false;
            }
        }

Code from controller:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Home
*
* Home is always the default controller. It's what's displayed as the root index.
*
*/

// ------------------------------------------------------------------------

/**
* @package CI1.7_kyct
* @subpackage controllers
**/

    class Home extends MY_Controller
    {

        
        function __construct()
        {
            parent::__construct();
            $this->load->model('Post');
        }
    
        function index()
        {
            $data['river'] = $this->Post->river(10);
            
            $this->load->view('home', $data);
        }
    }
?>
#2

[eluser]TheFuzzy0ne[/eluser]
Could it be that you're trying to access the array value at the wrong level? You should loop through them, or access them like this:

Code:
$val = $arr[0]['date_time'];

However, judging by the model code you posted, you're adding the date_time index to the containing array, and not each of the arrays within it.

Hope this makes sense.
#3

[eluser]kyleect[/eluser]
[quote author="TheFuzzy0ne" date="1241426662"]Could it be that you're trying to access the array value at the wrong level? You should loop through them, or access them like this:

Code:
$val = $arr[0]['date_time'];

However, judging by the model code you posted, you're adding the date_time index to the containing array, and not each of the arrays within it.

Hope this makes sense.[/quote]

Thanks. Here is my updated model method:

Code:
public function river($limit)
        {
            $query = $this->db->query("
                SELECT
                    p.id,
                    p.body,
                    p.user_id,
                    p.timestamp,
                    u.id,
                    u.username
                FROM
                    posts p,
                    users u
                WHERE
                    u.id = p.user_id
                ORDER BY p.timestamp DESC
                LIMIT 0, {$limit}
            ");
            
            if ($query->num_rows() > 0)
            {
                $results = $query->result_array();
                
                foreach ($results as $post)
                {
                    $post['datetime'] = date('Y/m/d @ g:i:s a', $post['timestamp']);
                }
                
                return $results;
            }
            else
            {
                return false;
            }
        }
    }

and here is the resulting array of $results:

Code:
Array
(
    [0] => Array
        (
            [id] => 1
            [body] => fasdfadf
            [user_id] => 1
            [timestamp] => 1241405669
            [username] => dev
        )

    [1] => Array
        (
            [id] => 1
            [body] => asdfadsf
            [user_id] => 1
            [timestamp] => 1241405666
            [username] => dev
        )

    [2] => Array
        (
            [id] => 1
            [body] => fdf
            [user_id] => 1
            [timestamp] => 1241405663
            [username] => dev
        )

    [3] => Array
        (
            [id] => 1
            [body] => test
            [user_id] => 1
            [timestamp] => 1241405104
            [username] => dev
        )

)

The datetime index doesn't get added to each of the arrays. Here is the view if it helps:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Home&lt;/title&gt;
                
        &lt;link rel="stylesheet" href="assets/design/stylesheets/global.css" type="text/css"&gt;
        &lt;link rel="stylesheet" href="assets/design/stylesheets/reset.css" type="text/css"&gt;
        
        &lt;link rel="stylesheet" href="assets/design/stylesheets/main.css" type="text/css" media="all"&gt;
        &lt;link rel="stylesheet" href="assets/design/stylesheets/media_screen.css" type="text/css" media="screen"&gt;
        &lt;link rel="stylesheet" href="assets/design/stylesheets/media_print.css" type="text/css" media="print"&gt;
        
        [removed][removed]
        [removed][removed]

    &lt;/head&gt;
    
    &lt;body id="home"&gt;
        <h1>Slog</h1>
        
        <h2>Post</h2>
        &lt;form action="&lt;?php echo site_url('posts/post_new'); ?&gt;" method="post" accept-charset="utf-8"&gt;
            <p>&lt;textarea rows="8" cols="80" id="body" name="body"&gt;&lt;/textarea></p>
            <p>&lt;input type="submit" id="submit" name="submit" value="Post!"&gt;&lt;/p>
        &lt;/form&gt;
        
        <h2>River</h2>
        
    &lt;?php if ($river): ?&gt;
        
        &lt;?php echo print_r($river); ?&gt;
        
        <div class="river">
        &lt;?php foreach ($river as $post): ?&gt;
            <div class="river_item" id="&lt;?php echo $post['id']; ?&gt;">
            <p>&lt;?php echo $post['body']; ?&gt;</p>
            <p>by: &lt;?php echo $post['username']; ?&gt; @ &lt;?php echo $post['datetime']; ?&gt;</p>
        &lt;?php endforeach ?&gt;
        </div>
    &lt;?php else: ?&gt;
        <p>No posts to display.</p>
    &lt;?php endif ?&gt;
    
    &lt;/body&gt;
&lt;html&gt;
#4

[eluser]TheFuzzy0ne[/eluser]
You're adding the date_time index to the $post variable, which doesn't persist.

I'd have thought you'de need to do something more like this to get the results above:

Code:
for ($i =0; $i < count($results); $i++)
{
    $results[$i]['datetime'] = date('Y/m/d @ g:i:s a', $results[$i]['timestamp']);
}
#5

[eluser]kyleect[/eluser]
[quote author="TheFuzzy0ne" date="1241456826"]You're adding the date_time index to the $post variable, which doesn't persist.

I'd have thought you'de need to do something more like this to get the results above:

Code:
for ($i =0; $i < count($results); $i++)
{
    $results[$i]['datetime'] = date('Y/m/d @ g:i:s a', $results[$i]['timestamp']);
}
[/quote]

That worked and thank you but still not fully understanding why the foreach loop didn't work. $post (which only exists inside the foreach) represents each array inside the results array so I would be adding a datetime index to each of those.
#6

[eluser]TheFuzzy0ne[/eluser]
You were only adding the index to the variable which was assigned the value, not the actual array itself. You could also do it with a foreach loop if you assign by reference:

Code:
foreach ($results as &$post)
{
    $post['datetime'] = date('Y/m/d @ g:i:s a', $post['timestamp']);
}

Sorry, but for some reason it didn't occur to me before.




Theme © iAndrew 2016 - Forum software by © MyBB