CodeIgniter Forums
Looping over $this->request - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Looping over $this->request (/showthread.php?tid=76433)



Looping over $this->request - stlake2011 - 05-12-2020

Good Morning everyone,

Quick question. Is it possible to loop over  $request object?

example:
PHP Code:
foreach($this->request as $key => $value)
{
    print $key."<br>";


Reasoning for this question is I have a form with quite few fields, that all I want to check is if they are is set or empty and a foreach/for loop seems to make more sense then writing out a few dozen isset()/empty() statements for each field. 

I know it can be done with the raw $_POST data but I want to do this with the inbuilt request object because of a few advantages it does offer, if at all possible.

Unless of course, there is functionality builtin that does this that I have just not seen/found yet. If that's the case, where can I find it?


RE: Looping over $this->request - niklas - 05-12-2020

Hi,

I don't tested but maybe it helps you to name your form-fields as array with name="blabla[]" and use php array_filter function.

Array-Form Example:
https://stackoverflow.com/questions/9073690/post-an-array-from-an-html-form-without-javascript

And Array Filter description:
https://www.php.net/manual/de/function.array-filter.php


RE: Looping over $this->request - jreklund - 05-12-2020

You need to loop over $this->request->getPost(). The proper way are using the Validation tool for this however, but nothing is stopping you for doing it that way.