Dville Posted July 20, 2006 Share Posted July 20, 2006 so i used to have new.php most.php least.php and old.phpeach 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 variableso 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.phpbut 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 ashttp://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 thisbtw, index.php captures the sort variable via $sort = $_GET['sort']; Quote Link to comment https://forums.phpfreaks.com/topic/15103-mysql-paging-and-sort-variables/ Share on other sites More sharing options...
Dville Posted July 20, 2006 Author Share Posted July 20, 2006 bump. . .anyone have any ideas? maybe there's some confusion I could clear up? Quote Link to comment https://forums.phpfreaks.com/topic/15103-mysql-paging-and-sort-variables/#findComment-61354 Share on other sites More sharing options...
redarrow Posted July 20, 2006 Share Posted July 20, 2006 $sort='old' ect ect ect ............ Quote Link to comment https://forums.phpfreaks.com/topic/15103-mysql-paging-and-sort-variables/#findComment-61366 Share on other sites More sharing options...
Dville Posted July 20, 2006 Author Share Posted July 20, 2006 not two equal signs? on my index php page i use double equal signscause 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. . . Quote Link to comment https://forums.phpfreaks.com/topic/15103-mysql-paging-and-sort-variables/#findComment-61370 Share on other sites More sharing options...
Dville Posted July 20, 2006 Author Share Posted July 20, 2006 I use index.php?page=X, where X is either most, least, old, or newthat's how i stuff one of those four into the variable $sortand 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) Quote Link to comment https://forums.phpfreaks.com/topic/15103-mysql-paging-and-sort-variables/#findComment-61375 Share on other sites More sharing options...
Dville Posted July 20, 2006 Author Share Posted July 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/15103-mysql-paging-and-sort-variables/#findComment-61384 Share on other sites More sharing options...
redarrow Posted July 20, 2006 Share Posted July 20, 2006 are you post $sort in the page.echo every $sort out ok.are they set correctly. Quote Link to comment https://forums.phpfreaks.com/topic/15103-mysql-paging-and-sort-variables/#findComment-61387 Share on other sites More sharing options...
redarrow Posted July 20, 2006 Share Posted July 20, 2006 what about useing a if(!$_GET[sort]=='new' ) {function} Quote Link to comment https://forums.phpfreaks.com/topic/15103-mysql-paging-and-sort-variables/#findComment-61391 Share on other sites More sharing options...
Dville Posted July 20, 2006 Author Share Posted July 20, 2006 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 $sorthttp://digg.sytes.net/diggyou 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 correctlyso i suspect the 4 if statements i have in the class.pager.php are in the wrong syntax Quote Link to comment https://forums.phpfreaks.com/topic/15103-mysql-paging-and-sort-variables/#findComment-61393 Share on other sites More sharing options...
redarrow Posted July 20, 2006 Share Posted July 20, 2006 try a ?& way in the links of the pages ok. Quote Link to comment https://forums.phpfreaks.com/topic/15103-mysql-paging-and-sort-variables/#findComment-61397 Share on other sites More sharing options...
Dville Posted July 20, 2006 Author Share Posted July 20, 2006 sweetif($_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 Quote Link to comment https://forums.phpfreaks.com/topic/15103-mysql-paging-and-sort-variables/#findComment-61401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.