Page 1 of 1

Page system

Posted: Sun May 08, 2011 10:48 am
by Carbine
I'm doing 10 posts per page, and in the url it will be s=0, which is page 1, s=10, which is page 2 ect. And what page it is on appears bold. But if s=11, I want to round the 11 down to 10 when it comes to making the page numbers appear bold. Here is my current code:
if (isset($_GET['s'])) $start = (int)$_GET['s']; else $start = 0;
$gposts = mysql_query("SELECT * FROM articles_posts WHERE parent='$aid' LIMIT $start, 10");
$count = mysql_num_rows($gposts);
$i=1;
		for ($x=0;$x<$count;$x=$x+10)
		{
			if ($start < 10) $round = round($x, 0, PHP_ROUND_HALF_DOWN);
			else if ($start < 100) $round = round($x, -1, PHP_ROUND_HALF_DOWN);
			else if ($start > 100) $round = round($x, -2, PHP_ROUND_HALF_DOWN);
			if ($start!=$round)
				echo "<a href='articles.php?act=article&id=$id&s=$x'>$i</a> ";
			else
				echo "<a href='articles.php?act=article&id=$id&s=$x'><b>$i</b></a> ";
			$i++;
		}
I obviously have more code...I just select out the bits needed... Thanks

Re: Page system

Posted: Sun May 08, 2011 11:02 am
by jacek
$count is the problem I believe, you are displaying the page numbers based on the query which you limit the rows to 10.

You need to add a second query which gets the total number of rows in the table.

Re: Page system

Posted: Sun May 08, 2011 11:06 am
by Carbine
ahh yes, that's the only use of count, i'll change it to:
$count = mysql_query("SELECT COUNT('id') FROM articles_posts WHERE $parent='$aid'");
It still doesn't appear bold, and I get a page 2 when there is only 2 posts....
/E Shall I post the whole page script?

Re: Page system

Posted: Sun May 08, 2011 11:33 am
by jacek
Carbine wrote:/E Shall I post the whole page script?
That may be useful ;)

You need to fetch the result of that query also, ie using mysql_result.

Re: Page system

Posted: Sun May 08, 2011 11:37 am
by Carbine
<?php
@$id = (int)$_GET['id'];
if ($id)
{
	if (isset($_GET['s'])) $start = (int)$_GET['s']; else $start = 0;
	$gtopic = mysql_query("SELECT * FROM articles_topics WHERE id='$id'");
	$gtrows = mysql_num_rows($gtopic);
	if ($gtrows==1)
	{
		while ($gtfetch = mysql_fetch_assoc($gtopic))
		{
			$aid = $gtfetch['id'];
			$title = $gtfetch['title'];
			$parent = $gtfetch['parent'];
			$link = "<a href='articles.php?act=articles&id=$aid'>$title</a>";
			while ($parent!=0)
			{
				$gcat = mysql_query("SELECT * FROM articles WHERE id='$parent'");
				$gcfetch = mysql_fetch_assoc($gcat);
				$name = $gcfetch['name'];
				$gid = $gcfetch['id'];
				$parent = $gcfetch['parent'];
				$link = "<a href='articles.php?act=cat&id=$gid'>$name</a> - ".$link;
			}
			echo $link;
		}
		echo "<p /><div id='head'><b>$title</b></div>";
		$gposts = mysql_query("SELECT * FROM articles_posts WHERE parent='$aid' LIMIT $start, 10");
		$count = mysql_query("SELECT COUNT('id') FROM articles_posts WHERE $parent='$aid'");
		echo "<div id='table'><table width='100%'>";
		while ($gpfetch = mysql_fetch_assoc($gposts))
		{
			$message = bbcode(nl2br($gpfetch['message']));
			$pid = $gpfetch['id'];
			$date = $gpfetch['date'];
			$uid = $gpfetch['uid'];
			$edited = $gpfetch['edited'];
			$lastedited = $gpfetch['lastedited'];
			$guser = mysql_query("SELECT * FROM users WHERE id='$uid'");
			$gufetch = mysql_fetch_assoc($guser);
			$username = $gufetch['username'];
			$posts = mysql_query("SELECT * FROM articles_posts WHERE uid='$uid'");
			$posts = mysql_num_rows($posts);
			echo "<tr><td wdith='15%'><div id='posts'><center><a href='ucp.php?profile=$uid'>$username</a><br />Posts: $posts</center></div></td><td width='85%'>$message<p><font size='-1'>Posted on $date";
			if ($edited==1) echo "(Last edited on: $lastedited)";
			if (loggedin())
			{
				if ($_SESSION['username']==$username || admin()>=1) echo "<a href='articles.php?act=edit&id=$pid'><div align='right'>Edit</div></a>";
			}
			echo "</font><hr /></td></tr>";
		}
		echo "</table></div>";
		echo "<div style='text-align: right;'>";
		$i=1;
		for ($x=0;$x<$count;$x=$x+10)
		{
			if ($start < 10) $round = round($x, 0, PHP_ROUND_HALF_DOWN);
			else if ($start < 100) $round = round($x, -1, PHP_ROUND_HALF_DOWN);
			else if ($start > 100) $round = round($x, -2, PHP_ROUND_HALF_DOWN);
			if ($start!=$round)
				echo "<a href='articles.php?act=article&id=$id&s=$x'>$i</a> ";
			else
				echo "<a href='articles.php?act=article&id=$id&s=$x'><b>$i</b></a> ";
			$i++;
		}
		echo "</div>";
	}
	else echo "The article does not exist!";
}
?>
Kind of a long script I suppose, half of the shit on there has nothing to do with this situation :P Thanks :D

Re: Page system

Posted: Sun May 08, 2011 11:47 am
by jacek
Well you still need to ge the result of that query
$count = mysql_query("SELECT COUNT('id') FROM articles_posts WHERE $parent='$aid'");
should be
$count = mysql_query("SELECT COUNT('id') FROM articles_posts WHERE $parent='$aid'");
$count = mysql_result($count, 0);

Re: Page system

Posted: Sun May 08, 2011 11:50 am
by Carbine
I tried that, but then no numbers appeared at all...

Re: Page system

Posted: Sun May 08, 2011 11:52 am
by jacek
If you output $count, is it the number you expect ?

Re: Page system

Posted: Sun May 08, 2011 11:55 am
by Carbine
No, for every 10 it counts there is a page number, for if there was 16 there should be page 1 and 2. But it's outputting nothing, and it should kind of be like this forum number system thing.

Re: Page system

Posted: Sun May 08, 2011 12:01 pm
by jacek
yeah but just from the query above, the $count variable should contain the number of rows in the table.
echo $count;
does that give you the right number ?

Re: Page system

Posted: Sun May 08, 2011 12:08 pm
by Carbine
Ahhh, that helped a lot, it outputted 0 and i figured out this was the problem:
$count = mysql_query("SELECT * FROM articles_posts WHERE $parent='$aid'");
Thanks for the help :D But it doesn't solve my original problem, which is if s was 1, I would want page 1 to be bold even though it only does that when s = 0. But not to bothered about that at the moment, anyway thanks for help.

Re: Page system

Posted: Sun May 08, 2011 12:36 pm
by Tino
Maybe you're not too bothered about it, but the logic is fairly simple :)

Basically, inside the loop where you display all links, you want to check if $_GET['s'] is equal to the value of the counter, like $i or something. So you could have something like this:
for ( $i = $first; $i <= $last; $i++ ) {
    if ( $_GET['s'] == $i ) {
        echo '<a class="this_page" href="whatever.php?s=' . $_GET['page'] . '"> . $i . '</a>';
    } else {
        echo '<a href="whatever.php?s=' . $_GET['page'] . '"> . $i . '</a>';
    }
}
where $first and $last are the first and last database entries to display.