Pagination - Driving Me Insane

Post here is you are having problems with any of the tutorials.
Post Reply
User avatar
TerryHarvey
Posts: 20
Joined: Thu Mar 01, 2012 11:12 am

Pagination - Driving Me Insane

Post by TerryHarvey »

I'm ready to pull my hair out. *frustrated face*.

My problem is that I have multiple fields in my table and I want to display them in the pagination. Instead of just having "username" like in the tutorial, I want "username" and "bio".

Can anyone help? Here's my code:
<?php  
include ('config.php');

if (isset($_SESSION['user_id'])) {
	$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
	$limit = 3;

	$sql = mysql_query("SELECT * FROM `users`");

	?>

		<table>
			<tr>
				<th>Username</th>
				<th>Website</th>
			</tr>

			<?php foreach (fetchUsers($page, $limit) as $user) { ?>

			<tr>
				<td><?php echo $user; ?></td>
				<td>Joined</td>
			</tr>

			<?php } ?>

		</table>

	<?php

	$pages = ceil(fetchTotalusers() / $limit);
		
	echo '<br />';

	for ($i = 1; $i <= $pages; ++$i) {
		echo ' <a href="?page='.$i.'">'.$i.'</a> ';
	}
} else {
	loginForm();
}

?>
The function:
function fetchUsers($page, $per_page) {
	$start = ($page -1) * $per_page;
	$per_page = $per_page;

	$query = mysql_query("SELECT * FROM `users` LIMIT $start, $per_page");

	while ($row = mysql_fetch_assoc($query)) {
		$users[] = $row['user_username'];
		$date = date("D M d, Y",$row['user_joined']);
	}

	return $users;
}
User avatar
TerryHarvey
Posts: 20
Joined: Thu Mar 01, 2012 11:12 am

Re: Pagination - Driving Me Insane

Post by TerryHarvey »

False alarm!

After much fiddling, I finally figured out how to fix it myself by switching the foreach loop to a while loop. :P
Post Reply