Jump to content


Photo

mysql paging and sort variables


  • Please log in to reply
10 replies to this topic

#1 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 20 July 2006 - 04:41 AM

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

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

}


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


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


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

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


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'];

#2 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 20 July 2006 - 09:35 PM

bump. . .

anyone have any ideas? maybe there's some confusion I could clear up?

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 20 July 2006 - 09:50 PM

$sort='old' ect ect ect ............
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 20 July 2006 - 09:52 PM

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. . .

#5 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 20 July 2006 - 10:00 PM

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)

#6 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 20 July 2006 - 10:19 PM

when i make the double quotes into single quotes, i get 4 next links, one for most, one for new, one for old, and one for least.

so it's like it isn't doing the if statement correctly for some reason.

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 20 July 2006 - 10:30 PM

are you post $sort in the page.

echo every $sort out ok.

are they set correctly.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 20 July 2006 - 10:33 PM

what about useing a

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

function

}
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#9 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 20 July 2006 - 10:34 PM

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

#10 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 20 July 2006 - 10:43 PM

try a ?& way in the links of the pages ok.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#11 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 20 July 2006 - 10:46 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users