Welcome Guest, Not a member yet? Register   Sign In
Insert form data + date stamp
#1

[eluser]debow[/eluser]
I have a form that post data most which is required. I'm wanting to time stamp each insert with just a year stamp. I have the insert working without the year stamp and I'm having issue try to figure out where I specify the date value and insert it with the same insert as the other form data.

Controller content is below.
Code:
function add()
    {        
        // Validate form
        $this->form_validation->set_rules('firstname', 'firstname', 'trim|required');
        $this->form_validation->set_rules('lastname', 'lastname', 'trim|required');
        $this->form_validation->set_rules('name', 'name', 'trim|required|callback_athlete_not_exist');
        $this->form_validation->set_rules('athEmail', 'email', 'trim|valid_email');
        $this->form_validation->set_rules('gender', 'type', 'trim|required');
        //|callback_athlete_not_exist
        
        if($this->form_validation->run())
        {
                
            // Validation passes
            $athId = $this->athlete_model->AddAthlete($_POST);
            
            if($athId)
            {
                $this->session->set_flashdata('flashConfirm', 'The athlete has been successfully added.');
                redirect('athletes');
            }
            else
            {
                $this->session->set_flashdata('flashError', 'A database error has occured, please contact your administrator.');
                redirect('athletes');
            }
        }
            $data['main_content'] = 'athletes/athletes_add_form';
            $this->load->view('includes/template', $data);
        //$this->load->view('users/users_add_form');
    }

Model content is below. I tried adding the info in red below but that's not working. Since date doesn't come from the form how do I get it inserted with the below insert?
Quote: function AddAthlete($options = array('date' => 'date("Y")'))
{
// required values
if(!$this->_required(
array('firstname',
'lastname',
'gender',
'name'
),
$options)
) return false;

$options = $this->_default(array('athStatus' => 'active'), $options); //declares all users active by default


$this->db->insert('athlete', $options);

$last_id = $this->db->insert_id();

$athId = $last_id;
return $athId;

}

Thanks for any help.
#2

[eluser]bretticus[/eluser]
Since I have no idea what $this->_default() does with your $options array, I'll assume this might work:

Code:
$options = $this->_default(array(‘athStatus’ => ‘active’), $options);

$options['date'] = date(“Y”);
    
$this->db->insert(‘athlete’, $options);

The $options argument in your AddAthlete method (a method is a function in a class) is overwritten by passing $_POST. Thus the default you are showing will never pass in the code you have shown. This is how PHP is supposed to work.

You *might* take a look in your _default() method and perhaps put the date code there (since it would seem like that is the place where extra data is attached to your insert ($options) array?)
#3

[eluser]debow[/eluser]
Since I'm new to both CI and PHP I'm not sure if that's the best way but your suggestion does work and gives me the results I wanted. Going to also look at putting it in the default section.

Thank you very much.




Theme © iAndrew 2016 - Forum software by © MyBB