Dynamic content in PHP WordPress page - php

I have a ecommerce store website running with WordPress. I'd like to include a section with a -random custormer's product review, so that every time someone access the page, there will be a different comment there.
I'm not used to PHP, but I managed to create a shortcode which takes a random comment and returns the proper HTML. It is working fine (in eddition mode, every time I insert the shortcode a different comment appears).
My issue is that when I leave the page and return, the previous one is still there. I believe that it is being caused by cache, but I wouldn't like to disable the cache for the whole page.
How you I force the shortcode run again (I don't know if it is the right way to explain) and make sure that at every access, a different comment appears?
One solution I thought is to have JS code which would do preaty much the same thing my PHP code does, using Woocommerce API to get the data. But I'm wondering if there is a simpler solution to do that, like forcing the specific section not being cached or re-run the shortcode.
Thanks!

JS can't do what PHP does here: at most it can create an AJAX-call to the backend that then runs a query for a random comment and returns it. You need to render it thereafter. It's fairly standard, but overkill for your case.
Instead, you're going to want to check whether your caching mechanism supports ESI or something else that excluded parts of your code from being cached.

Related

how is already loaded php script processed by server if there is another request from the same page

I real beginner and try to understand how things work more then to develop stuff, and now i can't move forward till someone gives me an accurate answer about a little detail of following issue.
Let's assume there's a page with php code http://example.com/blablabla and link on it like http://example.com/blablabla?file=number_1 which's used to modify some parts of this page
What i really don't know is what happens with the already loaded script from http://example.com/blablabla when there's a request from this page -http://example.com/blablabla?file=number_1
The questions actually are:
Is code from the already loaded page processed every time when requesting ?file=number_1?
For me it seems very strange, 'cause if with the first http://example.com/blablabla via php i selected for example a huge size of data from database and only want to modify small part of page with ?file=number_1 and why do i need server to process request to the database one more time.
My experience says me that server do process again already loaded code,
BUT according to this i have a very SLIGHT ASSUMPTION, that i'm not really sure about this, but it seems very logical:
The real trick is that the code in the first page has one VARIABLE and its value is changed
by the second request, so i assume that server see this change and modifies only that part of the code with this VARIABLE - for example the code in http://example.com/blablabla looks like this
<?
/* some code above */
if (empty($_GET['file'])) {
/* do smth */
} else {
/* do smth else */
}
/* some code below */
?>
with the request http://example.com/blablabla?file=number_1 the server processes only part of the original code only including changed $_GET['file'] variable.
Is it totally my imagination or it somehow make a point?
Would someone please explain it to me. Much appreciated.
HTML is a static language. There is php and other similar languages that allows you to have dynamic pages but because it still has to send everything over as html you still have to get a new page.
The ?file=number_1 just gives a get request to the page giving it more information but the page itself had to still be rerun in order to change the information and send the new static html page back.
The database query can be cached with more advanced programming in PHP or other similar languages so that the server doesnt have to requery the database but the page itself still had to be completely rerun
There are more advanced methods that allows client side manipulation of the data but from your example I believe the page is being rerun with a get request on the server side and a new page is being sent back.
i believe this is what your asking about.
Yeah, thanks you guys both. It certainly clarified the issue that every script (clean html or generated by php) runs every time with each request, and only external types of data like image files and, even as it follows from the previous answer, mysql results can be cached and be used via php to output necessary data.
The main point was that I mistakenly hoped that if the page is loaded and consequently cached in computer memory, the appended QUERY STRING to this URL will send, of course, new get request, but retrieved respond will affect this page partly without rerunning it completely.
Now i have to reconsider my building strategy – load as much data as it’s required from each requested URL.
If you are looking for a way to edit the page dynamically, use JavaScript.
If you need to run code server side, invisibly to the client, use PHP.
If you need to load content dynamically, use AJAX, an extension of JavaScript.
I hope that helps.

JQuery Pagination With Dynamic Loading

I'm currently creating a website for a friend of mine, and he wants a "Blog" type of system on the front page, which is fine by me, i'll be able to do that.
I'm currently using the Pagination Class slightly modified to remove the "All" option (And needless output related to it).
However, I would like to be able to Dynamically load each page instead of redirecting to each page (If a re-write of the Pagination class is required i won't mind doing it, as long as I can get a general concept of how to do it...)
Solutions like JPaginate Won't work for me, because in the end, the page could be loading huge amounts of text (The message body is stored in a LargeText table) and i don't want to have all the articles in the source code (Previous experience such as that has actually taught me loading that much text into a web browser is not the smartest thing)
A few things to note, the connection is MySQLi (The Pagination class only appends the limit code for Sql), I don't know much Javascript, and i still consider myself a noob at php.
You can use something like this:
http://www.9lessons.info/2009/09/pagination-with-jquery-mysql-and-php.html
http://www.9lessons.info/2010/10/pagination-with-jquery-php-ajax-and.html

JQuery AJAX Autocomplete issue with PHP and MySQL

I'm having a problem with my autocomplete. It works on another one of my pages, but on this one, it doesn't work. It's returning the correct number of entries, but they are all "blank" (or at least black so I can't see it), and selecting one does not put it into the text field either.
I'm using this: http://papermashup.com/jquery-php-ajax-autosuggest/
My page right now looks like
Any suggestions?
Thanks!
I'd post my code, but it's pretty much exactly what's on the site linked above, with some variables changed, and embedded into a PHP. Let me know if you want to see it (I don't want to paste it here and make the page huge and fugly).
Oh and this is taking it from a column in a MySQL database.
I think this link is quite useful to understand the technique. After you got the AJAX technique, you may simply change your php files which can run sql queries etc. You may show the results in that way with a simple div, very trivial css implementation would be enough. I think the hardest part is solved here:
http://www.w3schools.com/ajax/ajax_aspphp.asp
maybe your problem lies within encoding, jquery needs utf8 in the default settings, but without any code, I can only speculate...
try utf8_encode($output) instead of only returning the output...
also maybe your ajax request awaits a specific datastructure (json/xml/ etc)

How to change language without reloading page with PHP / Jquery

I am working on a multilangual website right now.
I am currently including the related language file ('lange/_en.php') for language phrases.
To change languages users will select their language from a < select > item. The thing i want to do is changing related phrases (and urls too if possible) in the page without refreshing or submitting the page.
I remember i saw something like this in web but i have no idea where.
Any help or any ideas about how this thing can be done?
The issue with this is that a language change doesn't only affect a small section of the page, it affects the whole page. So really, you are left with three choices.
The simple way which is indeed reloading the whole page. It's easy to implement, easy to maintain, and doesn't require you to make sure that JavaScript currently running on your page is aware of the new language at runtime.
The complicated way which is getting all the new markup via AJAX and replacing the content of the <body> tag with the reloaded content. This will cause issues with other scripts running (such as image carousels, etc.) that holds a reference to an element so you have to reinitialize every single script that is running on settimeout() on your page.
The close to impossible way which is to have a client side dictionary, selecting each relevant tag, and changing its contents with the new language. This is a pain to setup and a pain to maintain. You literally need a section tailored to each specific page. Again, if you have scripts with strings, you'll have to make sure that the strings they use are updated to the new language.
You are better off simply reloading the page. It will work without JavaScript and it's a one time deal that won't bother users.
check this plugin out:
http://keith-wood.name/localisation.html
It changes language 'on the fly' without going back to server side.
Image reload prototype if-modified i posted about image content update to selected language without reloading the page.
The image takes the value thats sent and updates to language/country code (which is bound to language) on DB and updates image content (characters) to match their keyboard layout..
Hope its some use

Building PHP WYSIWYG Editor

I am building a web application in which the user may add a page, edit the layout, drag drop element, resize element, format the text, edit the element attribute etc.
In the page the user may include (retrieve) dynamic data, like maybe data from database, data generated by php code, etc.
I have played around with cakephp and jquery lately and tried to build this app. But I stumbled upon on how to appropriately display the php code. I tried to look into the cakephp core code and find about output buffering and tried to utilize output buffering to parse the php code and use regex to display it but it is more likely to reinvent the wheel if I write the parser my self
What I am asking is:
Ok, to be more simple and specific I just want to ask, how to save and load the page that was created by the user especially if the page contains php code. I just want to know is there any other method than write my own parser or maybe a library to parse a php code?
Ok that's all for now, does anyone have any idea how to implement it? Or maybe any page / website that could be useful to take some reference from? Maybe a sample code from which I can take some reference
Thanks
I'm not sure you'll find any good answer here about that.
Whoa I don't know where to start. I'll start by the number 3. You want widgets. Then that means you have to create widget class or objects that possesses a template or something that makes them drawable "well, kinda". If I were you it would be loaded from javascript and not really from php. Each widget would be in some way individual applications loaded in a div using javascript.
Point 2, You wanted widgets. When you add widgets to your page, you have to save some informations, like Position, Title, dimensions and so on. You may even save creation parameters. For exemple a ListWidget may be started with different ItemProvider. That way you don't have to write 1000 widgets but only one that shows different content. That said you have widgets, dimension and position. Now that lead us to point 1.
Point 1. Once you have your widgets, position and dimensions, you send the data you used to create them associated with the page to the server. That lead us two point 2 again.
Once you have saved a page. You can see it by retrieving all widgets with parameters and so on. That leaves you 2 options.
Generate Javascript that will recreate the saved widgets.
Generate Html will all the widgets.
Option 1 is simpler since option 2 won't bind html to javascript by itself. Solution 2 on the other hand is better since there is only 1 request to the server.
Oh and a last thing, You should set yourself some limits. That kind of thing can get very complicated and unfortunately not that great. See drupal for example. It does lots of cool stuff but as soon as you install lots of module. Drupal transform itself in some sort of memory eating monster. And almost all the time you don't really need that much of dynamic content. Fixed layouts will do work nice almost 99% of the time.
I'm also forced to say that but if you try to create an application that give users as much power as a scientist that could raise a 7 legged cat. I think you're going to play with really obscure forces!

Categories