Welcome Guest, Not a member yet? Register   Sign In
include with variable (solved)
#1

(This post was last modified: 02-18-2021, 02:56 PM by neoneeco.)

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!
Reply
#2

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 02-12-2021, 01:40 PM by neoneeco.)

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
Reply
#4

Use view instead of include.

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

Use View Cell?
Reply
#6

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

(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...
Reply
#8

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

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

Yes it works. Thank you !
Reply




Theme © iAndrew 2016 - Forum software by © MyBB