tags:

views:

186

answers:

2

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"?

THANKS ***-----------------------------------------------------

OK - Thanks for the help so far! I have this (I'm a beginner..):

<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

A: 

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'])).

Franz
Thanks, trying...
Mike
I also hope your script is called `page.php` in that example code in your edited answer... ;) We cannot help much when all you give us is "it does not work". Error messages?
Franz
Thanks Franz. I'm using different tables so the queries will run faster, and the page will load faster. I'm running the counts within the db and then inserting the results into these different tables.
Mike
Hmm - could this have anything to do with the fact that I'm trying to call this in my sidebar.php? I'm trying to get these results to load in the sidebar... http://www.celebrything.com/
Mike
Well yes, just change `page.php` or whatever to `sitebar.php` or what it's called...
Franz
Oh, i got it now. You are calling the function from the other answer without defining it. Either you enclose the code above the function call within the function definition or you just delete the function call and replace `$table` with `$_GET['table']`.
Franz
Looking at it again, you seem to want it to be displayed on multiple pages. That makes this thing a little more complicated. Where do you want the list to be displayed anyways?
Franz
+1  A: 

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

Darkerstar
Trying to get this to work. How would I show the result from one of these tables by default, on the pageload?
Mike
Add a logical to check if $_GET['table'] is empty.
Darkerstar
Having some issues... Can't get this to work. I'm going to edit above with what I have, that isn't working. REALLY appreciate the help!
Mike
You are not copying the whole code. If you are using it as a function, make sure you have that function. Here is a link to function explained in PHP manual, http://www.php.net/manual/en/language.functions.php
Darkerstar
AAAhhh!! You're right! I forgot that part - defining the function. Now it works! THANKS. But now.. when I click the main body displays an page not found error.
Mike
http://www.celebrything.com/post.php?table=week
Mike
You have three options:1. Show the list(s) on a separate page and use that instead of `post.php`.2. Use the name of the current script instead of `post.php`. That's necessary because `sidebar.php` is obviously included from many different files.3. Use AJAX to load the list(s) asynchronously inside the sidebar. That removes the hassle of playing with the URL parameters, but it only works with JavaScript enabled...
Franz