在一页上显示不同查询的结果,或者,不同时显示所有结果
不确定这是否是表达我想做的事情的最佳方式。事情是这样的:
我希望用户能够基于三个不同的表(一次一个)显示我的数据库的结果。
我已准备好查询,但只想根据用户单击的内容一次在页面上显示一个结果。查询基于 3 个不同的表。有一个“今天”表、一个“本周”表和一个“本月”表。
因此,当用户单击“今天”时,我想显示“今日”表中的结果。当他们单击“本周”时,我希望结果切换为来自“本周”表。我假设这可以通过 PHP 中的简单 if-then 逻辑来完成。
以下是我从“今日”表调用的方式:
<?php
global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_today');
foreach($result as $row) {
echo '<a href="http://www.celebrything.com/?s=' .
urlencode($row->name) . '&search=Search">' . $row->name .
'</a> - ' . $row->count . ' Posts<br/>';}
?>
我将从“本周”表调用,如下所示:
<?php
global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_thisweek');
foreach($result as $row) {
echo '<a href="http://www.celebrything.com/?s=' .
urlencode($row->name) . '&search=Search">' . $row->name .
'</a> - ' . $row->count . ' Posts<br/>';}
?>
该查询当前正在从页面加载运行。如何插入逻辑以使其通过单击“今天”、“本周”或“本月”运行?
好的 - 感谢迄今为止的帮助!我有这个:
<div id="sidebar">
<div class="post">
<h2>
<font color="#333333">Most Popular Celebrities</font><br>
<font color="#333333">in last 24 hours</font>
<br>
<br>
<a href="page.php?table=today">Today</a>
<a href="page.php?table=week">Week</a>
<a href="page.php?table=month">Month</a>
<?php
if (!in_array($table, array('today', 'week', 'month')) {
return false;
}
global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_' . $table);
foreach($result as $row) {
echo '<a href="http://www.celebrything.com/?s=' .
urlencode($row->name) . '&search=Search">' . $row->name .
'</a> - ' . $row->count . ' Posts<br/>';
}
}
showTable($_GET['table']);
?>
</h2>
</div>
</div>
<div class="clear"></div>
我收到此错误:
解析错误:语法错误,第 16 行 /home/content/c/e/l/celebrything/html/wp-content/themes/celebrything/sidebar.php 中出现意外的“{”
Not sure if that was the best way to phrase what I'm trying to do. Here goes:
I want users to be able to show results form my DB based on three different tables, one at a time.
I have the queries ready, but only want to show one result on the page at a time, based on what the user clicks. The queries are based on 3 different tables. There is a Today table, a This Week table, and a This Month table.
So when a users clicks Today, I want to show the results from the Today table. When they click This Week, I want the results to switch to come from the This Week table. I'm assuming this can be done with a simple if-then logic in PHP.
Here's how I'm calling from the Today table:
<?php
global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_today');
foreach($result as $row) {
echo '<a href="http://www.celebrything.com/?s=' .
urlencode($row->name) . '&search=Search">' . $row->name .
'</a> - ' . $row->count . ' Posts<br/>';}
?>
I would call from the This Week table like so:
<?php
global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_thisweek');
foreach($result as $row) {
echo '<a href="http://www.celebrything.com/?s=' .
urlencode($row->name) . '&search=Search">' . $row->name .
'</a> - ' . $row->count . ' Posts<br/>';}
?>
This query is currently running from the page load. How do I insert logic to make it run from a click on "Today", "This Week", or "This Month"?
OK - Thanks for the help so far! I have this:
<div id="sidebar">
<div class="post">
<h2>
<font color="#333333">Most Popular Celebrities</font><br>
<font color="#333333">in last 24 hours</font>
<br>
<br>
<a href="page.php?table=today">Today</a>
<a href="page.php?table=week">Week</a>
<a href="page.php?table=month">Month</a>
<?php
if (!in_array($table, array('today', 'week', 'month')) {
return false;
}
global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_' . $table);
foreach($result as $row) {
echo '<a href="http://www.celebrything.com/?s=' .
urlencode($row->name) . '&search=Search">' . $row->name .
'</a> - ' . $row->count . ' Posts<br/>';
}
}
showTable($_GET['table']);
?>
</h2>
</div>
</div>
<div class="clear"></div>
I'm getting this error:
Parse error: syntax error, unexpected '{' in /home/content/c/e/l/celebrything/html/wp-content/themes/celebrything/sidebar.php on line 16
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将代码放入函数中,并通过请求参数选择表。就像下面这样。注意我添加了
in_array
检查以确保该表不是任何表。PHP
调用
page.php
中的函数:You can make the code into a function, and choose the table via a request param. Like so below. Note I added
in_array
check to make sure the table is not any table.PHP
Call the function in your
page.php
:您可以根据 URL 参数来制作它。
该链接将如下所示:
site.php?type=today
。然后,在您的 PHP 脚本中,您可以执行以下操作:
编辑:我使示例更加具体并使用
switch
构造,因为它允许混淆默认情况下更容易。现在假设当给出无效类型时,将显示本月的列表。我想这就是你想要的......
不过,让我问你:为什么你有这三张桌子?请告诉我您没有使用某种类似 cron 的脚本将项目从今天表移动到本周列等等...如果是这样,您应该只在一个表中执行此操作,并使用一个日期列。
编辑 2:不过,要修复示例代码,您需要将变量名称从
$table
更改为$_GET['table']
。您还应该检查该变量是否存在(通过使用isset($_GET['table'])
检查)。You can make it based on URL parameters.
The link will look something like this:
site.php?type=today
.Then, in your PHP script, you could for example do this:
EDIT: I made the example be more concrete and used a
switch
construct, because it allows to mess with the default case more easily.This is now assuming that when an invalid type is given, the list for this month is displayed. I think that's what you want...
Let me ask you, though: Why exactly do you even have these three tables? Please tell me you're not using some sort of cron-like script to move items from the today table to the this week column and so on... If so, you should just do it in one table, with one date column.
EDIT 2: To fix your example code, though, you need to change the variable name from
$table
to$_GET['table']
. You should also check whether the variable exists at all (by checking withisset($_GET['table'])
).