Jump to content

Pagination with Function -> Need Query Outside of Function


chaseman

Recommended Posts

I've followed the PHP Freaks pagination tutorial which you can find here.

 

And I also got it to work, the only problem is that the script won't work when I use the query outside of the function.

 

Here's the function:

 


<?php 
function knuffix_list ($query, $dbc) {

// find out how many rows are in the table:
$query_row = "SELECT COUNT(*) FROM con";
$query_run = mysqli_query ($dbc, $query_row);
$row = mysqli_fetch_row($query_run);
$num_rows = $row[0];

// number of rows to show per page
$rows_per_page = 5;

// find total pages -> ceil for rounding up
$total_pages = ceil($num_rows / $rows_per_page);

// get the current page or set a default
if (isset($_GET['current_page']) && is_numeric($_GET['current_page'])) {

// make it an INT if it isn't
$current_page = (int) $_GET['current_page'];

} else {
	// default page number
	$current_page = 1;
	}


// if current page is greater than total pages then set current page to last page
if ($current_page > $total_pages) {
$current_page = $total_pages;
}

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

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

echo "test " . $query;

// SCRIPT ONLY WORKS IF I INSERT QUERY HERE

$query	= "SELECT * FROM con, user WHERE con.user_id = user.user_id ORDER BY contributed_date DESC LIMIT $offset, $rows_per_page";

$data = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc));						

// Loop through the array of data
while ($row = mysqli_fetch_array ($data)) {

	global $array;

	// Variables for the table
	$con_id = $row['con_id'];
	$likes_count = $row['likes'];
	$dislikes_count = $row['dislikes'];
	$dbuser_name = $row['nickname'];
	$dbuser_avatar = $row['avatar'];
	$user_id = $row['user_id'];

	// The TABLE
	echo "<table padding='0' margin='0' class='knuffixTable'>";
	echo "<tr><td width='65px' height='64px' class='avatar_bg' rowspan='2' colpan='2'><img src='avatar/$dbuser_avatar' alt='avatar' /></td>";
	echo "<td class='knuffix_username'><strong><a href='profile.php?user=$dbuser_name' title='Profile of $dbuser_name'>" . $dbuser_name . "</a> ___ " . $user_id . "____ <form action='' method='POST'><button type='submit' name='favorite' value='fav'>Favorite</button></form>";
	echo "</strong><br />" . $row['category'] .  " | " . date('M d, Y', strtotime($row['contributed_date'])) . "</td></tr><tr><td>";
	echo "<form action='' method='post'>
			<button class='LikeButton' type='submit' name='likes' value='+1'>Likes</button>
			<button class='DislikeButton' type='submit' name='dislikes' value='-1'>Dislikes</button>
			<input type='hidden' name='hidden_con_id' value='" . $con_id . "' />
			</form></td><td class='votes'>Y[" . $likes_count . "] | N[" . $dislikes_count . "]</td></tr>";
	echo "<tr><td class='knuffix_name' colspan='3'><strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>";
	echo "<tr><td colspan='2' class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>";
	echo "</table>";

	// POST BUTTONS inside the table
	$likes = $_POST['likes'];
	$dislikes = $_POST['dislikes'];
	$con_id = $_POST['hidden_con_id'];
	$favorite = $_POST['favorite'];

	$array = array ($likes, $dislikes, $con_id, $user_id, $favorite);


}


/********* build the pagination links *********/	
// BACKWARD
// if not on page 1, show back links and show << link to go back to the very first page
if ($current_page > 1) {

echo " <a href='{$_SERVER['PHP_SELF']}?current_page=1'><<</a> ";

// get previous page number and show < link to go to previous
$prev_page = $current_page - 1;

echo " <a href='{$_SERVER['PHP_SELF']}?current_page=$prev_page'><</a> ";

}


// CURRENT
// range of number of links to show
$range = 3;

// loop to show links in the range of pages around current page
for ($x = ($current_page - $range); $x < (($current_page + $range) + 1); $x++) {

// if it's a valid page number...
if (($x > 0) && ($x <= $total_pages)) {

	// if we're on current page
	if ($x == $current_page) {

	// highlight it but don't make a link out of it
	echo "[<b>$x</b>]";

	// if it's not the current page then make it a link
	} else {
		echo "<a href='{$_SERVER['PHP_SELF']}?current_page=$x'>$x</a>";
	}
}
}


// FORWARD
// if not on the last page, show forward and last page links
if ($current_page != $total_pages) {
// get next page
$next_page = $current_page + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?current_page=$next_page'>></a> ";
// echo forward link for last page
echo " <a href='{$_SERVER['PHP_SELF']}?current_page=$total_pages'>>></a> ";
}

/***** end building pagination links ****/



mysqli_close($dbc);





}

?>

 

As you can see above, the script will only work if I put the query right below the $offset variable and above the $data variable.

 

I put the test echo above the query to see how the query looks like when I induce the query from the outside through the function parenthesis into the function, and this is what I get printed out:

 

test SELECT * FROM con, user WHERE con.user_id = user.user_id ORDER BY contributed_date DESC LIMIT ,

 

Obviously the $offset and the $rows_per_page variables are not set, when I induce the query from the outside into the function.

 

So in that sense my question is:

 

How can I induce the query from the outside into the function SO THAT the $offset and the $rows_per_page variables are set as well and NOT empty?

 

 

p.s.

 

I need the query outside of the function because I'm using a sort by category functionality.

Link to comment
Share on other sites

I solved this problem, by splitting it up into parts, likes this:

 

if (($select_category == 'All') || (!isset($select_category)) && (!isset($most_liked))) {

	pagination_start ($dbc);

	$query	= "SELECT * FROM con, user WHERE con.user_id = user.user_id ORDER BY contributed_date DESC LIMIT $pag_array[0], $pag_array[1]";

	knuffix_list ($query, $dbc);

	pagination_end ($pag_array);

 

pagination_start, above the query, and pagination_end below.

 

 

 

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.