0

Just like in demo of kirby

<?php foreach(page('projects')->children()->visible()->limit(3) as $project): ?>

I want to make dynamic limit of records. How can i do that?

I have tried JavaScript but it not worked.

Here is JavaScript code that no worked

<script> 
  var p1 = 3;
  function load()
  {
    p1=p1+3;
  }                 
</script>

<?php
$number="<script>document.write(p1)</script>";
// echo $number; 
<?php foreach(page('projects')->children()->visible()->limit($number) as $project): ?>
  //Code Here
<?php endforeach ?>

<div class="text-center"><a href="#" class="load-more" onClick="load();">LOAD MORE</a></div>

suggest me if anyone has done it.

Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40

1 Answers1

0

You can't, at least not in this way.

Think of when and where your code is getting executed. PHP is executed by the server, before the browser even receives the page. JavaScript is executed by the browser, after the page is done loading.

You can either have a separate script that generates what you need from your number, and pass that as a GET or POST value to the script via an AJAX request, or generate the number you need in PHP.

So in case of an AJAX request you'd have your PHP script doing something like:

<?php
$number = $_GET['number'];
foreach (page('projects')->children()->visible()->limit($number) as $project) {
    echo .....
}
?>

and your JavaScript would call that script via an AJAX request and put the resulting HTML into your page, with something like:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    document.getElementById("container").innerHTML = xht.responseText;
}
xhr.open("GET", "script.php?number=" + p1, true);
xhr.send();
YamiTenshi
  • 166
  • 3
  • i need to do dynamic pagination without reload the whole page in kirby cms. this will not work. i dont need to add extra page for that. by the way thanks for reply –  Aug 23 '16 at 09:22
  • The idea of an AJAX request is that it is done in the background without having to reload the page for the user. An AJAX request is the *only* way to do what you're describing. And yes, you will need a separate script for that - because there needs to be a script that generates the result for you. – YamiTenshi Aug 23 '16 at 09:25
  • how can we implement this concept in kirby cms ? –  Aug 23 '16 at 09:29
  • I have no idea how Kirby CMS works, so I can't help you there, but roughly speaking you'll have to create a URL that generates the listing you want based on a GET value. You can undoubtedly find how to do that in the Kirby CMS documentation. Check https://getkirby.com/docs/cookbook/json for a starting point - it's generally good practice to output JSON for stuff you do via AJAX. – YamiTenshi Aug 25 '16 at 09:26