Jump to content
Peter Knight

Children of children listed by parent heading

Recommended Posts

I have a new challenge - outputting the grandchild pages of 3 children (simple enough) but confine the display of each grandchild under it's parent heading.

Will try explain...

My tree looks like this:

Foods  (where I want to mirror the structure below in a table)

   - Fruit (Child of Foods and Level 2 Parent)
   - -  Apple 
   - -  Banana
   - -  Orange

   - Vegetable (Child of Foods and Level 2 Parent)
   - -  Carrot
   - -  Potato
   - -  Cauliflower

   - Meats (Child of Foods and Level 2 Parent)
   - -  Beef
   - -  Pork
   - -  Chicken

Lets say "Foods" is the parent.
Fruit, Veg and Meat are children.
Apple, Bananna etc are grand children

 

 

I have the Foods template working to an extent but it's doing the following. IE it's 

Fruit
- -  Apple
- -  Banana
- -  Orange
- -  Carrot
- -  Potato
- -  Cauliflower 
- -  Beef
- -  Pork
- -  Chicken

Vegetable
- -  Apple
- -  Bananna
- -  Orange
- -  Carrot
- -  Potato
- -  Cauliflower 
- -  Beef
- -  Pork
- -  Chicken

Meats
- -  Apple
- -  Bananna
- -  Orange
- -  Carrot
- -  Potato
- -  Cauliflower 
- -  Beef
- -  Pork
- -  Chicken

 

This is my effort so far.

<?php
		// the children of this page
		$products = $page->children();
		
		foreach ($products as $prod){
		
		// Create a table for each child page	
		echo"
			<div>
				<div class='prod-ov-wrapper'>
				
					<div class='prod-ov-text'>
					{$prod->title}
					
					<table class='uk-table uk-table-divider' border=1>
					<thead>
						<tr>
							<th>Part No.</th>
							<th>Length</th>
							<th>Break Force</th>
							<th>Width</th>
							<th>Colour</th>
						</tr>
					</thead>
					<tbody>
			";
					// populate the table with details of the children of each child	
					
					$proditem = $page->find("parent=$page->children, template=product"); 
					foreach ($proditem as $prodDetail){
						echo"
							<tr>
							<td>{$prodDetail->prod_partNo}</td>
							<td>{$prodDetail->prod_length}</td>
							<td>{$prodDetail->prod_breakForce}</td>
							<td>{$prodDetail->prod_width}</td>
							<td>{$prodDetail->title}</td>
							</tr>
							";}
				echo"			
						
						
					</tbody>
				</table>
					
					
					
					
					</div>
				</div>
			</div>
    		
			
		";}
		?>

 

I've tried a couple of things over the last few hours and just can't quite understand the API call I need at 

$proditem = $page->find("parent=$page->children, template=product"); 
foreach ($proditem as $prodDetail){
echo"

 

is there a way to do this? I suspect I am close but need to add another variable in there?

 

 

 

 

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

I have it working - thanks again @ottogal

Eventually I stripped back everything and started again with this

foreach($page->children as $category) {
 echo "...
";
foreach($category->children as $prod) 
{
echo "

 

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 Noel Boss
      EDIT:
      What I'm actually really looking for is a way to edit and add children of a page in a similar way as RepeaterMatrix fields:
      Easy to add new ones and easy to move, hide and modify inline:

      ––––
      I have a problem and am so close to a solution, but everything i've found so far only gets me about 95% and before I create something on my own want to ask is anyone knows a soltion or if I miss something.
      My simple requirement: Editing Children and creating new ones inside the current parent page context – eighter inline or in a Lightbox.
       
      Not working Option A:
      With page relation fields I can create new pages – but it requires me to select a fixed parent. If I don't provide a parent, no new link is present.

      Not working Option B:
      With the awesome AdminPageFieldEditLinks Module I can create new Pages directly without the need to provide a parant beforehand – but then I need to select it at runtime.

      There is a new link now – the link is: parent_id=0&amp;template_id=51&amp;modal=1 … if it only provided the correct parent_id!!!

      But now i have to select the parent:

      Not working Option C :
      That's what I do right now… Reordering the Child-Field to the first tab – but to maintain the context, I would need the edit and new button to open a lightbox instead of a page reload.
       
      Possible solutions:
      For A+B: If I could provide a selector as parent, this would solve my issue: "parent=page.id" or a checkbox "use parent page for new pages". Or I could use some hook?
      For C : I could write my own javascript to open the links in a Lightbox.
       
    • By kaba86
      Hello PW Community, really glad that discovered this CMS recently, it is very strange it took so long That idea of no front design limitations is just awesome!
      Need to say that I have a bit of knowledge of html and css, but almost no php, so I need your help.
      What I want to do is an article posting  cms, with this structure:
       
      - Homepage - Projects - Articles -- Category 1 --- Articles of category 1 -- Category 2 --- Articles of category 2 - About - Contact Found this ProcessWire Profile https://github.com/tutsplus/how-to-create-an-ajax-driven-theme-for-processwire
      It covers almost all my needs, except the menu. When I add a childpage for this page http://artist.nicegrp.com/publications/world-world/ , World:World doesn't appear under Writings & Publications.
      I need a menu that works like a breadcrumb, that shows on the menu the category that you are viewing. So when I'm in articles page, on the menu it shows only articles and it's categories. When I get into a category, that category takes state active link but doesn't show on the menu links and titles for contained articles. How can I do that?
      Sorry for my long writing and English, it is not my native but I hope you understood what I need. Can you help me with that?
      Thank you
       
    • By gerald
      Hi all,
      I use version 3.0.62. Is there a way to delete all children of a page with one click - without parent page?
      thanks
    • By John W.
      SYNOPSIS
      A little guide to generating an sitemap.xml using (I believe) a script Ryan originally wrote with the addition of being able to optionally exclude child pages from being output in the sitemap.xml file.
      I was looking back on a small project today where I was using a php script to generate an xml file, I believe the original was written by Ryan. Anyway, I needed a quick fix for the script to allow me to optionally exclude children of pages from being included in the sitemap.xml output.
      OVERVIEW
      A good example of this is a site where if you visit /minutes/ a page displays a list of board meetings which includes a title,  date, description and link to download the .pdf file.
      I have a template called minutes and a template called minutes-document. The first page, minutes, when loaded via /minutes/ simply grabs all of its child pages and outputs the name, description and actual path of an uploaded .pdf file for a visitor to download.
      In my back-end I have the template MINUTES and MINUTES-DOCUMENT. Thus:


      So, basically, their employee can login, hover over minutes, click new, then create a new (child) record and name it the date of the meeting e.g. June 3rd, 2016 :

       
      ---------------------------
      OPTIONALLY EXCLUDING CHILDREN - SETUP
      Outputting the sitemap.xml and optionally excluding children that belong to a template.
      The setup of the original script is as follows:
      1. Save the file to the templates folder as sitemap.xml.php
      2. Create a template called sitemap-xml and use the sitemap.xml.php file.
      3. Create a page called sitemap.xml using the sitemap-xml template
       
      Now, with that done you will need to make only a couple of slight modifications that will allow the script to exclude children of a template from output to the sitemap.xml
      1. Create a new checkbox field and name it:   sitemap_exclude_children
      2. Add the field to a template that you want to control whether the children are included/excluded from the sitemap. In my example I added it to my "minutes" template.
      3. Next, go to a page that uses a template with the field you added above. In my case, "MINUTES"
      4. Enable the checkbox to exclude children, leave it unchecked to include children.
      For example, in my MINUTES page I enabled the checkbox and now when /sitemap.xml is loaded the children for the MINUTES do not appear in the file.

       
      A SIMPLE CONDITIONAL TO CHECK THE "sitemap_exclude_children" VALUE
      This was a pretty easy modification to an existing script, adding only one line. I just figure there may be others out there using this script with the same needs.
      I simply inserted the if condition as the first line in the function:
      function renderSitemapChildren(Page $page) { if($page->sitemap_exclude_children) return ""; ... ... ...  
      THE FULL SCRIPT WITH MODIFICATION
      <?php /** * ProcessWire Template to power a sitemap.xml * * 1. Copy this file to /site/templates/sitemap-xml.php * 2. Add the new template from the admin. * Under the "URLs" section, set it to NOT use trailing slashes. * 3. Create a new page at the root level, use your sitemap-xml template * and name the page "sitemap.xml". * * Note: hidden pages (and their children) are excluded from the sitemap. * If you have hidden pages that you want to be included, you can do so * by specifying the ID or path to them in an array sent to the * renderSiteMapXML() method at the bottom of this file. For instance: * * echo renderSiteMapXML(array('/hidden/page/', '/another/hidden/page/')); * * patch to prevent pages from including children in the sitemap when a field is checked / johnwarrenllc.com * 1. create a checkbox field named sitemap_exclude_children * 2. add the field to the parent template(s) you plan to use * 3. when a new page is create with this template, checking the field will prevent its children from being included in the sitemap.xml output */ function renderSitemapPage(Page $page) { return "\n<url>" . "\n\t<loc>" . $page->httpUrl . "</loc>" . "\n\t<lastmod>" . date("Y-m-d", $page->modified) . "</lastmod>" . "\n</url>"; } function renderSitemapChildren(Page $page) { if($page->sitemap_exclude_children) return ""; /* Aded to exclude CHILDREN if field is checked */ $out = ''; $newParents = new PageArray(); $children = $page->children; foreach($children as $child) { $out .= renderSitemapPage($child); if($child->numChildren) $newParents->add($child); else wire('pages')->uncache($child); } foreach($newParents as $newParent) { $out .= renderSitemapChildren($newParent); wire('pages')->uncache($newParent); } return $out; } function renderSitemapXML(array $paths = array()) { $out = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; array_unshift($paths, '/'); // prepend homepage foreach($paths as $path) { $page = wire('pages')->get($path); if(!$page->id) continue; $out .= renderSitemapPage($page); if($page->numChildren) { $out .= renderSitemapChildren($page); } } $out .= "\n</urlset>"; return $out; } header("Content-Type: text/xml"); echo renderSitemapXML(); // Example: echo renderSitemapXML(array('/hidden/page/'));  
      In conclusion, I have used a couple different processwire sitemap generating modules. But for my needs, the above script is fast and easy to setup/modify.
      - Thanks
       
×
×
  • Create New...