dohclude Posted March 17, 2008 Share Posted March 17, 2008 I'm not a seasoned coder in PHP, and I am having problems with one of the scripts on my site. It's basically a database of "homebrew" games and applications everything is divided by categories. The problem I am having is that the selection doesn't work properly. Say for instance you are in the "Nintendo DS" section for example. The url would be: http://www.modawii.com/homebrew/categoryResults.php?cat=5 If you were to try and show 15 items per page as opposed to the default of 5 per page, it would send you to the wrong url: http://www.modawii.com/homebrew/categoryResults.php?cnt=15 What it should have done was this: http://www.modawii.com/homebrew/categoryResults.php?cat=5&cnt=15 There are also some other similar problems I am having as well, like when I click the ">>" button which takes you to the next page it simply adds another "?page=1" to the end of the url everytime I click it like this: http://www.modawii.com/homebrew/categoryResults.php?cat=5?page=1?page=1?page=1 When it should do this: http://www.modawii.com/homebrew/categoryResults.php?cat=5&page=2 I hope you are still following me on this, I am trying to explain the best that I can. You can take a look at the page and see what I mean by visiting http://www.modawii.com/homebrew and playing around with it yourself. Here is my code for my "categoryResults.php": <? include("./config/config.php"); if($_GET['cat']){ $WHERE = "Category='".$_GET['cat']."'"; } else{ header("Location: ./index.php"); } $page = "search homebrew"; include("./config/header.php"); $pg = $_GET['page']; if(!$_GET['cnt']){$cnt = 5;} else{$cnt = $_GET['cnt'];} if(!$pg){$pg = 1;} $count = (($pg * $cnt)-$cnt); if($count < 0){$count = 0;} $i=0; $query = mysql_query("SELECT ID,Title,Category,DishImage,description,ratingStr FROM ".$cfg['homebrewTable']." WHERE ".$WHERE." AND approved='yes' LIMIT ".$count.",".$cnt); while(list($ID,$title,$category,$image,$description,$ratingStr)=mysql_fetch_row($query)){ echo "<p>"; echo smallHomebrewTable($ID,$title,$category,$image,$description,$ratingStr,$cfg['bodyWidth']); $i++; } echo table(1,0,$cfg['bodyWidth']); echo tr(); echo td(1,"basicText","left",0,0,0,0," style='padding:5px;'").perPageSelect($cnt,"categoryResults.php","&cat=".$_GET['cat']).td(2); echo td(1,"basicText","right",0,0,0,0," style='padding:5px;'"); echo pageLinks($cfg['homebrewTable'],"./categoryResults.php?cat=".$_GET['cat'],$WHERE,$pg,$count,$cnt); echo td(2); echo tr(2); echo table(2); include("./config/footer.php"); ?> This has been driving me nuts for days now and I can't seem to get it working properly. Any help would be greatly appreciated. Thanks! Link to comment https://forums.phpfreaks.com/topic/96617-category-select-and-per-page-problems/ Share on other sites More sharing options...
Jeremysr Posted March 17, 2008 Share Posted March 17, 2008 For the first problem you should use a hidden field, put it somewhere inside the <form> tags: <input type="hidden" name="cat" value="<?php echo $_GET['cat']; ?>" /> For the second problem, I think we need the code for at least the pageLinks() function to help. Link to comment https://forums.phpfreaks.com/topic/96617-category-select-and-per-page-problems/#findComment-494430 Share on other sites More sharing options...
dohclude Posted March 17, 2008 Author Share Posted March 17, 2008 Oops, forgot that. Here is the "pageLinks" function: function pageLinks($dbTable,$link,$where,$page,$count,$cnt){ GLOBAL $cfg,$_GET; if($page <= 0){$page = 1;} if(!empty($where)){$WHERE = " WHERE ".$where;} $cntQ = @mysql_query("SELECT * FROM ".$dbTable.$WHERE); $total = @mysql_num_rows($cntQ); $pageCount = ceil((($total / $cnt))); if($page > $pageCount){$page = $pageCount;} if($pageCount <= 0){$pageCount = 1;} $prev = $page - 1; $next = $page + 1; if($prev < 1){$prev = 1;} if($next > $pageCount){$next = $pageCount;} if($page <= 1){ $prevDisabled = " DISABLED"; }else{ $prevDisabled = ""; } if($pageCount <= 1){ $selectDisabled = " DISABLED"; }else{ $selectDisabled = ""; } $str[] = "<div>"; $str[] = " <input type=button class='flatButton' value='<<' onClick=\"window.location='".$link."?page=".$prev."';\"".$prevDisabled.">"; $str[] = "<select name=page onChange=\"window.location='".$link."?page=' + this.value;\" style='font: normal 8pt Arial;'".$selectDisabled.">"; for($i=1; $i <= $pageCount; $i++){ if($i < ($pageCount)){$sep = " ";} else{$sep = "";} if($i == $page){ // $str[] = "<b>".$i."</b>".$sep; }else{ // $str[] = "<a href='".$link."?page=".$i."'>".$b.$i.$bc."</a>".$sep; } if($i == $page){$selected = " SELECTED";} else{$selected = "";} $from = ($i * $cnt)-($cnt - 1); $to = ($from + $cnt)-1; $str[] = "<option value='".$i."'".$selected.">".$from." to ".$to."</option>"; } $str[] = "</select>"; if($pageCount <= 0){ $str[] = "<b>".$i."</b>".$sep; } if($page == $pageCount){ $nextDisabled = " DISABLED"; }else{ $nextDisabled = ""; } $str[] = "<input type=button class='flatButton' value='>>' onClick=\"window.location='".$link."?page=".$next."';\"".$nextDisabled.">"; $str[] = "</div>"; return implode("\n",$str); } and also here is the "perPageSelect" function: function perPageSelect($cnt,$redirect='searchResults.php',$urlVars=""){ GLOBAL $_GET; $arr[] = 5; $arr[] = 10; $arr[] = 25; $arr[] = 50; $arr[] = 100; $arr[] = 200; $arr[] = 500; $arr[] = "all"; $str[] = "<select name='pp' onChange=\"window.location='./".$redirect."?cnt=' + this.value;\" style='font: normal 8pt Arial;'>"; for($i=0; $i < count($arr); $i++){ if($arr[$i] == $cnt){$sel = " SELECTED";} else{$sel = "";} $str[] = "<option value='".$arr[$i]."'".$sel.">".$arr[$i]." per page</option>"; } $str[] = "</select>"; return implode("\n",$str); } Link to comment https://forums.phpfreaks.com/topic/96617-category-select-and-per-page-problems/#findComment-494473 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.