CodeIgniter Forums
include with variable (solved) - 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: include with variable (solved) (/showthread.php?tid=78597)



include with variable (solved) - neoneeco - 02-12-2021

Hello,

I would like to know how to pass a variable in an include.

Indeed when I do the classic way codeigniter returns me an error.


PHP Code:
                <?php if (!empty($galeries) && is_array($galeries)) :

                    foreach ($galeries as $galerie) :

                ?>
                    <?= $this->include('includes/include_card'?>

                    <?php endforeach; ?>

                <?php else : ?>

                    // No galerie

                <?php endif ?>

thank you in advance for any help!


RE: include with variable - InsiteFX - 02-12-2021

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs.
Any variables available at that line in the calling file will be available within the called file, from that point forward.
However, all functions and classes defined in the included file have the global scope.


RE: include with variable - neoneeco - 02-12-2021

Thank you for your reply. 

However i am not sure i understood correctly. I still have the same error ==> see attached file


It's like the foreach loop variable doesn't exist in the include.

Anyway i got around the problem by putting the foreach in the include, it works but not exactly what i wanted

attachements :

https://ibb.co/mFMGMVX


RE: include with variable - waleed - 02-12-2021

Use view instead of include.

Like
echo view('includes/include_card',$galerie)


RE: include with variable - nfaiz - 02-13-2021

Use View Cell?


RE: include with variable - ikesela - 02-13-2021

pass thru controller ( then return thru view -> load include using view)


RE: include with variable - neoneeco - 02-18-2021

(02-12-2021, 08:37 PM)waleed Wrote: Use view instead of include.

Like
echo view('includes/include_card',$galerie)

Hi,
Sorry but don't work (same error)
(02-13-2021, 12:03 AM)nfaiz Wrote: Use View Cell?
I don't want to use controller
(02-13-2021, 11:18 AM)ikesela Wrote: pass thru controller ( then return thru view -> load include using view)
It's the same thing, unless I didn't understand correctly

If there are others ideas...


RE: include with variable - paulbalandan - 02-18-2021

This should work. Change your include part to this one.

PHP Code:
<?= $this->setData(compact('galerie'))->include('includes/include_card'?>



RE: include with variable - neoneeco - 02-18-2021

Yes it works. Thank you !