Jump to content

Method(s) to cycle through foreach/compare values of Children


creativejay
 Share

Recommended Posts

I think this is an API question more than a PHP one.

I have a repeater and several tables, and I cannot seem to access the fields I want. The following code throws no error but $prod_vid_bandwidth_lo is empty:

$field = array();
$childrenByField = array();
foreach($children as $child) $field[$child->prod_vid_bandwidth->band_lo][] = $child; // <-- THIS LINE
foreach($field as $f => $items) {
	$childrenByField[] = "$f";
}
$prod_vid_bandwidth_lo = implode(" OR ", $childrenByField);	

While the following reference outputs an error calling band_lo an undefined constant:

$child->prod_vid_bandwidth.band_lo

And this gets us back to no error but no output:

$child->prod_vid_bandwidth->get("band_lo")

How do I access, in the context of this code, the content of a field within a FieldtypeTable or FieldtypeRepeater of the child page?

 

Thanks so much! I'm almost there!

 

(and for the record I decided not to iterate through the fields and try to define them based on fieldtype... I'm going through the fields manually, and it's shaved time off the load. Wish I'd just sucked it up and did that way back when I first started. Spent a lot of time beating my head over how to do this with a minimum number of foreach loops.)

Link to comment
Share on other sites

What kind of field is prod_vid_bandwidth?

 

59 minutes ago, creativejay said:

While the following reference outputs an error calling band_lo an undefined constant:


$child->prod_vid_bandwidth.band_lo

You have a typo. There shouldn't be a period there, it should be an object operator -> like others. Unless I'm mistaken period is used for string concatenation only.

Link to comment
Share on other sites

Thanks for taking a look, abdus.

Quote

What kind of field is prod_vid_bandwidth?

prod_vid_bandwitdth is a table. band_lo is an integer column in that table.

Quote

You have a typo. There shouldn't be a period there, it should be an object operator -> like others.

I was showing everything I tried in the above. -> did not work, either.

Link to comment
Share on other sites

So, it's similar to this one?

5995c41e4e70b_ClipboardImage.thumb.jpg.624c0c28485bc04e9b28c28f51a232bb.jpg

Since $child->prod_vid_bandwidth will return a TableRows object, you cant simply do $page->tableField->column, but you can foreach over the TableRows object or extract any column you want

5995c5207d259_ClipboardImage(1).thumb.jpg.25606b2819946e0c671a82b8978a8621.jpg

Link to comment
Share on other sites

I think I get it now. You want to group children by values of a column

Something like this one works:

5995c7ec3757a_ClipboardImage(3).thumb.jpg.ffbdd685daef0e67e961891a2f0e0bd5.jpg

 

So this code

$parent = $pages->get(1017);

$groupedByLow = [];
foreach($parent->children as $child) {
    foreach($child->prod_vid_bandwidth as $item) {
        $groupedByLow[$item->band_lo][] = $child->name;
    }
}
ksort($groupedByLow); // sort by band_lo
dump($groupedByLow);

 

Groups children like this (I used name field for presentation)

array (3)
    10 => array (1)
        0 => "new-car" (7)
    20 => array (2)
        0 => "old-car" (7)
        1 => "new-car" (7)
    60 => array (1)
        0 => "old-car" (7)

You may need to check for null inside the second foreach.

  • Like 2
Link to comment
Share on other sites

		$field = [];
		$childrenByField =[];
		foreach($children as $child) {
			$tableCount = count($child->prod_vid_bandwidth);
			if($tableCount>0) { 
				foreach($child->prod_vid_bandwidth as $item) {
				$value = number_format($item->band_lo,1,'.','');
				$field[$value][] = $child;
			}
		}    }    
		foreach($field as $f => $items) {
			$childrenByField[] = "$f";
		}
		if(!empty($childrenByField)) $prod_vid_bandwidth_lo = implode(" OR ", $childrenByField);
		
		
		$field = [];
		$childrenByField =[];
		foreach($children as $child) {
			$tableCount = count($child->prod_vid_bandwidth);
			if($tableCount>0) { 
				foreach($child->prod_vid_bandwidth as $item) {
				$field[$item->lo_unit][] = $child;
			}
			}}
		foreach($field as $f => $items) {
		    $childrenByField[] = "$f";
		}
		if(!empty($childrenByField)) $prod_vid_bandwidth_loU = implode(" OR ", $childrenByField);	
		

		$field = [];
		$childrenByField =[];
		foreach($children as $child) {
			$tableCount = count($child->prod_vid_bandwidth);
			if($tableCount>0) { 
				foreach($child->prod_vid_bandwidth as $item) {
				$value = number_format($item->band_hi,1,'.','');
				$field[$value][] = $child;
			}
			}}
		foreach($field as $f => $items) {
		    $childrenByField[] = "$f";
		}
		if(!empty($childrenByField)) $prod_vid_bandwidth_hi = implode(" OR ", $childrenByField);	
		
		$field = [];
		$childrenByField =[];
		foreach($children as $child) {
			$tableCount = count($child->prod_vid_bandwidth);
			if($tableCount>0) { 
				foreach($child->prod_vid_bandwidth as $item) {
				$field[$item->hi_unit][] = $child;
			}
			}}
		foreach($field as $f => $items) {
		    $childrenByField[] = "$f";
		}
		if(!empty($childrenByField)) $prod_vid_bandwidth_hiU = implode(" OR ", $childrenByField);	
		unset($tableCount);

Here's the code that finally output my table fields. Note that my floats were being converted to integers so I had to force the display of their decimal places with number_format().

Originally, I hoped to cycle through the fields and output standardized strings of HTML based on their type and structure, but in the end I am omitting too many fields, and need to specialize the output code a little too much to make that feasible. So I am working my way through 170 fields, but hopefully this will put me ahead in this project when I'm done (toward integrating a Pages2PDF tie-in from these fields later), rather than being as behind as I feel!

 

Thanks so much to everyone who chimed in to give me a hand!

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...