Cakephp 3 index of foreach loop

Hello World,

may i ask how can i find the index of foreach loop for a cakephp 3 html page

example
<?php foreach ($apples as $apple) : ?>
<?php foreach ($apple as $key=>$value) : ?>

        <?php    echo $key     ?> 
        

        <?php endforeach; ?>     
       <?php endforeach; ?>

is it possible? i tried a few versions but its not showing out.

can anyone help?

thanks

What are you trying to accomplish here? What do you expect the “index” will look like in your output?

hello Zuluru, long time no saying hello to you

i am experimenting on the Html label and id attribute

example ; https://codepen.io/munsheerek/pen/QmEWwq

after foreach the index, perhaps i can do something regarding image radio button which needs id and labels

example




<img src " alt=“Image 2”>




not sure if i am make it as follows. so far not successful

            <div class='col text-center'>
                <?php foreach ($apples['product_images'] as $apple)  :  ?>

                    <input type="radio" class="d-none imgbgchk" value="" name="imgbackground"  
                    id="<?php $apple->id?>">
                    
                    
                    <label for="<?php $apple->id?> ">   
                        <?php
                        echo $this->Html->image($imgPath, ['width' => '100']);
                        ?>
                        <div class="tick_container">
                            <div class="tick"><i class="fa fa-check"></i></div>
                        </div>
                    </label>
       
                <?php endforeach; ?>
            </div>
        </div>
    </div>

another experiment is not successful too. as follows

            <div class='col text-center'>
                <?php foreach ($apples['product_images'] as $apple)  :  ?>

                   <?php foreach ($apple as $keys=>$value)  :  ?>

                    <input type="radio" class="d-none imgbgchk" value="" name="imgbackground"  
                    id="<?php $value?>">
                    
                    
                    <label for="<?php $value. ?> ">   
                        <?php
                        echo $this->Html->image($imgPath, ['width' => '100']);
                        ?>
                        <div class="tick_container">
                            <div class="tick"><i class="fa fa-check"></i></div>
                        </div>
                    </label>
       
                <?php endforeach; ?>
                 </div>
            </div>
           </div>

i tried to debug () to see if foreach index comes out, but no…

second thing is the way i type id=“<?php $apple->id ?>” , i think its not showing anything out.

You need echo!
Or <?= $var; ?>

If you do just <?php $var; ?> you are not giving command to show it.

1 Like

you mean the html i have to put it like this

                 <id="<?=  $value ?> ">     


                 <label for="<?=  $value ?> ">      

to show them out?

this one yes !!! completed.!!!
thanks Zuluru

one more question about form control in another post

You’re welcome, but it wasn’t me that answered that. :slight_smile:

When you write

foreach ($apple as $keys=>$value)

what are you expecting to see in your $keys variable as it iterates through? It’s still not clear what you’re trying to accomplish here. This foreach is almost certainly not what you need, but to tell you what you do need, we first need to know what you want to see as output.