Part of the EllisLab Network
   
 
Get date the message was submitted
Posted: 02 May 2009 05:21 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  69
Joined  12-31-2008

Hi,

I have this code, but I need to add the date a message was submitted. Does anyone know how I could to this?

function insert_comment() {    
        
//get blog_id so it can be used to add comment to correct blog item
        
$this->db->where('blog_id',$this->uri->segment(3));
        
$this->db->insert('comments',$_POST);    
        
redirect('/blog/post/'.$_POST['blog_id']);    
    

Many thanks!

C

Profile
 
 
Posted: 02 May 2009 06:50 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRank
Total Posts:  2932
Joined  01-07-2008

C,

Very easy.  Add a database column, let’s call it publish_date, and make it int(10).

Then manually populate that column with the current time:

// Add the publish date
$_POST['publish_date'time(); 

And when you retrieve it you can use a function such as date to format it.

I’m sure it’s just an example - but remember to validate your input.

[Edit: int not varchar, Pascal ... psh]

 Signature 
Profile
MSG
 
 
Posted: 02 May 2009 06:57 AM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2674
Joined  05-18-2008

Why store it as a varchar instead of one of the many date/time formats such as timestamp or datetime?

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
Posted: 02 May 2009 07:03 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  69
Joined  12-31-2008

I tried…

// Add the publish date
$_POST[‘publish_date’] = time();

Page redirects and all data is added to the database apart from the date. My date is ‘comment_date’ in the database and it is a “date” field.

This is the complete new code…

function insert_comment() {    
        
//get blog_id so it can be used to add comment to correct blog item
        
$this->db->where('blog_id',$this->uri->segment(3));
        
// Add the publish date
        
$_POST['comment_date'time();
        
$this->db->insert('comments',$_POST);    
        
redirect('/blog/post/'.$_POST['blog_id']);    
    

Any help welcome. Thanks for the advice so far.

Profile
 
 
Posted: 02 May 2009 07:23 AM   [ Ignore ]   [ # 4 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2674
Joined  05-18-2008

Seeing as its a date field, try using date() instead of time()

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
Posted: 02 May 2009 07:28 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  69
Joined  12-31-2008

Mmmm. Data submitted, but this error

A PHP Error was encountered

Severity
Warning

Message
date() expects at least 1 parameter0 given

Filename
controllers/blog.php

Line Number
49 
Profile
 
 
Posted: 02 May 2009 07:32 AM   [ Ignore ]   [ # 6 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2674
Joined  05-18-2008

try

$date date('c'); 

That will give you the full date and time, see PHP dtae for more options, the c option is near the bottom

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
Posted: 02 May 2009 07:35 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Rank
Total Posts:  69
Joined  12-31-2008

Excellent. Thanks so much. Worked a treat.

I was also dabbling with this…

function insert_comment() {    
$data[
'id'$this->db->where('blog_id',$this->uri->segment(3));
        
$now date("Y-m-d H:i:s");
  
$data = array( 
    
'author' => $this->input->xss_clean($this->input->post('author')),
    
'comment_text' => $this->input->xss_clean($this->input->post('comment_text')),
    
'comment_date' => $now
  
);

  
$this->db->insert('comments'$data);
redirect('/blog/post/'.$_POST['blog_id']);
    

Would there be any benefits? I know there are 100’s of different ways to write the same type of function, but I’m trying to write the best code I possibly can.

Thanks again!

Profile
 
 
Posted: 02 May 2009 07:43 AM   [ Ignore ]   [ # 8 ]  
Lab Assistant
RankRank
Total Posts:  115
Joined  10-19-2008

Hi,

Yes, there are many benefits to doing it that way. Inserting $_POST directly into your DB raises some security issues, so it is definitely better to define an array containing *only* the values you wish to insert.

Profile
 
 
Posted: 02 May 2009 07:47 AM   [ Ignore ]   [ # 9 ]  
Grad Student
Rank
Total Posts:  69
Joined  12-31-2008

Excellent. Learned so much already today.
Here’s my final code…

function insert_comment() {    
        
//get id from url
        
$id['blog_id'$this->db->where('blog_id',$this->uri->segment(3));
        
        
$now date("Y-m-d H:i:s");
        
//add data to array, clean data, and insert
        
$data = array( 
            
'blog_id' => $this->input->xss_clean($this->input->post('blog_id')),
            
'author' => $this->input->xss_clean($this->input->post('author')),
            
'comment_text' => $this->input->xss_clean($this->input->post('comment_text')),
            
'comment_date' => $now
        
);
        
        
$this->db->insert('comments'$data);
        
redirect('/blog/post/'.$_POST['blog_id']);
    
Profile
 
 
Posted: 02 May 2009 07:49 AM   [ Ignore ]   [ # 10 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2674
Joined  05-18-2008
Mike Ryan - 02 May 2009 07:43 AM

Hi,

Yes, there are many benefits to doing it that way. Inserting $_POST directly into your DB raises some security issues, so it is definitely better to define an array containing *only* the values you wish to insert.

Yeah it definatly better to do it this way, but there’s some code floating around the forums in the last week or so, which would take the post array, and return a data array with just those fields (it took the table name as a parameter, and extracted that data from the post array)

It works out as just a line of code, just like using $_POST directly, but much more safer

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
Posted: 02 May 2009 08:03 AM   [ Ignore ]   [ # 11 ]  
Grad Student
Rank
Total Posts:  69
Joined  12-31-2008

I’ll have a look. I’m only a beginner, so hopefully it’s easy enough.

THANKS

Profile
 
 
Posted: 02 May 2009 08:16 AM   [ Ignore ]   [ # 12 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2674
Joined  05-18-2008

The thread I was refering to is here
There are 3 possible implementations for the method, or make your own based on on the examples there (see post 6,8 and 10 for the code examples)

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
Posted: 02 May 2009 08:36 AM   [ Ignore ]   [ # 13 ]  
Lab Assistant
RankRank
Total Posts:  115
Joined  10-19-2008

Thanks for the link Dam1an. I extended my model class with a function to do this (although not as complex as Fuzzy’s), so I can just past $_POST to my model and let it worry about what fields to use. As an added complication, I use different field names in my DB/forms (for very good reasons!) so my function also renames them.

I love open-source communities, it’s great to see the different approaches people take to solve similar problems.

/**
 * Search through $_POST for fieldnames listed in
 * $fields, and return an array containing only those
 * fields. The controller can then pass the whole
 * $_POST (after validation) and let the model choose
 * which fields to use. Also translate html field name
 * to db field name
 * 
 * @param array $post Copy of the $_POST array
 * @param string $fields[][0] name of db field
 * @param string $fields[][1] name of html form field
 * @return array allowed fields/values only
 * 
 */

    
function get_values_from_post($post = array(), $fields = array())
    
{
        $settings 
= array();
        
        foreach(
$fields as $index => $value)
        
{
            
if (isset($post[$value]))
            

                $settings[$index] 
$post[$value];         
            
}
        }
        
return $settings;
    
Profile
 
 
Posted: 02 May 2009 09:37 AM   [ Ignore ]   [ # 14 ]  
Moderator
Avatar
RankRankRankRankRank
Total Posts:  2932
Joined  01-07-2008
Dam1an - 02 May 2009 06:57 AM

Why store it as a varchar instead of one of the many date/time formats such as timestamp or datetime?

Obviously that should’ve read int not varchar, sorry.
I don’t want to derail the thread, so short answer: I prefer manipulating PHP timestamps.  I almost never feel the need to do something that would properly use the mysql date field (i.e. sort by month), but I very frequently have to display the result in different formats.

Personal preference, really.

Mike, that last bit looks familiar.  One of my favorite snippets of all time (PHP 5):

function filter_input_data($data$allowed)
{
     $allowed 
array_flip($allowed);
     return 
array_intersect_key($data$allowed);
 Signature 
Profile
MSG
 
 
Posted: 04 May 2009 05:20 AM   [ Ignore ]   [ # 15 ]  
Grad Student
Rank
Total Posts:  69
Joined  12-31-2008

Thanks for the link and other snippets of code. Super!

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 148261 Total Logged-in Users: 69
Total Topics: 103041 Total Anonymous Users: 2
Total Replies: 515894 Total Guests: 480
Total Posts: 618935    
Members ( View Memberlist )