Jump to content


Photo

Selectors for page Field


  • Please log in to reply
7 replies to this topic

#1 vknt

vknt

    Full Member

  • Members
  • PipPipPip
  • 72 posts
  • 9

Posted 12 December 2011 - 02:49 PM

I'm doing a questionnaire with multiple choice questions, so I built pages with different fields of the type "Page" to store the choices.
I now want to build some metrics that show how many people choose a certain option but I don't understand how to use the selectors for fields of the type pages. I can't just use "any_field=any_value" because the field holds a Page.

So what I search is something like: Find al the pages that this Field with as the selected page this ID=SomeNumber

I hope I'm a bit clear in this. Can anyone help me with this?

Thx in advance!



#2 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,526 posts
  • 854

  • LocationVihti, Finland

Posted 12 December 2011 - 03:05 PM

Maybe looking for something like this:

<?php
$answer = $pages->get(1234); 

$questionnaires = $pages->find("question=$answer");

This assumes that 1234 is the "SomeNumber" in your example, and question is the "Field".

Probably easier version to maintain is to use paths:

<?php
$answer = $pages->get('/whois-father-of-luke-skywalker/anakin-skywalker/');


#3 vknt

vknt

    Full Member

  • Members
  • PipPipPip
  • 72 posts
  • 9

Posted 12 December 2011 - 03:50 PM

Thanks Apeisa, works like a charm. I didn't knew you could pass a Page into that string!

#4 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,526 posts
  • 854

  • LocationVihti, Finland

Posted 12 December 2011 - 04:05 PM

You actually need page there when looking for page values. Good to know is that you can also put PageArray there. So you could something like this to return all :

<?php
$positiveAnswers = $pages->find("name~=yes");
$questionnaires = $pages->find("question=$positiveAnswers");



#5 Soma

Soma

    Hero Member

  • Moderators
  • 3,194 posts
  • 1749

  • LocationSH, Switzerland

Posted 12 December 2011 - 04:54 PM

Thanks Apeisa, works like a charm. I didn't knew you could pass a Page into that string!


Congrats, you just opened a big new magic box there! Have fun ;)

@somartist | modules created | support me, flattr my work flattr.com


#6 Bill

Bill

    Jr. Member

  • Members
  • PipPip
  • 36 posts
  • 4

Posted 20 March 2012 - 04:53 PM

You actually need page there when looking for page values. Good to know is that you can also put PageArray there. So you could something like this to return all :

<?php
$positiveAnswers = $pages->find("name~=yes");
$questionnaires = $pages->find("question=$positiveAnswers");


I see many times on the forums where this 'find' feature is used... but everytime i see that successively done - i always tend to cringe - hence my question for clarification below:

"Is the above 2 liner - literally two separate queries on the dB? Or is it just a rifling thru what was already queried from the uber-query?"

Thanks.

#7 adamkiss

adamkiss

    Master of the universe

  • Moderators
  • 1,078 posts
  • 289

Posted 20 March 2012 - 05:15 PM

Looking into code (just briefly), it seems that that two liner are two queries :)

#8 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3122

  • LocationAtlanta, GA

Posted 21 March 2012 - 09:17 AM

In that fictional example there are two find() operations, so those aren't going to happen in 1 query, unless the results of 1 are already cached. If I recall, this should work (though not at a place to double check):

$pages->find("question.name=yes");

But I want to point out that query counting is usually a waste of time. And I used to do a lot of it, up until I read High Performance MySQL. MySQL will execute a 100 simple queries faster than it will 1 heavy one. So performance usually has much more to do with query quality and proper indexing than query quantity. This is not the way I was originally taught, and not how I worked for many years, so it took me awhile to get it. You can always $db->query() and join the original example into 1 query, making sure that everything happens using an index, but you may or may not see any tangible benefit for your efforts. If there is some real performance bottleneck that could be resolved by querying directly, I still do it, though it's pretty rare.

Also note that layers on top of MySQL are not unlike layers on top of languages. C++ is built on top of Assembler, and PHP is built on top of C++. PW is a layer on top of MySQL and PHP. It's possible to make lower layers perform faster than higher layers, if you know your way around them. Every layer is a compromise that has to be compared against the need. I know my way around MySQL and PHP, and for me PW lets me develop stuff 10x (or more) quickly than I could without it... and it's rare that I think it's costing me anything in performance. But if I had a project where maximizing the performance out of MySQL and PHP took precedence over all other factors, then I would keep out all other layers (no CMS, frameworks or other libraries).




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users