Welcome Guest, Not a member yet? Register   Sign In
How to get a value out of a function after a query insert with CodeIgniter
#1

[eluser]randomdriver[/eluser]
Below is my code. After the save() function puts the data into the database I need to use the $code variable in another place via email.

This first snippet is in my models folder:

Code:
function save()
    {
        $this->load->helper('string');
        $code = random_string('unique');
        
        $new_insert_data = array(
            'userid' => $this->session->userdata('userid'),
            'categoryid' => $this->input->post('category'),
            'sender' => $this->input->post('sender'),
            'sendto' => $this->input->post('sendto'),
            'message' => $this->input->post('message'),
            'code' => $code
        );
        
        $insert = $this->db->insert('data', $new_insert_data);
        return $insert;
    }

Here is my controller:

Code:
function send()
    {
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('sender', 'Name', 'trim|required');
        $this->form_validation->set_rules('sendto', 'Send to', 'trim|required|valid_email');
        if($this->form_validation->run() == FALSE)
        {
            
            $this->index();
        }
        
        else
        {
            
            
            // Load the model so we can insert the data into the database
            $this->load->model('model');
            
            // Insert the data into the database
            if($query = $this->model->save())
            {
                                        
                //Now send the message via email
                $this->load->library('email');

                $this->email->from('[email protected]', 'Gmail');
                $this->email->to($this->input->post('sendto'));

                $this->email->subject('You have a message);
                $this->email->message('Hello,
                
                You have a message from'. $this->input->post('sender').'
                
                To view visit the following link:
                 http://www.site.com/index.php/view/message'$CODE);

                $this->email->send();

                //echo $this->email->print_debugger();
                
                //Send the user back to the page
                redirect('site');
            }
            else
            {
                redirect('wtf');
            }
        }

As you can see, as soon as its saved into the DB via the model, I need to use the generated code in the email where I have $CODE. Any help appreciated.
#2

[eluser]affix[/eluser]
Well If you want it to return true or false on success you would do this in your model

Code:
function save()
    {
        $this->load->helper('string');
        $code = random_string('unique');
        
        $new_insert_data = array(
            'userid' => $this->session->userdata('userid'),
            'categoryid' => $this->input->post('category'),
            'sender' => $this->input->post('sender'),
            'sendto' => $this->input->post('sendto'),
            'message' => $this->input->post('message'),
            'code' => $code
        );
        
        if($this->db->insert('data', $new_insert_data))
        {
           return true;
        }
        else
        {
           return false;
        }
    }

Then in the controller

Code:
function send()
    {
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('sender', 'Name', 'trim|required');
        $this->form_validation->set_rules('sendto', 'Send to', 'trim|required|valid_email');
        if($this->form_validation->run() == FALSE)
        {
            
            $this->index();
        }
        
        else
        {
            
            
            // Load the model so we can insert the data into the database
            $this->load->model('model');
            
            // Insert the data into the database
            if($this->model->save())
            {
                                        
                //Now send the message via email
                $this->load->library('email');

                $this->email->from('[email protected]', 'Gmail');
                $this->email->to($this->input->post('sendto'));

                $this->email->subject('You have a message);
                $this->email->message('Hello,
                
                You have a message from'. $this->input->post('sender').'
                
                To view visit the following link:
                 http://www.site.com/index.php/view/message'$CODE);

                $this->email->send();

                //echo $this->email->print_debugger();
                
                //Send the user back to the page
                redirect('site');
            }
            else
            {
                redirect('wtf');
            }
        }


That should return TRUE if the save function succeeds, False if otherwise Smile

Hope I have helped
#3

[eluser]randomdriver[/eluser]
Thanks for the reply, not sure how switching to return TRUE; achieves what I need. To be more clear,

I need this code in the model:
Code:
$code = random_string('unique');

To go to this code in the controller, AFTER it is interested into the database:
Code:
$this->email->message('Hello,
                
                You have a message from'. $this->input->post('sender').'
                
                To view visit the following link:
                 http://www.site.com/index.php/view/message'$CODE);
#4

[eluser]jedd[/eluser]
[quote author="randomdriver" date="1254257733"]To be more clear,

...
[/quote]

You could probably be even clearer, if you wanted, and no one would mind.

Are you suggesting that the privacy of a given message will be solely limited to the obscure nature of the $CODE here?

That's a bit .. messy.

If you really want to go down that path, why not just generate a UUID for each message? Easy to store in the message table, plays nice in a URL, etc.
#5

[eluser]BrianDHall[/eluser]
To achieve what you want it seems you'll need a class property somewhere, either in the model or controller.

So the model loads the $code into itself, then in the email message you access it with $this->model->code.

I presume you would want to save the value of code in the model anyway, or at least in the data to be saved. Though you wouldn't have to I suppose - $code would just be available for the duration of the script that way.

Was that what you were wanting? Otherwise, I'm not getting what you are after either.
#6

[eluser]randomdriver[/eluser]
Not sure what to say at this point. Rather than critiquing my method of creating a UUID I would just like some help on how to get a variable (called $code in this example) in my model function to carry over to my controller's function so they match up without having to do extra queries to MySQL. Seems very strait forward, I just don't know how to do it. I can then worry about how I want to generate UUIDs next, looking at other services like Evite, their IDs for public pages is pretty complex:

http://www.evite.com/pages/invite/viewIn...KHQHPYHQCM
#7

[eluser]randomdriver[/eluser]
[quote author="BrianDHall" date="1254262934"]To achieve what you want it seems you'll need a class property somewhere, either in the model or controller.

So the model loads the $code into itself, then in the email message you access it with $this->model->code.

I presume you would want to save the value of code in the model anyway, or at least in the data to be saved. Though you wouldn't have to I suppose - $code would just be available for the duration of the script that way.

Was that what you were wanting? Otherwise, I'm not getting what you are after either.[/quote]

Yes, just saw this after my last reply. This is what I am getting at. The steps are:

- Generate a Unique Code
- Insert it and other info into the db
- Email that Unique Code to a person

My goal was to avoid having to insert into the database, then query the code back out of the database, seems like extra work and CPU when the code was just created in the first place.

I think I follow what you are saying and will try. Alternatively, are you able to pass code to the function? Like this?

Code:
$code = 123;
$this->model->save($code);

//model:
function save($code)
{
  $insertcode = $code;
  INSERT INTO MYSQL
}
#8

[eluser]BrianDHall[/eluser]
[quote author="randomdriver" date="1254263644"][quote author="BrianDHall" date="1254262934"]I think I follow what you are saying and will try. Alternatively, are you able to pass code to the function? Like this?

Code:
$code = 123;
$this->model->save($code);

//model:
function save($code)
{
  $insertcode = $code;
  INSERT INTO MYSQL
}
[/quote]

Sure, that would work just fine too.

When I'm accessing model info its always in object form, (like $user = new User() ), so there it is just easier to use a class variable as without a corresponding row in the DB it won't get saved anyway.

No method is particularly better than another, so pick whichever requires less code changes Smile
#9

[eluser]randomdriver[/eluser]
[quote author="BrianDHall" date="1254264367"][quote author="randomdriver" date="1254263644"][quote author="BrianDHall" date="1254262934"]I think I follow what you are saying and will try. Alternatively, are you able to pass code to the function? Like this?

Code:
$code = 123;
$this->model->save($code);

//model:
function save($code)
{
  $insertcode = $code;
  INSERT INTO MYSQL
}
[/quote]

Sure, that would work just fine too.

When I'm accessing model info its always in object form, (like $user = new User() ), so there it is just easier to use a class variable as without a corresponding row in the DB it won't get saved anyway.

No method is particularly better than another, so pick whichever requires less code changes Smile[/quote]

Got it! Thanks for your help! As someone trying to self-teach themselves without formal training it is very helpful and I appreciate it.
#10

[eluser]alboyd[/eluser]
What a confusing set of responses to your initial question! I'm not even sure what the final answer was?!

I think in the end - the suggestion was that you create your $code in the controller and pass this to the model function to insert into the database - then in the controller you can continue on using the $code for other purposes?

The other option with what you initially had would be to check the result of the query ($insert i think you called it) - if OK return $code otherwise return FALSE.




Theme © iAndrew 2016 - Forum software by © MyBB