Jump to content

Category Select and Per Page problems


dohclude

Recommended Posts

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!  :D

Link to comment
Share on other sites

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
Share on other sites

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