1

I have a CodeIgniter web apps using foreach for user insert data then if the submitted data is successfully inserted, I created a flashdata message called sc_msg[]. But I have no idea how to call it on flashdata.

$user = $this->input->post('user', TRUE);
foreach ($user as $u)
{
   $this->model->insert($u);
   $sc_msg[] = '- User '.$u.'has been successfully registered<br>';
}

This is a code how I called it on my view.

<?php if ($this->session->flashdata('warning'))
        echo $this->session->flashdata('message'); ?>

I wanted the message called like this :

  • User abc has been successfully registered
  • User bca has been successfully registered
  • User qwe has been successfully registered
  • User ewq has been successfully registered

All sc_msg[] stored on message so I don't need to using FOR or FOREACH in the views to called it.

The problems is on my controller, I'm trying to set the message :

$this->session->set_flashdata('message', $sc_msg);

It's just print array, so I try to change $sc_msg into $sc_msg[] the result is error : Cannot use [] for reading.

I'm also trying to use foreach inside set_flashdata too and is failed. Unexpected foreach.

Then I'm trying to set the flashdata like this :

 $this->session->set_flashdata('message', $sc_msg[0].$sc_msg[1]);

That is printed the result but only index 0 and index 1. Result :

  • User abc has been successfully registered
  • User bca has been successfully registered

There is another 2 messages not called for user qwe and ewq.

I have no idea how to call the message inside the flashdata. But, please don't give me for loop solutions. Thank you.

1 Answer 1

1

I seen an easy way to do that Zinc.

Your message on sc_msg[] is already using <br> for next line. Okay, so what you need to do is not using foreach inside the flashdata. But using foreach to append the string from the message.

foreach ($sc_msg as $sm)
{
    $msg = $msg.$sm; // OR using $msg .= $sm;
}

$this->session->set_flashdata('message', $msg);

You can try it, If you have another way to append the string it's good. I just give an simple example for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.