Nested loops are not working in SS 4.x

Silverstripe Version:
Silverstripe 4.x

Question:
Loop issue with retrieving parent values inside child loop

Details of your query go here
I am facing a strange issue in Silverstripe version 4.x. In nested loop, child loop does not picking the value of parent loop.

`$get_parent = ['id'=>1,'name'=>test];
$get_child = ['class'=>'btech,'subject'=>'test'];`

`<% loop $get_parent %>
<% loop $get_child %>
	// i want get value of parent loop 
<% end_loop %>
<% end_loop %>`

What you are seeing sounds normal. Once you are in the child loop, you are within its scope so the parent data can’t be seen.

You should be able to access the parent data with the $Up method in your template, eg:

<% loop $get_parent %>
  <% loop $get_child %>
    $class <!-- the 'class' value of the current item in the child loop -->
	$Up.name  <!-- should provide the name of the current item in the parent loop -->
  <% end_loop %>
<% end_loop %>

The following docs page describes the way that scopes work in templates: https://docs.silverstripe.org/en/4/developer_guides/templates/syntax/#scope

this is not working

when i do this , i am also not getting $get_child loop value

when i do this like below

<% loop $get_parent %>
  <% loop $Up.get_child %>

$class <!--i am getting this -->
$name  <!-- but this is not -->

  <% end_loop %>
<% end_loop %>

i am only getting child loop value ,not parent loop value

It looks like you’re trying to pass a standard php array to the template. Try using an ArrayList : https://docs.silverstripe.org/en/4/developer_guides/model/lists/
SilverStripe\ORM\ArrayList | SilverStripe API

i am using like below to pass array to template
use SilverStripe\View\ArrayData;
use SilverStripe\ORM\ArrayList;

but not getting parent value

It seems like you’re getting some confusion over the scopes… in your example, when you do <% loop $Up.get_child %> then what you are seeing is expected… you won’t get any value for $name because you have changed scope again.

Could you post a bit more of your code? and maybe tell us what the final result is that you want to achieve? It will help us to provide some more meaningful advice.

in controller
<?php
public function editPostjob(){
$job_id = $this->getRequest()->getVar(‘id’);
$relief_job = “SELECT * FROM vismaad_jobs where id=$job_id”;
$relief_job_data = mysqli_query($this->connect,$relief_job);
$array = new ArrayList();
if(mysqli_num_rows($relief_job_data) > 0){
while($fetch = mysqli_fetch_assoc($relief_job_data)){
$days = date_diff(date_create($fetch[‘from_date’]),date_create($fetch[‘to_date’]));
$total_days = $days->format("%r%a");
$job_profile_id = $fetch[‘job_profile’];
$job_profile = “SELECT * FROM vismaad_job_profiles where id=$job_profile_id”;
$job_profile_data = mysqli_query($this->connect,$job_profile);
$fetch_profile = mysqli_fetch_assoc($job_profile_data);
$job_profile_title = $fetch_profile[‘title’];
$arrayData = new ArrayData(array(
‘id’ => $fetch[‘id’],
‘category’ => $fetch[‘category’],
));
$array->push($arrayData);
}
return $array;
}
}

public function getProfile() {
	$job_profile = "SELECT * FROM vismaad_job_profiles";
	$job_profile_data = mysqli_query($this->connect,$job_profile);
	$array = new ArrayList();
	if(mysqli_num_rows($job_profile_data) > 0){
		while($fetch = mysqli_fetch_assoc($job_profile_data)){
			$arrayData = new ArrayData(array(
				'id' => $fetch['id'],
				'title' => $fetch['title'],
			));
			$array->push($arrayData);
		}
		return $array;
	}
}
?>

in template.ss
<% loop $editPostjob %>
<% loop $Up.getProfile %>
$title

<% end_loop %>
<% end_loop %>