如何使用复选框实时更改MySql查询?

时间:2021-07-27 16:23:34

On my website get articles with PHP query, something like

在我的网站上获取有关PHP查询的文章,例如

$sql - ("SELECT * FROM articles WHERE showing=0");

most important thing here is showing=0 I'll explain why latter on.

这里最重要的是显示= 0我会解释为什么后者。

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.

我正在考虑添加一个复选框,该复选框将执行以下操作:如果用户选中复选框,它将更改显示= 1,如果未选中则显示= 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 ?

我将如何做到1)它会记住用户是否有点击复选框。因此,每次用户访问网站时,复选框都会打开或关闭,具体取决于用户之前的操作。 2)刷新页面以显示新结果,例如显示= 0更改为显示= 1?

2 个解决方案

#1


0  

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

如果您完全关心性能,那么了解文章表格的大小以及索引的编制方式至关重要。如果文章表足够小,生成网页的其他步骤将超过两个查询(您可以并行化)的成本,只需生成两个列表,放入两个div,然后编写客户端JavaScript以显示仅在选中或取消选中复选框时指定div。就像是

<script type="text/javascript">
    function updateArticlesList() {
        var box = document.getElementById("ckart");
        if (box.checked) {
            document.getElementById("article0").style.visibility = "collapse";
            document.getElementById("article1").style.visibility = "visible";
        } else {
            document.getElementById("article0").style.visibility = "visible";
            document.getElementById("article1").style.visibility = "collapse";
        }
    }
</script>
<input id="ckart" type="checkbox" onclick="updateArticlesList()">Switch articles list</input>

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.

应该至少处理问题2。如果文章表很大且查询速度很慢,则必须使用完全不同的路径,并且只有在用户实际单击复选框时才填写缺少的div。在这种情况下,您需要向updateArticlesList函数添加AJAX调用。

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.

另一方面,问题1是一个完全不同的问题。我将不得不假设您有一些方法可以在生成页面时知道当前用户是谁,否则尝试谈论“用户以前做过的事情”是毫无意义的。但是,使用上面的代码,您唯一需要做的就是为divs article0和article1设置可见性的初始值,以及复选框ckart的可见性。 AJAX涉及将用户设置保存回服务器的问题。你最好的办法是给服务器添加一个URL(我有更多的Django经验,所以我的第一直觉是urls.py条目,即使我知道这不适用于PHP)这样一个帖子后面的字符串“0”或“1”会将设置保存在用户的配置文件中。在下一页服务中,如果用户的文章可见性设置为0,则可以看到div article0,折叠了article1,并且未选中ckart。如果用户的文章可见性设置为1,则折叠article0,可见article1,并检查ckart。

#2


1  

That's a lot of ground to cover. For the first part, you'd attach an 'onclick' handler to the checkbox, which triggers an AJAX call to the server and sends over the new checkbox status. The server-side script updates the database with the new checkbox.

这是很多需要解决的问题。对于第一部分,您将向复选框附加一个“onclick”处理程序,该处理程序触发对服务器的AJAX调用并通过新的复选框状态发送。服务器端脚本使用新复选框更新数据库。

As for remembering the database, that depends on how you treat users. If they're registered, store the checkbox status in their user information somewhere. If they're just joe-random unregistered, you'll need to use a cookie.

至于记住数据库,这取决于你如何对待用户。如果已注册,请将复选框状态存储在其用户信息中。如果他们只是joe-random未注册,您将需要使用cookie。

As for your 2), I'm not sure what you mean. You want the checkbox to update in all windows the user may have open? Or just change the checkbox in the window that they clicked on it?

至于你的2),我不确定你的意思。您希望复选框在用户可能已打开的所有窗口中更新吗?或者只是更改他们点击的窗口中的复选框?

#1


0  

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

如果您完全关心性能,那么了解文章表格的大小以及索引的编制方式至关重要。如果文章表足够小,生成网页的其他步骤将超过两个查询(您可以并行化)的成本,只需生成两个列表,放入两个div,然后编写客户端JavaScript以显示仅在选中或取消选中复选框时指定div。就像是

<script type="text/javascript">
    function updateArticlesList() {
        var box = document.getElementById("ckart");
        if (box.checked) {
            document.getElementById("article0").style.visibility = "collapse";
            document.getElementById("article1").style.visibility = "visible";
        } else {
            document.getElementById("article0").style.visibility = "visible";
            document.getElementById("article1").style.visibility = "collapse";
        }
    }
</script>
<input id="ckart" type="checkbox" onclick="updateArticlesList()">Switch articles list</input>

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.

应该至少处理问题2。如果文章表很大且查询速度很慢,则必须使用完全不同的路径,并且只有在用户实际单击复选框时才填写缺少的div。在这种情况下,您需要向updateArticlesList函数添加AJAX调用。

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.

另一方面,问题1是一个完全不同的问题。我将不得不假设您有一些方法可以在生成页面时知道当前用户是谁,否则尝试谈论“用户以前做过的事情”是毫无意义的。但是,使用上面的代码,您唯一需要做的就是为divs article0和article1设置可见性的初始值,以及复选框ckart的可见性。 AJAX涉及将用户设置保存回服务器的问题。你最好的办法是给服务器添加一个URL(我有更多的Django经验,所以我的第一直觉是urls.py条目,即使我知道这不适用于PHP)这样一个帖子后面的字符串“0”或“1”会将设置保存在用户的配置文件中。在下一页服务中,如果用户的文章可见性设置为0,则可以看到div article0,折叠了article1,并且未选中ckart。如果用户的文章可见性设置为1,则折叠article0,可见article1,并检查ckart。

#2


1  

That's a lot of ground to cover. For the first part, you'd attach an 'onclick' handler to the checkbox, which triggers an AJAX call to the server and sends over the new checkbox status. The server-side script updates the database with the new checkbox.

这是很多需要解决的问题。对于第一部分,您将向复选框附加一个“onclick”处理程序,该处理程序触发对服务器的AJAX调用并通过新的复选框状态发送。服务器端脚本使用新复选框更新数据库。

As for remembering the database, that depends on how you treat users. If they're registered, store the checkbox status in their user information somewhere. If they're just joe-random unregistered, you'll need to use a cookie.

至于记住数据库,这取决于你如何对待用户。如果已注册,请将复选框状态存储在其用户信息中。如果他们只是joe-random未注册,您将需要使用cookie。

As for your 2), I'm not sure what you mean. You want the checkbox to update in all windows the user may have open? Or just change the checkbox in the window that they clicked on it?

至于你的2),我不确定你的意思。您希望复选框在用户可能已打开的所有窗口中更新吗?或者只是更改他们点击的窗口中的复选框?