• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass data to a partial inside a loop

#1
Hi

This is the first time I'm using CI and I have a questiona about getting data inside a partial within a loop.

On the controller I call the view with an array of data:

PHP Code:
return view('numero'$data); 

In the main view I have:

PHP Code:
<?php foreach ($items as $item) : ?>

<?= $item['id']; ?>

<?= $this->include('partials/item'?>

<?php endif ?>

In the partial I have:
PHP Code:
<?= $item['name']; ?>

and I get an error saying "Undefined variable: item"

How am I able to make this work?
Thanks
Reply

#2
(06-21-2020, 04:46 AM)nneves Wrote: Hi

This is the first time I'm using CI and I have a questiona about getting data inside a partial within a loop.

On the controller I call the view with an array of data:

PHP Code:
return view('numero'$data); 

In the main view I have:

PHP Code:
<?php foreach ($items as $item) : ?>

<?= $item['id']; ?>

<?= $this->include('partials/item'?>

<?php endif ?>

In the partial I have:
PHP Code:
<?= $item['name']; ?>

and I get an error saying "Undefined variable: item"

How am I able to make this work?
Thanks

Hello !
This question has already been answered in the user manual. Carefully study the code blocks that were provided as an example, especially pay attention to the block with the code that is called in the view itself on line No. 9, here's the record:
PHP Code:
<?php foreach ($todo_list as $item):?>
in any case, look carefully at how your keys are named in the $ data array. If memory serves me right and you want to iterate over the entire $data array, then you just need to specify this variable instead of your $item.
Read the:
User_Guide#Creating-Loops

p.s. I have not coded for a long time and therefore I can be mistaken in my last sentence.

P.S. Sorry my English.
I would change this world, but God doesn't give me the source.
Reply

#3
Hi @Digital_Wolf

Thanks for your feedback.
The interaction works when everything is inside the main template and stop working when I place the variable inside a partial template.

I think that loops only work if they are in the same template or partial, I have loops inside partials but the stop working when the loop includes the partial like I wrote in the first post.

Thanks
Reply

#4
(06-22-2020, 01:32 AM)nneves Wrote: Hi @Digital_Wolf

Thanks for your feedback.
The interaction works when everything is inside the main template and stop working when I place the variable inside a partial template.

I think that loops only work if they are in the same template or partial, I have loops inside partials but the stop working when the loop includes the partial like I wrote in the first post.

Thanks
The documentation says little about the "$this->include("partials/item")" method, and I myself haven’t used it since I don’t have duplicate code, but the same documentation mentions that you can transfer data exactly the same way as in controllers, that is, after the line with the file name "'partials/item'", you can specify the data you want to transfer after the comma, it will look something like this (although I'm not sure):

PHP Code:
<?= this->include("partials/item"$item['name']) ?>
and already process the data in the "partials / item" file itself...
I would change this world, but God doesn't give me the source.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.