Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Plugin challange

    First time post, and of course it is a help question.

    I am using a WP plug-in to display previous and next games in a season. (Leaguemanager is the plug-in name - Link http://wordpress.org/extend/plugins/leaguemanager/ ). The challenge I am having is that the previous game only displays the first game of the season, not the actual previous game...

    The following code is pulled from the shortcode.php file in the lib folder of the plugin.

    Here is the code for the next game which works...


    Code:
    $next_matches = $leaguemanager->getMatches("( `home_team` = {$team->id} OR
    `away_team` = {$team->id} ) AND DATEDIFF(NOW(), `date`) <= 0");
    $next_match = $next_matches[0];
    Here is the code for the previous game that pulls the first game of the
    season, not the actual previous game.

    Code:
    $prev_matches = $leaguemanager->getMatches("( `home_team` = {$team->id} OR
    `away_team` = {$team->id} ) AND DATEDIFF(NOW(), `date`) > 0");
    $prev_match = $prev_matches[0];
    It's probably something really simple, but I have been hacking away at this for about 8 hours and I cannot get it to work. On a side note if you swap the "0" for a "1" in this line $prev_match = $prev_matches[0]; it will show the second game of the season.

    Thanks for any help!

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Perhaps you need to tweak your query. Maybe adding something like ORDER BY `date` DESC would get them in the correct reversed (descending) order so that array[0] is the immediate previous by date.

    I suspect they're pulling now in either ascending order or even unordered and you're seeing the first (oldest) result. That's further suggested by changing the array index to 1 and getting the second game.

    EDIT: By the way, if that's the solution, you may also want to modify the query for next game. If the dates aren't inserted in an order that matches their natural order, you could get the same problem for that query. Just reverse the DESC to ASC in the previous snippet.
    Last edited by tomws; 10-24-2009 at 05:50 PM.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    gpimpin (10-24-2009)

  • #3
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Makes a lot of sense, I'll report back if it worked. Thanks!

  • #4
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay I tried that (adding ORDER BY 'date' DESC) and the following error is thrown:

    [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY `date` ASC' at line 1]
    SELECT `group`, `home_team`, `away_team`, DATE_FORMAT(`date`, '%Y-%m-%d %H:%i') AS date, DATE_FORMAT(`date`, '%e') AS day, DATE_FORMAT(`date`, '%c') AS month, DATE_FORMAT(`date`, '%Y') AS year, DATE_FORMAT(`date`, '%H') AS `hour`, DATE_FORMAT(`date`, '%i') AS `minutes`, `match_day`, `location`, `league_id`, `home_points`, `away_points`, `winner_id`, `loser_id`, `post_id`, `season`, `id`, `custom` FROM wp_leaguemanager_matches WHERE ( `home_team` = 1 OR `away_team` = 1 ) AND DATEDIFF(NOW(), `date`) > 0 ORDER BY `date` DESC ORDER BY `date` ASC

  • #5
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    okay I got some more advice and here is what looks to work...

    $prev_match = end($prev_matches);

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    That will work as long as the query is pulling results ordered by date, which it may be doing. Looking back at the error, there are two ORDER BY statements, so the original ORDER BY must have been in place already. Sorry for the confusion.
    Are you a Help Vampire?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •