Jump to content

categories pagination + for loop issues


ncovill

Recommended Posts

Hey all,

I am hoping there is a simple solution behind this, but I've been trying for awhile now and cannot figure it out.

 

I have a website, where there are categories, which people will be able to click on a category. It then goes to: pages.php?action=get_entries&cat_id=categorynamegoeshere - This all works fine. But I cannot for the life of me figure out why the pagination isn't working properly on the category page. I have it to where it works to a point, not 100%. The page #'s are not being displayed for some reason. When I click "Next" it does go to the next page fine, but "First" does not become a link to go back to the first page.

 

If I take off the " WHERE `category` = '$cat_id'" part in the $sql, it displays pages, except it displays all of the pages, not just for that one category lol. I believe that WHERE statement needs to be there.. but again, the #'s will not display properly..

 

This pagination script is working fine, except on categories... from my testing it seems that the problem is the FOR LOOP (you'll see that alittle further down where $x variable is). Any help would be appreciated!

function pagination_catid()
{
$connection = db_connect();

/****** pagination ******/
// find out how many rows are in the table
$cat_id = $_REQUEST['cat_id'];
$sql = "SELECT COUNT(*) FROM `posts` WHERE `category` = '$cat_id'";
$result = mysql_query($sql, $connection);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 10;
//find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage']))
	{
	// cast var as int
	$currentpage = $_GET['currentpage'];
	}
else
	{
	// default page num
	$currentpage = 1;
	}

// if current page is greater than total pages...
if ($currentpage > $totalpages)
	{
	// set current page to last page
	$currentpage = $totalpages;
	}

// if current page is less than first page...
if ($currentpage < 1)
	{
	// set current page to first page
	$currentpage = 1;
	}

// the offset of the list, based on current page
//$offset = ($currentpage - 1) * $rowsperpage;

/******  build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1)
	{
	// show << link to go back to page 1
	echo "<div class='paginleft'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=1'>First</a></div>";
	}
else
	{
	echo "<div class='paginleft'>First</div>";
	}

// if not on last page, show forward and last page links
if ($currentpage != $totalpages)
	{
	// get next page
	$nextpage = $currentpage + 1;
	// echo forward link for next page
	echo "<div class='paginright'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=$nextpage'>Next</a></div>";
	}
else
	{
	echo "<div class='paginright'>Next</div>";
	}

// if not on page 1, don't show back links
if ($currentpage > 1)
	{
	// get previous page num
	$prevpage = $currentpage - 1;
	// show < link to go back to 1 page
	echo "<div class='paginright'>|</div>";
	echo "<div class='paginright'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=$prevpage'>Previous</a></div>";
	}
else
	{
	echo "<div class='paginright'>|</div>";
	echo "<div class='paginright'>Previous</div>";
	}

// range of num links to show
$range = 5;
$range2 = 4;

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range2) + 1); $x++)
	{
	// if it's a valid page number...
	if (($x > 0) && ($x <= $totalpages))
		{
		// if we're on current page...
		if ($x == $currentpage)
			{
			// highlight it but don't make a link
			echo "<div class='paginx'><b>$x</b></div>";
			}
		// if not current page
		else
			{
			// make it a link
			echo "<div class='paginx'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=$x'>$x</a></div>";
			}
		}
	}

if ($currentpage != $totalpages)
	{
	if ($totalpages > 5)
		{
		if ($totalpages < (($currentpage + $range2) + 1))
			{
			echo "";
			}
		else
			{
			echo "<div class='paginx'> ... </div> ";
			echo "<div class='paginx'><a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>$totalpages</a></div> ";
			}
		}
	// echo forward link for lastpage
	echo "<div class='paginlast'><a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a></div> ";
	}
else
	{
	echo "<div class='paginlast'>Last</div>";
	}
}

Link to comment
Share on other sites

I'm not on a computer I can test so you might want to check to make sure that this is parsing the variable correctly:

 

<?php
echo "<div class='paginleft'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=1'>First</a></div>";
?>

 

With $cat_id surrounded by text, is PHP parsing that variable correctly? Worth checking the HTML.

Link to comment
Share on other sites

I'm not on a computer I can test so you might want to check to make sure that this is parsing the variable correctly:

 

<?php
echo "<div class='paginleft'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=1'>First</a></div>";
?>

 

With $cat_id surrounded by text, is PHP parsing that variable correctly? Worth checking the HTML.

 

Yep, $cat_id is displaying the proper category. ;x what it does is it 'requests' the category you click on.

Link to comment
Share on other sites

Are you using the category name to allow the user to get to these pages? Perhaps a different page is linking the category name instead of the ID, and this page is expecting the ID instead of the name.

 

If your ID's are numeric, you could make an OO function that checks if cat_id's are numeric and if so, converts them to ints. If they're not numeric, it finds the category ID based on the string (assuming it's a name - with proper escaping you should be safe from SQL injection).

 

If not, I think you'd have to find the source of the problem.

Link to comment
Share on other sites

Ill post more code here.. how categories are brought up and stuff.

this function gets the category and uses the create_output function to display each post:

function get_entries_catid()
{
$connection = db_connect();

/****** pagination ******/
// find out how many rows are in the table
$cat_id = $_REQUEST['cat_id'];

$sql = "SELECT COUNT(*) FROM `posts`";
$result = mysql_query($sql, $connection);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 10;
//find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage']))
	{
	// cast var as int
	$currentpage = $_GET['currentpage'];
	}
else
	{
	// default page num
	$currentpage = 1;
	}

// if current page is greater than total pages...
if ($currentpage > $totalpages)
	{
	// set current page to last page
	$currentpage = $totalpages;
	}

// if current page is less than first page...
if ($currentpage < 1)
	{
	// set current page to first page
	$currentpage = 1;
	}

// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
/****** end pagination ******/

// get info from the db
$query = "SELECT posts.id, posts.content, posts.name, posts.datetime, posts.category, posts.comments, categories.category
		  FROM `posts` LEFT OUTER JOIN `categories`
		  ON posts.category = categories.cat_id ";

if ($cat_id = $_REQUEST['cat_id'])
	{
	if (is_int($cat_id))
		{
		$query .= " WHERE categories.cat_id = ". $cat_id;
		}
	else
		{
		$query .= " WHERE categories.category = \"";
		$query .= mysql_real_escape_string($cat_id);
		$query .= "\"";
		}
	}

$query .= " ORDER BY `id` DESC LIMIT ". $offset .", ". $rowsperpage;
$result = mysql_query($query, $connection);

return create_output($result);
}

 

here is the create_output function, which displays each post:

function create_output($result)
{
$output = "";

while($row = mysql_fetch_assoc($result))
	{
	$output .= "<div class='post'>";
	$output .= "<div class='postcontain'>";
	$output .= "<div class='ventcontain'>";
	$user = $row['name'];
	$user_array = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `username` = '$user'"));
	if ($user == "Anonymous")
		{
		$output .= "<a href='profile.php?user=". $user ."' title=''><img src='images/avatars/avatar_default.jpg' alt='' width='38' height='39' style='border: 1px #fcb540 solid;' /></a>";
		}
	else
		{
		$output .= "<a href='profile.php?user=". $user ."' title=''><img src='". $user_array['avatar'] ."' alt='' width='38' height='39' style='border: 0px;' /></a>";
		}
	if (strlen($row['content']) > 300)
		{
		    $content = substr($row['content'], 0, 300);
			$output .= "<div class='vent'>
					<a href='postdetails.php?id=". $row['id'] ."' style='color: #000000; text-decoration: none;'>". $content ." ... <span style='color: #025AA2; font-size: 11px;'>read more</span></a><hr />";
		}
	else
		{
		$output .= "<div class='vent'>
					<a href='postdetails.php?id=". $row['id'] ."' style='color: #000000; text-decoration: none;'>". $row['content'] ." </a><hr />";
		}
	$output .= "<div class='date'>";
	//$output .= "<a href='profile.php?user=". $row['name'] ."' title=''>". $row['name'] ."</a>    ". $row['datetime'] ."    <a href='pages.php?action=get_entries&cat_id=". $row['category'] ."' title=''>". $row['category'] ."</a>";
	$output .= "<a href='profile.php?user=". $row['name'] ."' title=''>". $row['name'] ."</a>    ". $row['datetime'] ."    <a href='pages.php?action=get_entries&cat_id=". $row['category'] ."' title=''>". $row['category'] ."</a>";
	$output .= "    <a href='postdetails.php?id=". $row['id'] ."'>Comments</a> (". $row['comments'] .")";
	$output .= "</div>";
	$output .= "<div class='share'>

				</div>";
	$output .= "</div>";
	$output .= "</div>";
	$output .= "<div class='clear'>
				</div>";
	$output .= "</div>";
	$output .= "</div>";
	}

return $output;
}

Link to comment
Share on other sites

"Keep it simple stupid" is a saying I tend to follow and didn't in my previous post, I apologize for that.

 

Here's a better way of getting at my previous question. In the statement "$cat_id = $_REQUEST['cat_id'];" what is defining $_REQUEST['cat_id']? Is it defining that array index as the category name?

Link to comment
Share on other sites

"Keep it simple stupid" is a saying I tend to follow and didn't in my previous post, I apologize for that.

 

Here's a better way of getting at my previous question. In the statement "$cat_id = $_REQUEST['cat_id'];" what is defining $_REQUEST['cat_id']? Is it defining that array index as the category name?

 

haha, yea, I believe it is getting "cat_id" from the action cat_id, such as here "pages.php?action=get_entries&cat_id=" and i have $query LEFT OUTER JOIN posts.category with categories.cat_id (posts, is one table with all the posts, categories is another table with only the categories in it) so what it does is in the posts.category column it inputs the category id # but because of:

if ($cat_id = $_REQUEST['cat_id'])
	{
	if (is_int($cat_id))
		{
		$query .= " WHERE categories.cat_id = ". $cat_id;
		}
	else
		{
		$query .= " WHERE categories.category = \"";
		$query .= mysql_real_escape_string($cat_id);
		$query .= "\"";
		}
	}

it displays the category name instead

Link to comment
Share on other sites

You use this if statement: "if (is_int($cat_id))"

 

If $cat_id is being passed to the server via the browser, it will always be text (everything sent via HTML is interpreted as a variable type of string). If this is how $_REQUEST['cat_id'] is defined (by an HTML submission) try using is_numeric instead.

Link to comment
Share on other sites

You use this if statement: "if (is_int($cat_id))"

 

If $cat_id is being passed to the server via the browser, it will always be text (everything sent via HTML is interpreted as a variable type of string). If this is how $_REQUEST['cat_id'] is defined (by an HTML submission) try using is_numeric instead.

 

:( no go, still is picking up the category name

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.