something like
$p = $pageArray->getField('id');
... where it would return all the id's of all child pages / pages within the pageArray?
I'm sure this already exists. thanks!
Edited by Bill, 20 March 2012 - 02:47 PM.
Posted 20 March 2012 - 02:46 PM
$p = $pageArray->getField('id');
Edited by Bill, 20 March 2012 - 02:47 PM.
Posted 20 March 2012 - 02:59 PM
// Let's assume $p is your 10 pages in a page array as you say, and $myfield is the field you're trying to filter on
$subset = $p->find("$myfield!=");Posted 20 March 2012 - 03:21 PM
Whyno for each? That what.it for. Already invented wheel is.
$ids=array(); foreach($pageArray as $p) $ids[]=$p->id;
Now bingo $ids have.what u need
$pageArray->children()->getAttr('id');
Posted 20 March 2012 - 03:27 PM
I don't think you could do it without iteration, but if you're trying to get a subset, you could possibly do something like this (though I've not tested it):
// Let's assume $p is your 10 pages in a page array as you say, and $myfield is the field you're trying to filter on $subset = $p->find("$myfield!=");
I seem to recall that that will return all pages where $myfield is not empty (as in it's not equal to "nothing").
$subset therefore now contains your filtered list. Let me know if that works.
My query would be why you would want to get a subset without iterating through them? Surely you have to iterate through them at some point to output some content?
$pagedIDs = $pageArray->children()->getAttr('id');
(pseudo-hack code Posted 20 March 2012 - 03:42 PM
// $p is your pagearray with 10 pages again
$childIDs = array();
foreach ($p->children as $child) {
$childIDs[] = $child->id;
}Posted 20 March 2012 - 03:58 PM
// $p is your pagearray with 10 pages again $childIDs = array(); foreach ($p->children as $child) { $childIDs[] = $child->id; }
Posted 20 March 2012 - 04:38 PM
Posted 20 March 2012 - 04:57 PM
I did end up having a lot of code that I needed to run to check various things on a site I'm currently building so I just went with the include file in head.inc like you say. Keeps things very neat and tidy in the template and is the easiest way of doing it.
Posted 20 March 2012 - 05:43 PM
abstract class PageArrayHelper
{
static function getAttr(PageArray $PageArray, $key='id')
{
$pageKeys = array();
foreach ($PageArray as $page)
{
$pageKeys[] = $page->$key; // all pages have one! <img src='http://processwire.com/talk/public/style_emoticons/<#EMO_DIR#>/smile.png' class='bbc_emoticon' alt=':)' />
}
return $pageKeys;
}
}
Then by just calling it as such...$IDs = PageArrayHelper::getAttr($randomChildren); // returns (array) of ids <img src='http://processwire.com/talk/public/style_emoticons/<#EMO_DIR#>/smile.png' class='bbc_emoticon' alt=':)' />
Posted 20 March 2012 - 07:40 PM
$rand_children = $pages->find("template=basic-page")->getRandom(3);
echo $rand_children; // returns 102|230|323
$ids = explode("|",$rand_children);
print_r($ids); // array with id's$ids = explode("|", $pages->find("template=basic-page")->getRandom(3));
@somartist | modules created | support me, flattr my work flattr.com
Posted 21 March 2012 - 07:49 AM
Bill, if I look at your code I just keep thinking that I would do this. And look! Without foreach!
$rand_children = $pages->find("template=basic-page")->getRandom(3); echo $rand_children; // returns 102|230|323 $ids = explode("|",$rand_children); print_r($ids); // array with id's
..so a one liner would be this:$ids = explode("|", $pages->find("template=basic-page")->getRandom(3));
Posted 21 March 2012 - 08:55 AM
I think you can use array_diff() to compare $pages->find("template=basic-page") with $pages->find("template=basic-page, limit=10") and return the difference.
I didn't test it, and can't do it now.
$children = $page->children('limit=3');
$ids = PageArrayHelper::getAttr($children,'id');
$randomChildNotInChildren = $page->children()->find("id!=".implode("|",$ids))->getRandom();
Posted 21 March 2012 - 10:49 AM
i'm getting WIERD 'churning without any results, blank screen' activity on my use of ->find(), or ->children() - when using the selector of "template=cool-page" (which is the page template i'm using for this particular page).
Just fyi - here's what i'm doing...
$IDs = $randomChildren->getAttr('id');
class YourModule implements Module {
static public function getModuleInfo() {
return array(
'title' => 'Your Module',
'version' => 100,
'summary' => 'Adds a getAttr() method to all PageArrays',
'singular' => true,
'autoload' => true
);
}
public function init() {
$this->addHook('PageArray::getAttr', $this, 'getAttr');
}
public function getAttr(HookEvent $event) {
$pageArray = $event->object;
$key = $event->arguments[0];
// ...the rest of your code goes here...
$event->return = $pageKeys;
}
}
Posted 21 March 2012 - 11:36 AM
This is not what it's supposed to do. This is probably the most common selector usage there is. Need some more information to determine what's going on here. Try turning on debug mode (/site/config.php) and edit the $config->debug line to be true. That should at least give a better sense of what error is getting thrown. Though if not, tell me more about this case. How many pages are there using 'template=cool-page' and how many 'autojoin' fields are part of it? If you are dealing with hundreds of pages you may want to place a limit=n on it.
This looks like a good approach. If you want to add your getAttr() method directly to the PageArray, so that you can call it like this:
$IDs = $randomChildren->getAttr('id');
...you can do it with an autoload module:// editted already as per your update above class YourModule extends Wire implements Module { static public function getModuleInfo() { return array( 'title' => 'Your Module', 'version' => 100, 'summary' => 'Adds a getAttr() method to all PageArrays', 'singular' => true, 'autoload' => true ); } public function init() { $this->addHook('PageArray::getAttr', $this, 'getAttr'); } public function getAttr(HookEvent $event) { $pageArray = $event->object; $key = $event->arguments[0]; // ...the rest of your code goes here... $event->return = $pageKeys; } }
Posted 21 March 2012 - 01:01 PM
Posted 21 March 2012 - 01:45 PM
continuing on this ePic journey...
how would i grab the $input->urlSegment1 stuff/lingo from within the module itself..
i init. did the lazy way of 'global $input' but got nuttin'
wire("input")->urlSegment1;
$wire->input->urlSegment1;
$this->input->urlSegment1;
@somartist | modules created | support me, flattr my work flattr.com
0 members, 0 guests, 0 anonymous users