Jump to content

excluding parent's title


fnode
 Share

Recommended Posts

Hello Community,

SETUP:

Fields I have (uses:Page), (instances:Page) and (categories:Page). Please see the attached file.

[ uses ] Selection of fields to render. See the attached file # 2

[ instances ] "Default" data for pages. See the attached file # 2

[ categories ] A bunch of items :)

CODE:

Renders the selected field from [ uses ] to the template. It's a simple way of not hard-coding the fields in your page. NOTE: Pagefields() is a function inside /site/templates/home.php

ISSUE:

I do not know how to exclude the parent's field ($fo->title) from field ($page->instances) (Categories). Which is foreach($page->{strtolower($p->title)} as $fo){} thus returning $fielddata. I want to get only the titles from ($fo->categories) and not the (Categories) title. I hope I am making sence. See the attached file # 3.

Field (uses)

Example: I select a field called (categories) from [ uses ] it outputs the title "2D". For this to work the current page or template has to have that field from [uses] or it will not work. See the attached file # 2

<?php
function Pagefields(Page $page, Pages $pages, Fields $fields){  
  $fielddata = array();

  foreach($page->uses as $p){

  var_dump($p->title); // OUTPUTS. instances, categories

  foreach($page->{strtolower($p->title)} as $fo){
	 $fieldr = strtolower($fo->title);

	 var_dump($fo->title); // OUTPUTS. Categories, 2D

	 $fielddata[] = $fo; // Returns the title (Categories) which I don't want. I do need to return the titles inside Categories with a field called (categories).

	 if($page->get($fieldr)!=NULL){
		foreach($fo->{$fieldr} as $f1){
		   $fielddata[] = $f1;
		   var_dump($f1->title); // OUTPUTS. Blender (software), CSS 3, HTML 5, Design, Development, 2D, 3D, Vehicle Design. See the attached file # 3
		}
	 }#endof: if($page->get($fieldr)!=NULL){}
  }

  } #endof: foreach($page->uses as $p){}

  return (object) $fielddata;
}

foreach(Pagefields($page, $pages, $fields) as $p){
  var_dump($p->title);
}

HARD-CODED VERSION: This is the other way of doing my code above. I do not want to hard-code the field names in my home.php file

I have debug set to TRUE and I get the following error.

<?php
foreach($page->instances as $p){
  foreach($p->categories as $cat){
  var_dump($cat->title); // OUTPUTS. Blender (software), CSS 3, HTML 5, Design, Development, 2D, 3D, Vehicle Design. See the attached file # 3
  }
}

## Warning: Invalid argument supplied for foreach() in line 2 foreach($p->categories as $cat){}

post-118-0-42408400-1332954887_thumb.png

Link to comment
Share on other sites

Try to this. It will output the categories titles from the pages you selected under instances.

foreach($page->instances as $inst) {
foreach($inst->categories as $cat) echo $cat->title;
}

While I don't fully understand what you're trying to do here, I think it looks too complicated. Why are you having a page select "uses" to select what fields are below on the same page? I'm sure it works but think this extra step could be saved.

EDIT: Still trying to understand your code. It could be that the foreach $page and again foreach $page is the problem.

Link to comment
Share on other sites

Ah ok. :) And why do you want to change the field names? Anyway interesting, though something I wouldn't do, but don't care about me :)

Still trying to wrap my mind around it. I think since you name it "Select Instance" it must be single page select field type? So there would be no foreach possibly for field "instances". That why the hard coded doesn't work.

foreach($page->instances->categories as $car) {
  echo $cat->title;
}

Only multiple page select field will return page array of the selected pages even if there's only 1 selected.

Link to comment
Share on other sites

It will be easy to change field name for every particular project. If I do not like the field name (instances) I can change to (references), something like that. The "Select Instance" field has to be in a foreach I have more (instances) in that list. For example: Categories, and Tags.

Link to comment
Share on other sites

This was the one you got the error:

## Warning: Invalid argument supplied for foreach() in line 2 foreach($p->categories as $cat){}

But does this code work or not?

foreach($page->instances->categories as $car) {
	  echo $cat->title;
}
Link to comment
Share on other sites

And if you do?

if(count($page->instances)){
foreach($page->instances as $instance) {
	foreach($instance->categories as $cat){
		echo $cat->title;
	}
}
} else if($page->instances->categories){
  foreach($page->instances->categories as $cat) {
   echo $cat->title;
  }
} else {
echo "no instances";
}

***

Edited code

Link to comment
Share on other sites

And if you do?

if(count($page->instances)){
foreach($page->instances as $instance) {
	foreach($instance->categories as $cat){
		echo $cat->title;
	}
}
} else if($page->instances->categories){
  foreach($page->instances->categories as $cat) {
   echo $cat->title;
  }
} else {
echo "no instances";
}

Yes, that works for the first loop foreach($page->instances as $instance) {} it OUTPUTS. Blender (software), CSS 3, HTML 5, Design, Development, 2D, 3D, Vehicle Design. See the attached file # 3

EDIT : It does not work in foreach($instance->categories as $cat){ } if I add another instance in (instances). I have Categories, and Tags

Link to comment
Share on other sites

then how about like this?

function Pagefields(Page $page){  

$fielddata = new PageArray();

foreach($page->uses as $p){
	foreach($page->get($p->name) as $fo){
		if(count($fo->get($fo->name))){
			foreach($fo->get($fo->name) as $f1){
				$fielddata->import($f1);
			}
		}
	}
}
return $fielddata;
}

foreach(Pagefields($page) as $p){
echo "<p>$p->title</p>";
}

.... edited code a little.

Link to comment
Share on other sites

I think I slowly gettin the problem you're running into with this approach. How you're assuming the page field on the "Categories" is named? It works only if you name them always the same as the page that holds the field selected in "instances". So you chose the strtolower($fo->title) to be the field name for the inner foreach, the last one. Instead you would require to do another page select on the "Categories" page to define what the field is called on itself. But I wouldn't want to go as far.

Also you added the $fo to the $fielddata before the last foreach, that's why it got prepended. So you add it and at the same time you say you don't want it to be added. Or I'm missing something? :)

Also you could (assuming they're always pages to return) make $fielddata a PageArray and store (import) the pages in there to return. That also kinda shows you're not really returning fielddata but page objects. I'm still not sure about this approach, if there's an easier way or should be done differently.

  • Like 1
Link to comment
Share on other sites

Yes, you are correct I had to use strtolower($fo->title) for it to match or it will not work. $fielddata needs to returned data in each foreach loop.

then how about like this?

function Pagefields(Page $page){  

$fielddata = new PageArray();

foreach($page->uses as $p){
	foreach($page->get($p->name) as $fo){
		if(count($fo->get($fo->name))){
			foreach($fo->get($fo->name) as $f1){
				$fielddata->import($f1);
			}
		}
	}
}
return $fielddata;
}

foreach(Pagefields($page) as $p){
echo "<p>$p->title</p>";
}

.... edited code a little.

My code

<?php

function Pagefields(Page $page){

 $fielddata = new PageArray();

  foreach($page->uses as $p){

  //var_dump($p->title); // OUTPUTS. instances and categories

  foreach($page->get($p->name) as $fo){
	 //var_dump($fo->title); // OUTPUTS. Categories and 2D

	 // I need to $fielddata also in this foreach loop, so it returns (categories) in this page.
	 // Or, it will not return [2D] as the selected item in categories.
	 // But is not returning
	 $fielddata->import($fo);

	 // I changed the name to match with (categories) it was category before. So, I do not use strtolower($fo->title) function.
	 if(count($fo->get($fo->name))){
		foreach($fo->get($fo->name) as $f1){
		   var_dump($f1->title); // Yes, It outputs. Blender (software), CSS 3, HTML 5, Design, Development, 2D, 3D, Vehicle Design. No errors if I add another instance in (instances).
		   $fielddata->import($f1);
		}
	 }
  }
  } #endof: foreach($page->uses as $p){}

 return $fielddata;
}

foreach(Pagefields($page) as $p){
 //echo "<p>$p->title</p>";
 //var_dump($p->title);
}

Thank you for helping me.

EDIT : Attached file Pic #1 Pagefields > has 2 children assets and instances, it has 2 children categories and tags. Instances Page holds Categories and Tags, as you can see in Attached file # 1 if one of them matches I will get its data. Attached file Pic #1

Link to comment
Share on other sites

You could try something like this:

function Pagefields(Page $page){  

       $fielddata = new PageArray();

       foreach($page->uses as $p){
               foreach($page->get($p->name) as $fo){
                       if($fo->get($fo->name) && count($fo->get($fo->name))){
                               foreach($fo->get($fo->name) as $f1){
                                       $fielddata->import($f1);
                               }
                       } else {
                           $fielddata->import($fo);
                       }
               }
       }
       return $fielddata;
}

foreach(Pagefields($page) as $p){
       echo "<p>$p->title</p>";
}
  • Like 1
Link to comment
Share on other sites

Thank you, Soma for your help. It's working now. The code you gave me, helped me. I just added the the following:

<?php
if($p->numChildren){

foreach($page->get($p->name) as $fo){
		if(count($fo->get($fo->name))){
		   foreach($fo->get($fo->name) as $f1){
			  $fielddata->import($f1);
		   }
		}
	 }
} else {
  foreach($page->get($p->name) as $fo){
   $fielddata->import($fo);
}

FROM YOUR CODE

then how about like this?

function Pagefields(Page $page){  

$fielddata = new PageArray();

foreach($page->uses as $p){
	foreach($page->get($p->name) as $fo){
		if(count($fo->get($fo->name))){
			foreach($fo->get($fo->name) as $f1){
				$fielddata->import($f1);
			}
		}
	}
}
return $fielddata;
}

foreach(Pagefields($page) as $p){
echo "<p>$p->title</p>";
}

.... edited code a little.

MY FINAL VERSION

<?php

function Pagefields(Page $page){

 $fielddata = new PageArray(); // Nice !

 foreach ($page->uses as $p){

  if($p->numChildren){ // line added

	 foreach($page->get($p->name) as $fo){
		if(count($fo->get($fo->name))){
		   foreach($fo->get($fo->name) as $f1){
			  $fielddata->import($f1);
		   }
		}
	 }

  } else { // lined added
	 foreach($page->get($p->name) as $fo){
		$fielddata->import($fo);
	 }
  }

  } #endof: foreach($page->uses as $p){}

 return $fielddata; // Return me some data!
}

foreach(Pagefields($page) as $p){
 echo "<p>$p->title</p>"; // Yes, I got data!
}

Thank you again for your big help. Awesome Community! ;)

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...