I need help about CodeIgniter $this->session->set_flashdata
array.
I think my code is so long, is there any way to simplify my code?
Script at my views
<?php
for($i=0; $i<$max; $i++) {
if($this->session->flashdata("'warning".$i."'"))
echo $this->session->flashdata("'warning".$i."'");
if($this->session->flashdata("'info".$i."'"))
echo $this->session->flashdata("'info".$i."'");
}
?>
<form action="xxxx/insertMember" method="post">
<?php for($i=0; $i<$max; $i++) { ?>
<label>Username <?php echo $i; ?></label>
<input type="text" placeholder="username" name="username[<?php echo $i; ?>]>
<?php } ?>
<button type="submit">Submit</button>
</form>
Script at my controller
...
parent::__construct();
$max = 5;
public function insertMember() {
....form was validated & return true...
$name = $this->input->post('username',TRUE);
$a = 1;
$b = 1;
for($i=0; $i<$max; $i++) {
if($name[$i] != '') {
//check username was registered on database or not.
$doCheck[$i] = $this->model->check($name[$i]);
//if already registered
if(!empty($doCheck[$i])) {
$this->session->set_flashdata("'warning".$a."'",$name[$i].' was registered');
$a++;
}
//if not registered
else {
$this->model->insert($name[$i]);
$this->session->set_flashdata("'warning".$b."'",$name[$i].' sucessfully registered');
$b++;
}
redirect('registerpage');
}