Good afternoon, implemented a lazy load tape, which as the scrolling page loads the news. Used samopisny engine on Codeigniter 3.
Everything works, the problem is that
$("#posts").append('');
You need to insert a large layout design in conjunction with the php functions of Codeigniter itself.
For example, the data array contains the date of the news in the unix_timestamp format .. I try to display it using the native Codeignter function:

 $.each(data, function(index, data){ $("#posts").append('<?php unix_to_human( + data.created + )?>'); }); 

This output does not work. How can append such a complicated output using php tags in append?

PS I give the full code below:

 <script type="text/javascript"> $(document).ready(function(){ var inProcess = false; var num = 15; $(window).scroll(function() { if($(window).scrollTop() + $(window).height() >= $(document).height() && !inProcess) { $.ajax({ url: 'http://домен/lazy/index', method: 'GET', data: {"num" : num}, beforeSend: function() { inProcess = true; } }).done(function(data){ data = JSON.stringify(data); data = jQuery.parseJSON(data); if (data.length > 0) { $.each(data, function(index, data){ $("#posts").append(''); // вывод контента тут }); inProcess = false; num += 15; } }); } }); }); 

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Unfortunately, you cannot call the PHP functions inside Javascript after your file has completed its work (i.e., the page has been loaded by the browser).

In your case, I see 2 options for the implementation of the plan:

  1. Return data that has been converted as necessary from the server (the recommended method, the server is needed for this);
  2. Duplicate the php functions you use in javascript and call them.

    In your example, the php code is executed, but not displayed in the browser. Try adding an output function.

     $.each(data, function(index, data){ $("#posts").append('<?php echo unix_to_human( + data.created + )?>'); }); 

    If this is what you need. But it is better to implement a separate php script to generate the necessary layout dynamically and already access it.