Welcome Guest, Not a member yet? Register   Sign In
How to loop through userdata?
#1

(This post was last modified: 01-24-2015, 05:48 PM by user2374.)

I have a form that once submitted, I am using to create an array of information that is used to fill a table on a results page. The validate function of the form puts all the info for the table into an array, which is saved as session userdata. Now I want to create a loop in my view page to fill a table with the saved user data. The elements in $query_array in my controller are stored in the order that I will be inserting them into the table. I can access any individual element of the array by using

PHP Code:
<?= $this->session->userdata['1']; ?>

but I cannot figure out how to replace '1' with the index of my for loop (ie, $j) on my view page. See code below:

PHP Code:
<tr>
 
               <td><?= $this->session->userdata['16']; ?> </td>
                <td class="txtcenter"><?= $this->session->userdata['17']; ?></td>
                <td class="txtcenter"><?= ucwords(str_replace('_'' '$this->session->userdata['query_state'])); ?></td>
                <td class="txtcenter"><img src="{site_url}assets/images/checklist.png" alt="checklist"  /></td>
 <td class="txtcenter"><?= $this->session->userdata['18']; ?>-***-****</td>
 <td class="txtcenter"></td>
 <td class="txtcenter"></td>
 <td class="txtcenter"></td>
 <td class="txtcenter"></td>
              </tr>
  
 <?php for($j 0$j $this->session->userdata['0']; $j++)
 
 { 
?>
 
 <tr>
                <td><?= $this->session->userdata['query_fullname7']; ?> </td>
                <td class="txtcenter"><?= $this->session->userdata['query_agerange3']; ?></td>
                <td class="txtcenter"><?= ucwords(str_replace('_'' '$this->session->userdata['query_state'])); ?></td>
                <td class="txtcenter"><img src="{site_url}assets/images/checklist.png" alt="subscriber benefits checklist"  /></td>
 <td class="txtcenter"><?= $this->session->userdata['query_phone3']; ?>-***-****</td>
 <td class="txtcenter"></td>
 <td class="txtcenter"></td>
 <td class="txtcenter"></td>
 <td class="txtcenter"></td>
              </tr>
  
  <?php ?>
  
  </tbody>
 </table> 

Note that the first row in the table, before the for loop, is just there to show how I am retrieving individual array elements. The for loop is after the first row, and the 'query_fullname7' type array elements within the loop are old, needing to be replaced by the array index ($j).

And here's the relevant code from my controller containing the array stored in userdata:

PHP Code:
$query_array = array(
 
 
'0' => $numberofrows,
 
 
'1' => $this->input->post('fname'),
 
'2' => $this->input->post('lname'), 
 
'3' => $this->_agegenerator($originalage),
 
'4' => $this->input->post('state'),
 
'5' => $this->_statephone($this->input->post('state')),

 
'6' => $this->input->post('fname'),
 
'7' => $this->input->post('lname'),
 
'8' => $this->_agegenerator($originalage),
 
'9' => $this->_statephone($this->input->post('state')),
 
 
'10' => $this->_createnames($tempfname$templname), 
 
'11' => $this->_agegenerator($originalage),
 
'12' => $this->_statephone($this->input->post('state')),
 
 
'13' => $this->_createnames($tempfname$templname), 
 
'14' => $this->_agegenerator($originalage),
 
'15' => $this->_statephone($this->input->post('state')),
 
 
'16' => $this->_createnames($tempfname$templname), 
 
'17' => $this->_agegenerator($originalage),
 
'18' => $this->_statephone($this->input->post('state')), 
 
 
'19' => $this->_createnames($tempfname$templname), 
 
'20' => $this->_agegenerator($originalage),
 
'21' => $this->_statephone($this->input->post('state')) 
 
 
 );
 
           $this->session->set_userdata($query_array);
 
           $this->session->set_flashdata('type'$searchtype);
 
 
redirect('/search/results''refresh'); 


I have tried the following to loop through the userdata:

PHP Code:
<?= $this->session->userdata[$j]; ?>
PHP Code:
<?= $this->session->userdata['$j']; ?>

What do I need to do to make this work?
Reply
#2

(01-24-2015, 04:57 PM)user2374 Wrote: And here's the relevant code from my controller containing the array stored in userdata:



PHP Code:
$query_array = array(
 
 
'0' => $numberofrows,
 
'1' => $this->input->post('fname'),
 
'2' => $this->input->post('lname'), 
 
'3' => $this->_agegenerator($originalage),
(
etc)
 ); 

I have tried the following to loop through the userdata:



PHP Code:
<?= $this->session->userdata[$j]; ?>
PHP Code:
<?= $this->session->userdata['$j']; ?>

What do I need to do to make this work?

I'm sorry if I am wrong, but it seems that you are creating an associative array where the keys are the strings '1', '2','3', etc. instead of the integers 1, 2, 3, etc. So when you try to retrieve with $j, which contains an integer, it fails.
Reply
#3

RobertSF Explained the technical reason why you are not getting any output by calling
Code:
$this->session->userdata[$j];
cause
Code:
$j = 17
but
Code:
array_key = '17'
there's difference in your datatypes. Solution would be to remove quotes from array keys when you save input->post in your controller or instead of single-quotes use double-quotes:
Code:
<?= $this->session->userdata["$j"]; ?>
Reply
#4

Thanks guys, you're right, I changed the array to a numeric array instead of associative and now it works. I'm just not familiar enough with PHP syntax yet to realize my mistake. It's working now!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB