Soma Posted June 6, 2014 Share Posted June 6, 2014 It also depends if output formatting for the pages is on or off. If it's OFF a $page->field may be an object or array but when ON the same might be a string or a single object instead of an array. Good luck. 1 Link to comment Share on other sites More sharing options...
bwakad Posted June 6, 2014 Author Share Posted June 6, 2014 Thanks for the thorough explanations! I will delve myself deeper in php (hope I get out though) lol Link to comment Share on other sites More sharing options...
Soma Posted June 6, 2014 Share Posted June 6, 2014 I think we are not really done yet. You mentioned that those fields are page fields? Well if the values all are page fields, this means you know the all the values ahead. There's simple ways to get the distinct count using this reference. After all it's like a field relation from one page to another and PW can use that in selectors to find pages matching a page in selected in a page field. But this can be done in different ways and direction depending on what you need this can make a big difference. Loop all values for a page field and filter then count pages that matches (maybe not ideal in your case), or loop all pages and collect and count the values of fields, kinda what we were doing above. And others. On the last one if you are using page fields, you somehow only need to know the ID of the page selected to be able to count (got it?), but its value(!) is, as we know it is a selected page (Page object), so you also need to know the field that makes the value, the one you usually output. This is usually the title, but can be also any other field on that selected pages. After all you could even have your fields array, you define previous to loop them through, specify the field name for the "value" this field is using as value. Uff, does this still make sense? I'm not sure how your setup is and how you'll go about to define what fields, so again just an example code. Assuming your page fields are all single value selects (they'd have to be for this to make sense). $somePages = $pages->find("template=child-templates, somefield=somevalue"); $fieldsArray = ("province" => "title", "network" => "count"); // "fieldname" => "fieldname_onselectedpage" $result = array(); foreach($fieldsArray as $field => $subfield){ foreach($somePages as $p) { if(isset($result[$field][$p->$field->$subfield])) $result[$field][$p->$field->$subfield]++; else $result[$field][$p->$field->$subfield] = 1; } } So $p->$field->$subfield would translate to $p->province->title $p->network->count Which is the selected page's "title" or for the network field "count" (maybe an integer field). Looping through all values of a field in your case, since those are pages would mean to maybe also loop values that are not used at all in those pages. So maybe not always desired depending on the pro and cons, and maybe also amount of pages you deal with. I think in this case just try and see what happens, there should be no very noticeable slowdown if you run this code until let's say 10 fields over 500 pages or 2-3 fields over 1000 pages. Hard to say out of the blue. If there's a lot more that need to be looped, I think you would need to consider some other direct sql queries ($db->query("SQLSTATEMENT"), $database->query("PDOSTATEMENT") On a sidenote I find it more and more very hard to commuicate such "complex" thing just for the terminology of all the things, like pages, selected pages, fields when having to deal with page arrays and page fields. I never know if the other person understands the same. Anyway. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now