Welcome Guest, Not a member yet? Register   Sign In
How to use variable in flash message
#1

[eluser]debow[/eluser]
Can someone help with the below issue. All I'm wanting to do is use the users first name that gets deleted in the message after they are deleted.

Code:
function pdelete($options = array()) {
        // required values
        if (!$this->_required(array('athId'), $options))return false;
        
        $name = $options['athfirst'];
        
        //$this->db->where('athId', $options['athId']);
        //$this->db->delete($this->athlete);
        
        $message = "<p class='successmsg'> User .$name.  has been deleted</p>";
        $this->session->set_flashdata('message', $message);
        
    }

The $name is not showing up at all. I know I doing something wrong but not sure what. I've tried it many different ways.
#2

[eluser]cideveloper[/eluser]
So I assume when you run this

Code:
echo $this->session->flashdata('message');

you get a response like this "User has been deleted"

have you tried just showing the passed in options array to make sure the values are being passed into function pdelete properly.

Code:
function pdelete($options = array()) {
        print_r($options);
        // required values
        //if (!$this->_required(array('athId'), $options))return false;
        
        //$name = $options['athfirst'];
        
        //$this->db->where('athId', $options['athId']);
        //$this->db->delete($this->athlete);
        
        //$message = "<p class='successmsg'> User .$name.  has been deleted</p>";
        //$this->session->set_flashdata('message', $message);
        
    }

Also could you show the result of the above.
#3

[eluser]debow[/eluser]
The response I get is "User has been deleted" The result from the code you gave is just 1. I assume that's because there is only 1 record being deleted but I could be wrong. I don't have to have my message display this way but I like the ideal of it giving me more info on what was deleted.
#4

[eluser]cideveloper[/eluser]
if you are getting "1" as a response to the print_r($options), you are not passing an array to your pdelete function. Can you show what is calling that function. Is "1" the id of the user you want to delete?
#5

[eluser]debow[/eluser]
Ok, so I've had this setup many diff ways. Here's the full flow now.

In my view I have a table showing all the athletes. I have actions such as view/update/del.

In the controller that loads that view I have the actions items anchored with this.

Code:
anchor('athletes/delete/' . $athlete->athId, 'del', array('class' => 'delete', 'onclick' => "return confirm('Are you sure want to delete this user?')"))

If I click on del it then calls the below function. This is how it looks now a little diff then the options array I was passing. Both worked by deleting the user. Its just a matter of getting my message the way I'd like.

Code:
function delete($athId) {
        
        $data['athlete'] = $this->athletes_model->GetAthlete(array('athId' => $athId));
        if (!$data['athlete'])
            redirect('athletes');
        
        $name = $this->data->athfirst; //doesn't work
        $name = athlete->athfirst; //doesn't work

        // delete user
        //$this->athletes_model->delete($athId);
        //$this->athletes_model->delete(array('athId' => $athId));
        
        //$message = "<p class='successmsg'> User $name  has been deleted</p>";
        //$this->session->set_flashdata('message', $message);
        
        redirect('athletes');
    }
#6

[eluser]cideveloper[/eluser]
Is $this->athletes_model->GetAthlete(array('athId' => $athId)); returning an array with athfirst, athId, etc.?

than you would access $name like this $data['athlete']['athfirst']
#7

[eluser]debow[/eluser]
Yes, I tried using this

Code:
$name = $data['athlete']['athfirst'];
        
        $message = "<p class='successmsg'> Athlete $name was deleted. Thank You!</p>";
        $this->session->set_flashdata('message', $message);

But got the following error.

Fatal error: Cannot use object of type stdClass as array in othd\controllers\athletes.php on line 161
#8

[eluser]cideveloper[/eluser]
so you are not returning an array. try this.

Code:
$name = $data['athlete']->athfirst

I am only guessing here and can help more if I can see GetAthlete function in athletes_model Model.
#9

[eluser]debow[/eluser]
Thank you very much that worked. I'll look at my query and try to better understand why I wasn't returning an array but thought I was.

Thanks.
#10

[eluser]cideveloper[/eluser]
[quote author="debow" date="1305080448"]Thank you very much that worked. I'll look at my query and try to better understand why I wasn't returning an array but thought I was.

Thanks.[/quote]

NP. You dont have to return a result array. I never do. I like returning object like you did.

check out this

but then I would change

Code:
$data['athlete'] = $this->athletes_model->GetAthlete(array('athId' => $athId));
$name = $data['athlete']->athfirst;

to the simplified version

Code:
$ath_data = $this->athletes_model->GetAthlete(array('athId' => $athId));
$name = $ath_data->athfirst;




Theme © iAndrew 2016 - Forum software by © MyBB