Create "simple" pagination for PHP, foreach loop - javascript

I'm pretty new with the "dynamics" programming, but I really love it and now I need to improve my PHP web page.
Actually I have an PHP array, and I'm building "standard" html output inside a foreach cycle to build my page.
As you can see from the screenshot the actual result it's a page with a lot of <div class="ui-content">, there is a way, to simple create pagination where I'm able to show only six DIV entry per page, and create the menu to switch between page?
I think, the best solution is build something using advanced "dynamics" technics like AJAX call, but I'm totally new with this kind of programming.
Another possibilities is to use some pure java script at runtime to make the pagination, but I think this is not the best solution concerning the performance.

You Can try with simple Jquery Pagination, which is simple one to implement, but it loads the complete data and then paginate, in other approch like AJAX which request server each time you change the page.
Simple pagination jquery example easily available online. for example :
http://flaviusmatis.github.io/simplePagination.js/#page-2

Related

Best way to inject HTML with the results of a WP REST API call?

Let's say you want to dynamically inject 10 extra posts to a WordPress site's homepage blog roll. The 10 new posts are added after some user interaction. So for this example let's pretend the JSON response of the user interaction is identical to the results of this call:
GET /wp-json/wp/v2/posts?s=awesome
What is the ideal way to add the results into the homepage, but ensuring the new posts use the same HTML as the existing ones?
In my mind it seems like the options are currently:
1- Write a loop in Javascript and write the correct html for the posts inside the loop. But that would complicate things like translations and I already have loop templates in PHP, so feels like duplicating code.
2- Writing a custom endpoint. But from what I understand I'd need to write a new WP_Query() with the search parameters, and then return all the html in a single variable (so no get_template_part() and duplicating code again).
3- A hacky idea I had was to add a hidden empty skeleton of the html of a post somewhere on the site on page load. Then when the time comes, in Javascript run a loop and clone the skeleton each time to inject the relevant post data from the JSON. But this feels nasty to me.
Is there a better way? Or am I misunderstanding a basic concept of the WP REST API?
Your idea #3 sounds a lot like using a templating language like Handlebars, and isn't all that hacky necessarily.
You'd "hide" your HTML template in a script tag and then use Javascript to render it with the data that comes from your Ajax call. See the examples here for a basic idea: http://handlebarsjs.com/

How to let 2 html pages communicate

i'm new to web tecnologies, and i'm trying to make some stuff work. I have 2 similar html pages, with the same table on them.
I have to make sure that every data inserted in the first table (editable) is replicated in the same cell of the second table (of the other HTML page).
HTML code I used is really simple, I've added some style with CSS and 3 functions with JS (add, delete and modify row). I have to replicate these changes to rows too.
I'm a bit confused, I think I have to manage this thing with a server application, maybe in php, but i don't know how to start.
I've searched this site, but I didnt find anything useful, for now.
How can I approach this problem? I want to learn these things, I am not asking you for having working code and easily solve the problem...
Thanks to everybody in advance.
You need to store the data somewhere (database, local storage, cookie, file, etc.), and read it when page loads.
The most common way is to store the data in database.
When your page loads, get the data from the database and build the table.
This is a good place you can start learning how to work with MySql database: http://www.w3schools.com/php/php_mysql_intro.asp
Angular JS library will be a simple solution to your problem.

fast way to load ajax content

Lets say i have json which contains informations. What would be the best way to put the informations on site. Creating elements via js like
(this is just part im interested in , inside ajax call)
var div=document.creteElement("div");
var text=document.createTextNode("bla");
var img=new Img;
img.src=json1.img;
img.onload=function(){
div.appendChild(text);
div.appendChild(img);
}
or putting them "in" like
body.innerHTML+="<div><p>blabla</p><img src='json1.img'/></div>"`
which one of there is better/faster/more efficient , or is there any better way how to put content into site via ajax? Also how does big sites that load tons of data from server handle this? Tons of data would take a lot of time to load
Ok, I understand now. To render content from ajax you have 3 choices:
Load the data with an ajax request ($.ajax in jquery or xmlhttprequest from scratch) and then create elements and insert into DOM. This solution is very bad because it is unmaintainable.
Load data using ajax and use javascript templates like Underscore template (_.template) or Handlebars (http://handlebarsjs.com/). This is a very useful solution.
Load data using ajax and use a very powerfull system views like React JS. I love it and it's very intuitive and easy to use to have a good performance without a lot of work from your part. "Template system" is very cool and easy (like using html). I will use react if I were you :) https://facebook.github.io/react/

Mysql take a long time to load

I have a website that mirror the hacked websites
From the day of starting everything is cool till it's being bigger
I have 2,197 records in MySQL DB
Every time I want to load this page http://red-zone.org/archive/ 2,197 have to be shown.
The cause that i'm using JavaScript for pagination and the JavaScript will work just after loading all the records
I don't like to use PHP for pagination
Does there's any other solution ?
I think the best way to use paging with big data is using Server-side processing with ajax/java script.
If you want a cool example you can look into datatables:
https://datatables.net/examples/data_sources/server_side.html
Of you can make your own javascript, but i see your already using datatables try the server side processing it will resolve your issue.

How is it possible to call data from an xml document into an html page?

I'm new to web development but from what I've learned so far, I'm sure what I want to do is super easy. I just haven't quite figured it out, yet.
I have an app that pulls external html pages. These pages all pertain to grain prices. I have about ten pages or so and currently to change the prices I open each page, find the prices and manually change them. What I want to do is just have all the pages pull their prices from the same document (I'm assuming this will have to be an xml or txt document) so that I just have to update that one external document.
The external document will be hosted on the same server as the html pages.
This seems like something I should be able to do simply with javascript and Ajax and I have seen many examples using just that. The thing is that in all of the examples I've seen, the ajax calls an entire text document instead of just one piece of it.
For example, I have an html page called Northern Alberta Prices and it lists about six different grain prices. I want that page to call the external document (let's call it prices.xml) and get all the northern Alberta prices off of it and put them in their proper places on the html page and then I want the Central Alberta Prices page to also call prices.xml and take the relevant prices off it etc.
What would be the best way to do this?
(I'm not able to use PHP on my server at the moment but any answers involving PHP would still be welcome.)
Thank you for your time.
You'd have to call the entire document and then interact with the specific node in the XML
without the actual schema there's not much more help I can give you, although there is an ok tutorial here:
http://www.sitepoint.com/server-side-xml-javascript/
which will help you understand the core concepts behind data manipulation
Yes, to use XML with PHP you can do this:
http://php.net/manual/en/simplexml.examples-basic.php
That pretty much says, you can do this:
<?php
include 'example.php';
$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie[0]->plot;
?>
... Which will get the Plot attribute from the first movie node. Please let me know if you have any additional questions.
Edit: One more option I came across was using jQuery (javascript):
http://think2loud.com/224-reading-xml-with-jquery/

Categories