在一页上显示不同查询的结果,或者,不同时显示所有结果

发布于 2024-08-14 21:02:52 字数 2184 浏览 3 评论 0原文

不确定这是否是表达我想做的事情的最佳方式。事情是这样的:

我希望用户能够基于三个不同的表(一次一个)显示我的数据库的结果。

我已准备好查询,但只想根据用户单击的内容一次在页面上显示一个结果。查询基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゝ偶尔ゞ 2024-08-21 21:02:52

您可以将代码放入函数中,并通过请求参数选择表。就像下面这样。注意我添加了 in_array 检查以确保该表不是任何表。

<a href="page.php?table=today">Today</a>
<a href="page.php?table=week">Week</a>

PHP

function showTable ($table) {

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/>';
}
}

调用 page.php 中的函数:

if (empty($_GET['table'])) {
    showTable($_GET['table']);
} else {
    showTable('today');
}

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.

<a href="page.php?table=today">Today</a>
<a href="page.php?table=week">Week</a>

PHP

function showTable ($table) {

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/>';
}
}

Call the function in your page.php:

if (empty($_GET['table'])) {
    showTable($_GET['table']);
} else {
    showTable('today');
}
挽手叙旧 2024-08-21 21:02:52

您可以根据 URL 参数来制作它。

该链接将如下所示:site.php?type=today

然后,在您的 PHP 脚本中,您可以执行以下操作:

if (isset($_GET['type']))
{
    switch ($_GET['type'])
    {
        case 'today':
            // Query and list for today
            break;
        case 'thisweek':
            // Query and list for this week
            break;
        case 'thismonth':
        default:
            // Query and list for this month
            break;
    }
}
else
    // Error handling here

编辑:我使示例更加具体并使用 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:

if (isset($_GET['type']))
{
    switch ($_GET['type'])
    {
        case 'today':
            // Query and list for today
            break;
        case 'thisweek':
            // Query and list for this week
            break;
        case 'thismonth':
        default:
            // Query and list for this month
            break;
    }
}
else
    // Error handling here

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 with isset($_GET['table'])).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文