On my website get articles with PHP query, something like
$sql - ("SELECT * FROM articles WHERE showing=0");
most important thing here is showing=0 I’ll explain why latter on.
I was thinking of adding a check box that will do the following:
If user checks the check box it will change showing=1 and if it is unchecked showing=0.
How would I make it so 1) it remembers if user has or hasn’t clicked a check box. So every time user visits the website the check box is on or off depending on what user did before.
2) refresh page to show new results if for example showing=0 changed to showing=1 ?
If you care at all about performance, it’s vitally important to know how big the articles table could get, and how it’s indexed. If the articles table is small enough that the other steps of generating the webpage overwhelm the cost of two queries (which you may be able to parallelize), simply generate both lists, drop in two divs, and write client-side JavaScript to show the specified div only when the checkbox is checked or unchecked. Something like
should take care of question 2 at least. If the articles table is big and slow to query, you have to go a completely different route, and fill in the missing div only if the user actually clicks on the checkbox. In this case, you need to add AJAX calls to the updateArticlesList function.
On the other hand, question 1 is an entirely different question. I’m going to have to assume that you have some way to know who the current user is whenever you generate a page, otherwise it’s meaningless to try to speak of a “what the user did before.” But, with the code above, the only thing you need to do is to set the initial values of the visibility for divs article0 and article1, and the visibility of checkbox ckart. AJAX comes in on the question of saving the user’s settings back to the server. Your best bet is to add a URL to the server (I have far more Django experience, so my first instinct is a urls.py entry, even though I know that doesn’t apply to PHP) such that a post back of the string “0” or “1” causes the settings to be saved in the user’s profile. On the next page serve, if the user’s article visibility setting is 0, then div article0 is visible, article1 is collapsed, and ckart is unchecked. If the user’s article visibility setting is 1, then article0 is collapsed, article1 is visible, and ckart is checked.