0
\$\begingroup\$

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');
}
\$\endgroup\$

migrated from stackoverflow.com Dec 15 '17 at 4:20

This question came from our site for professional and enthusiast programmers.

0
\$\begingroup\$

I know that it is different answer related to that question but its efficient and simple. You can set the array like this:

$userDetails = array();  // declare the array

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])) {
            $userDetails["user_name$i"] = array('userid'=> $name[$i], 'is_reg'=>1); //set array that user is already register
            $this->session->set_flashdata('UserData', $userDetails);
       }
       //if not registered
       else {
          $this->model->insert($name[$i]);
          $userDetails["user_name$i"] = array('userid'=> $name[$i], 'is_reg'=>0); //set array that user is not already register
         $this->session->set_flashdata('UserData', $userDetails);
       }
   redirect('registerpage');
}

Get array

$userdata = $this->session->flashdata('UserData');
print_r($userdata);
\$\endgroup\$
  • \$\begingroup\$ Hi, thank you for your answers. How about this code ? $userDetails['user_name1'] $userDetails['user_name2'] $userDetails['user_name3'] and how to retrieve it ? thank you dude. \$\endgroup\$ – Zinc Nov 7 '17 at 13:34
  • \$\begingroup\$ I have changed the answer. Please check now and let me know. \$\endgroup\$ – Anand Pandey Nov 9 '17 at 6:20
  • \$\begingroup\$ I will check it now.. Thank you for your help Anand ! \$\endgroup\$ – Zinc Nov 9 '17 at 6:44
  • \$\begingroup\$ That's working well Anand ! I have another question about php & javascript, can you help me ? \$\endgroup\$ – Zinc Nov 9 '17 at 6:50
  • \$\begingroup\$ Yes please but ask as a separate question \$\endgroup\$ – Anand Pandey Nov 9 '17 at 6:51
1
\$\begingroup\$

This seems more like a code review question (like Alive to Die comments), so I'm not entirely sure what you are needing. For the most part, your code looks fine. I have a few things to consider, but they are mainly preferential.

You could adjust the View output within the form.

<?php 
    for($i=0; $i<$max; $i++) { 
        echo '<label>Username ' . $i . ' </label><input type="text" placeholder="username" name="username[' . $i . ']>';
    }
?>

In your Controller, you could also initiate $a and $b differently, as mentioned in this Stack Overflow article: Assign same value to multiple variables.

$a = $b = 1;
\$\endgroup\$
  • 1
    \$\begingroup\$ Thank you for remembering me about $a = $b = 1;, I was forgot it. \$\endgroup\$ – Zinc Nov 7 '17 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.