Jump to content

mysql paging and sort variables


Dville

Recommended Posts

so i used to have new.php most.php least.php and old.php

each page held the header, footer, body, etc. the only thing that was different was the mysql query.

so now i wanted to clean my code, and reduce some of the script size so instead of having all 4 of those pages, i was just going to use if statements to find out what is held in the $sort variable

so in index.php i have

[code=php:0]if ($sort == 'new' || $sort == '') {

$result = mysql_query("SELECT a.*, COUNT(v.id) AS voteCount, COUNT(c.articleid) AS commentsCount
FROM articles AS a
LEFT JOIN votes AS v ON ( v.articleid = a.id )
LEFT JOIN comments AS c ON ( c.articleid = a.id )
GROUP BY a.id
ORDER BY id DESC LIMIT ".$start.", ".$limit);
}

if ($sort == 'old') {

$result = mysql_query("SELECT a.*, COUNT(v.id) AS voteCount, COUNT(c.articleid) AS commentsCount
FROM articles AS a
LEFT JOIN votes AS v ON ( v.articleid = a.id )
LEFT JOIN comments AS c ON ( c.articleid = a.id )
GROUP BY a.id
ORDER BY id LIMIT ".$start.", ".$limit);

}

if ($sort == 'most') {

$result = mysql_query("SELECT a.*, COUNT(v.id) AS voteCount, COUNT(c.articleid) AS commentsCount
FROM articles AS a
LEFT JOIN votes AS v ON ( v.articleid = a.id )
LEFT JOIN comments AS c ON ( c.articleid = a.id )
GROUP BY a.id
ORDER BY voteCount DESC LIMIT ".$start.", ".$limit);

}

if ($sort == 'least') {

$result = mysql_query("SELECT a.*, COUNT(v.id) AS voteCount, COUNT(c.articleid) AS commentsCount
FROM articles AS a
LEFT JOIN votes AS v ON ( v.articleid = a.id )
LEFT JOIN comments AS c ON ( c.articleid = a.id )
GROUP BY a.id
ORDER BY voteCount LIMIT ".$start.", ".$limit);

}[/code]


now, this add's a '?sort=X' where x is either least, most, new, or old. or even empty if the user is just looking at index.php
but when i try to change pages(i limit it to 15 articles per page) it removes the ?sort= and just adds ?page=


so i go into the code where it does the whole paging work, and found this code


[code=php:0]  function nextPrev($curpage, $pages)
    {
    $next_prev  = "";

    if (($curpage-1) <= 0)
      {
      $next_prev .= "Previous";
      }
    else
      {
      $next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."\">Previous</a>";
      }

    $next_prev .= " | ";

    if (($curpage+1) > $pages)
      {
      $next_prev .= "Next";
      }
    else
      {
      $next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage+1)."\">Next</a>";
      }

    return $next_prev;
    }
  }[/code]


so I change this to the following, and it leaves my $sort variable empty, and keeps the link as
http://localhost/digg/index.php?sort=new&page=2

[code=php:0]  function nextPrev($curpage, $pages)
    {
    $next_prev  = "";

    if (($curpage-1) <= 0)
      {
      $next_prev .= "Previous";
      }
    else
      {
      $next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."\">Previous</a>";
      }

    $next_prev .= " | ";

    if (($curpage+1) > $pages)
      {
      $next_prev .= "Next";
      }
    else
      {
      if ($sort == 'most')
      {
      $next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?sort=most&page=".($curpage+1)."\">Next</a>";     
      }

      if ($sort == 'new' || $sort == '')
      {
      $next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?sort=new&page=".($curpage+1)."\">Next</a>";     
      }     

      if ($sort == 'least')
      {
      $next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?sort=least&page=".($curpage+1)."\">Next</a>";     
      }
     
      if ($sort == 'old')
      {
      $next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?sort=old&page=".($curpage+1)."\">Next</a>";     
      }     
     
      }

    return $next_prev;
    }
  }[/code]


any thought's on why this isnt capturing the $sort variable? the code that i added these if statements to, are inside a page called class.pager.php which is brought into index.php(where it captures the $sort variable) via include, it was set to require_once, but i changed it to include to test, but it didn't help. it also didnt hurt, so i kept it as include.


thanks in advance to anyone that can help me understand why this isnt working, and how i can fix this


btw, index.php captures the sort variable via
$sort = $_GET['sort'];
Link to comment
Share on other sites

not two equal signs? on my index php page i use double equal signs


cause it detects it correctly with the if ($sort == 'new' || $sort == '') {

it detects the empty variable correctly, cause the url that always shows up in my 'next' link is ?new&page=

so is there something different when checking for an empty variable, rather than checking for something like 'new' or 'old'?

I thought == was 'equal to' and = is equals, like for inputing something into the variable. . .
Link to comment
Share on other sites

I use index.php?page=X, where X is either most, least, old, or new
that's how i stuff one of those four into the variable $sort

and in index.php i use $sort = $_GET['sort']; to capture it.

but the function listed above, that i added these 4 if lines to is a class.page.php that i 'include' into index.php. should i add a $sort = $_GET['sort']; line in the class.page.php?(doesnt work)
Link to comment
Share on other sites

yes, it echos correctly. but if each are in an if statement, why would it echo 'Next' four different times, one with each value as $sort

http://digg.sytes.net/digg

you can check and see the 4 'Next' links at the bottom. along with the echo to $sort. if you go up to the menu, hover over 'all articles' and click between the newest, oldest, least, and most. at the bottom when $sort is echo'd it changes with each different page. so at least it's capturing the variable correctly


so i suspect the 4 if statements i have in the class.pager.php are in the wrong syntax
Link to comment
Share on other sites

sweet

if($_GET[sort]=='new' ) {

function

}


that worked. but i had to remove the ! before the $.

and since im reducing so much other code size, having this one class.pager.php 4 times as big, is not a big deal at allllllll!!!!


thanks again redarrow. i added you to the special thanks section as well
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.