![]() |
How to loop through userdata? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: How to loop through userdata? (/showthread.php?tid=875) |
How to loop through userdata? - user2374 - 01-24-2015 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> 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( 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? RE: How to loop through userdata? - RobertSF - 01-24-2015 (01-24-2015, 04:57 PM)user2374 Wrote: And here's the relevant code from my controller containing the array stored in userdata: 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. RE: How to loop through userdata? - stopz - 01-26-2015 RobertSF Explained the technical reason why you are not getting any output by calling Code: $this->session->userdata[$j]; Code: $j = 17 Code: array_key = '17' Code: <?= $this->session->userdata["$j"]; ?> RE: How to loop through userdata? - user2374 - 01-26-2015 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! |