PHP:跟踪搜索查询中的数组元素
我构建了一个简单的数据库,其中页面显示随机选择的报价,这些报价显示在无序列表中。 js 脚本循环遍历每个列表项,一次显示一个。
这是查询循环:
while ($row = mysql_fetch_assoc($query)) {
echo "<li><span id='id'>";
// print the quote number
printf ("%s", $row['id']);
echo "</span> <span id='quote'>";
// print the quote
printf ("%s", $row['strategy']);
echo "</span> <span id='author'>";
// print the quote author
printf ("by %s", $row['author']);
echo "</span></li>";
}
到目前为止一切顺利。我的问题是如何显示喜欢和不喜欢的数量,并让用户对其进行“投票”。我不知道如何在像这样打印出来后保持数组数据处于活动状态。我可以使用计数器来跟踪 PHP 中的行元素,以便我可以知道用户正在投票的引用吗?
谢谢!
I built a simple database where the page displays a random selection of quotes which are displayed in an unordered list. A js script cycles through each list item showing them one at a time.
This is the query loop:
while ($row = mysql_fetch_assoc($query)) {
echo "<li><span id='id'>";
// print the quote number
printf ("%s", $row['id']);
echo "</span> <span id='quote'>";
// print the quote
printf ("%s", $row['strategy']);
echo "</span> <span id='author'>";
// print the quote author
printf ("by %s", $row['author']);
echo "</span></li>";
}
So far so good. My problem is how to show the number of likes and dislikes, and have users 'vote' on them. I cannot figure out how I can keep array data active once it is printed out like this. Could I use a counter to keep track of row elements in PHP so I can then know which quote the user is voting on?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的代码很难读,所以我重写了它。您想要使用报价 ID 号来访问有关报价的其他数据。 Dhruv 的示例是使用元素 id 并通过 javascript 获取数据的好方法。但对于某些事情,您可能希望在服务器上处理更多内容。
您可以看到,like 链接只是将报价的 id 作为 $_GET 变量传递的链接。对于点赞数部分,假设我们已经拥有该信息,并且它已经位于一个数组中,索引键为引用 ID。 (这就是 $like_array)
这只是基本想法,但应该让您朝着正确的方向前进。
Your code was hard to read so I rewrote it. You want to use the quote id number to access other data about the quotes. Dhruv's example was a good way to use an element's id and get the data with javascript. But for some things you might want to handle more of it on the server.
You can see that the like link is just a link that passes the quote's id as a $_GET variable. For the number of likes part assume that we already have that information and it's already in an array with the index keys being the quote ids. (That's the $like_array)
This is just the basic idea, but should get you going in the right direction.
您可以将数据库主键或索引号回显为行中的 id。
然后 JavaScript 就可以轻松检测到哪个元素被投票了。
示例:
Javascript:
You can echo database primary key or an index number as id in the row.
Then javascript can easily detect which element was voted upon.
Example :
Javascript :