Jump to content
creativejay

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

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.)

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Thank you abdus, this seems to be what I need. I am working on getting the output exactly as I want it, per the example array to string above, but I can confirm that it build an array containing the information I need!

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By louisstephens
      So I reread my first draft, and it made absolutely no sense (I deleted it to hopefully better explain myself).  I am trying to make a system (that to me is a bit complicated) utilizing jquery and processwire together. My whole goal is to put a url like https://domain.com/launch?first_name=jim&occupation=builder in a script tag on another site(just a localhost .php page) to then pull out the data for that person and append to divs etc. Basically, the initial script tag would point to "launch" which has a content-type of "application/javascript". Using jquery, I would pull out the persons name and occupation and then make a specific ajax get request to "domain.com/api" (in json format) for a look up of the person. Essentially then I could pull that particular person's information from the json data, and do with it how I please in the "launch" page.  In processwire, I have a page structure like:
      People -Jim Bob (template: person ) --Occupations (template: basic-page) ---Builder (template: occupation) ---Greeter (template: occupation) It is really just a bunch of people with their occupations and a few fields to the occupation template. With the "api" (template: api) url, I was hoping to return all the data (of people) in json format like:
      Example Format:
      { "id": 1, "title": "Jim Bob", "occupations": { "builder": { "id": 44, "title": "Builder", "years_worked": 1, "etc": "ect", }, "Greeter": { "id": 44, "title": "Greeter", "years_worked": 1, "etc": "ect", }, } } Where I get lost is really outputting the page names and nesting in the occupations into json. I have used Pages2JSON before, but I was a bit lost on how to implement what i was thinking.
       
      I have access to all the local host files, but I was hoping to kind of build out a "system" where I could place the script tag/parameters in any project, and be able to interact with the data without doing an ajax call on the actual site. In a way, this would keep processwire handling all the data and requests, and my other "projects" just with a simple script tag. This might all be way too much/over complicated, but I couldn't quite wrap my head around how to achieve it. 
    • By louisstephens
      From my last post, I was given a good idea on how to count the repeater items, and it worked wonderfully. I got my code working well and the columns (based on the count) all work well as well. Now, I have a head scratcher on my hands. 
      <?php $buttonsIncluded = $page->special_custom_buttons->find('special_custom_buttons_include=1'); $buttonsIncludedCount = count($buttonsIncluded); $buttonsIncludedCountAdditional = $buttonsIncludedCount +1; echo $buttonsIncludedCount; ?> <div class="row"> <?php foreach($buttonsIncluded as $button): ?> <?php if($button->custom_buttons_include): ?> <?php if($buttonsIncludedCountAdditional == 2): ?> <div class="col-6"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php elseif($buttonsIncludedCountAdditional == 3): ?> <div class="col-4"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php elseif($buttonsIncludedCountAdditional == 4): ?> <div class="col-3"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php endif; ?> <?php endif; ?> <?php endforeach; ?> </div> All of this is included in a larger foreach statement that is pulling in other data (like body copy etc etc) from a Page Table field. As you can see in my code above, I am adding "1" to the count, so I can have space in the grid layout for a new button.
      So, right now: it looks something like: 
      [repeater button] [repeater button] [repeater button] [space for new button] What I really need to do is to pull in the button from the Page Table and add it into the new space so it looks like:
      [repeater button] [repeater button] [repeater button] [button from Page Table] Is this even possible todo, or is there a better way to go about this? 
       
      *Edit*
      So, I really just overlooked something quite easy here. Since the grid is based on 12 columns, I could just take 12 and divide by $buttonsIncludedCountAdditional which would give me the remaining col width to use outside the foreach loop. I was trying to make this too complicated.
    • By rolspace.net
      Hi all
      I have a problem here. I created a gallery with 240 pictures. Created an images field with no maximum amount (0). Unfortunately, only 98 of the 240 images show on the website. Any idea what I possibly could have done wrong? Thanks for your help!
      <?php foreach ($page->images as $image) { $options = array( 'quality' => 90, 'upscaling' => false ); $thumb = $image->size(250, 250, $options); ?> <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6 col-6 foto"> <a href="<?= $image->url ?>" data-lightbox="lightbox" > <img src="<?= $thumb->url ?>" alt="" > </a> </div> <?php } ?>  
    • By celfred
      Hello !
      I have somehting I don't understand here... Here's my code :
      $allPlayers = $pages->find("parent.name=players, team=$selectedTeam"); $allTrains = $allPlayers->find("template=event, task.name~=ut-action, refPage!='', date>=$startDate, date<=$endDate, sort=refPage, sort=date"); bd('$allTrains:'.$allTrains->count()); // DISPLAYS 0 ???? foreach($allPlayers as $p) { $allTrainings = $p->find("template=event, task.name~=ut-action, refPage!='', date>=$startDate, date<=$endDate, sort=refPage, sort=date"); $test += $allTrainings->count(); } bd('$test:'.$test); // DISPLAYS 883 pages (normal) As you can read from my comments, I have no idea why my first $allTrains stays at 0 while the second request actually finds the corresponding pages. If someone could explain I'd appreciate a lot. I have been struggling with this for hours now... For your information, my pages having template 'event' are in a subtree like so :
      - player 01
        - history-1
          - event 01
         - event 02
         - event ...
        - history-2
          - event 01
          - event ...
      - player 02
        - history-1
          - event 01
         - event 02
         - event ...
        - history-2
          - event 01
          - event ...
      - player ...
        - history-1
          - event 01
         - event 02
         - event ...
        - history-2
          - event 01
          - event ...
       
      Thanks in advance. (sorry for my preceding 'tree' which doesn't look like much. I need to find a way to output this better 😉 )
    • By Sten
      Hi,
      I have a lot of difficulties with getting a field value.
      First I created a template, inside I created several fields
      two fields are a dropdown select box So I have a value and a label for each line.
      Then I want to get the field value in _init.php or in the template to display it. How can I get the value of a field for the page ? I need to understand the flow through which data goes.
      Could someone help me ?
      Thank you
       
       
       
×
×
  • Create New...