Forum for discussing general topics related to Couch.
9 posts Page 1 of 1
Hi! (It's been a while again :) )

On my search how to fetch pages from multiple masterpages I found these two explanations
http://www.couchcms.com/forum/viewtopic.php?p=20416#p20416
http://www.couchcms.com/forum/viewtopic.php?p=13982#p13982

What I would like to have is a random output and a limit of three pages. While the limit is easy, I don't get the random factor to work. I can't place " orderby='random' " within the cms:query nor does it work here
Code: Select all
<cms:pages masterpage=tname id=pid>
       <a href="<cms:show k_page_link />"><cms:show k_page_title /></a><br />
    </cms:pages>

So how do I randomly choose three pages from within multiple masterpages?
klaus wrote: Hi! (It's been a while again :) )

<cms:pages id=pid>

So how do I randomly choose three pages from within multiple masterpages?


Hi!
With this undelined, you always get the page you are asking for by id..
What you need is to remove this. And from then on the pages tag would fetch all pages in random order with orderby='random' . 3 random pages from 1 template need limit='3' also.
If you need 3 random pages from 3 templates, then make 3 times cms:pages with limit='1' and orderby='random'.
If you get into trouble, I will try to help here.
Thanks for your answer, but I don't really get it to work :/

Here is what I tried
Code: Select all
<cms:query
   sql="SELECT p.id pid, t.name tname
      FROM <cms:php>echo K_TBL_PAGES;</cms:php> p
      inner join <cms:php>echo K_TBL_TEMPLATES;</cms:php> t
      on p.template_id = t.id
      WHERE (t.name='architektur.php' or t.name='gestaltung.php' or t.name='fotografie.php')
      AND publish_date < '<cms:date format='Y-m-d H:i:s' />'
      AND NOT publish_date = '0000-00-00 00:00:00'
      ;"
   limit='3'
   >
   <cms:pages masterpage=tname orderby='random'>
   <article class="12u">
   <span class="image fit alt"><a href="#"><img src="images/pic01.jpg" alt="" /></a></span>
   <h2><a href="<cms:show k_page_link/>"><cms:show k_page_title/></a></h2>
   <p>Erat ac non lorem justo amet primis dolor adipiscing lacinia <cms:show k_template_title/></p>
   </article>
   </cms:pages>

</cms:query>


What I would like to get is 3 pages randomly choosen out of 3 templates. Like 2 from template1, 1 from template3. Or one from each, or 3 from template2.

Any ideas? :)
2 from template1, 1 from template3. Or one from each, or 3 from template2.

Looks like this:
Code: Select all
1. Find out how many templates are going to participate in output. Random(3).
2. a. If one template - cms:pages masterpage=template orderby='random' limit='3'
    b. If three templates - repeat 3 times cms:pages masterpage=template orderby='random' limit='1'
    c. If 2 templates:
              1) Find out the exact number of pages out of 3, which is fetched from _first_ template. Show them with cms:pages orderby='random' limit=step1_count
              2) If result of step1 is less than 3, then fetch the remaining (substracting step1_count)
Heck of a task, not possible with couch tags alone.
Now, when your question is cleared and presented in its full scope, let's hope someone helps you with php :D
Another take, that is not as dumb and straightforward as in my previous post.

1.Fetch all pages from all 3 templates
cms:pages masterpage=templateX ids_only='1'

2. And store a list of id-s with template id-s in 1 Array or String. So, it looks like:
'12_1', '13_1', '18_2', '6_2', '15_2', '1_3', '4_3'

3. Pick 3 from them with this: http://www.w3schools.com/php/func_array_rand.asp
Code: Select all
<?php
$a=array("red","green","blue","yellow","brown");
$random_keys=array_rand($a,3);
echo $a[$random_keys[0]]."<br>";
echo $a[$random_keys[1]]."<br>";
echo $a[$random_keys[2]];
?>
OR
Code: Select all
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r(array_rand($a,3));
?>

You'll need to adapt these php snippets!

4. As you have a string with 3 samples, like '15_1', '18_2', '6_2', they will be randomized also by templates and quantity of templates and pages. Just like you asked for.

5. Fire cms:each 2 times -
first with separator ', ' to get each item one by one
second with separator '_' to get page and template.

6. Output cms:pages masterpage=template id=id
7. ???
8. Profit!
I appreciate your willingness to help, but I guess I have to wait on KK's anwer as the problem should not be too hard to solve, just we two are missing the proper knowledge.

With this I can already access three different templates at once, and I guess the solution lies in changing what comes after "ORDER BY", only that "random" doesn't work there directly... :/
Code: Select all
<cms:query
    sql="SELECT p.id, p.template_id
        FROM <cms:php>echo K_TBL_PAGES;</cms:php> p
        inner join <cms:php>echo K_TBL_TEMPLATES;</cms:php> t
        on p.template_id = t.id
        WHERE (t.name='actors.php' or t.name='movies.php')
        AND publish_date < '<cms:date format='Y-m-d H:i:s' />'
        AND NOT publish_date = '0000-00-00 00:00:00'
        ORDER BY publish_date desc;" <!-- This might need to change -->
    limit='3'
    fetch_pages='1'
    >

    <!-- Displaying the pages in whatever manner -->

</cms:query>
Code: Select all
SELECT name
  FROM random
ORDER BY RAND()
LIMIT 1;
Hi,
I think you two were almost there :)
Following is what would work -
Code: Select all
<cms:query
    sql="SELECT p.id, p.template_id
        FROM <cms:php>echo K_TBL_PAGES;</cms:php> p
        inner join <cms:php>echo K_TBL_TEMPLATES;</cms:php> t
        on p.template_id = t.id
        WHERE (t.name='actors.php' or t.name='movies.php')
        AND publish_date < '<cms:date format='Y-m-d H:i:s' />'
        AND NOT publish_date = '0000-00-00 00:00:00'
        ORDER BY RAND();"
    limit='3'
    fetch_pages='1'
    >

    <!-- Displaying the pages in whatever manner -->
    <cms:show k_page_title /><br />
   

</cms:query>

Hope it helps.
Nice, thanks!!

Would it be possible to check for a custom field (it's within all templates) like " custom_field='project_value=2' " as well? And exclude a couple of page IDs? Im getting greedy, I know :D.
9 posts Page 1 of 1