Page system

Ask about a PHP problem here.
Post Reply
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Page system

Post 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:
[syntax=php]
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++;
}
[/syntax]
I obviously have more code...I just select out the bits needed... Thanks
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Page system

Post 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.
Image
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: Page system

Post by Carbine »

ahh yes, that's the only use of count, i'll change it to:[syntax=php]$count = mysql_query("SELECT COUNT('id') FROM articles_posts WHERE $parent='$aid'");[/syntax]

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?
Last edited by Tino on Sun May 08, 2011 11:08 am, edited 2 times in total.
Reason: No need to double post in such a short time span.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Page system

Post 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.
Image
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: Page system

Post by Carbine »

[syntax=php]<?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!";
}
?>[/syntax]
Kind of a long script I suppose, half of the shit on there has nothing to do with this situation :P Thanks :D
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Page system

Post by jacek »

Well you still need to ge the result of that query

[syntax=php]$count = mysql_query("SELECT COUNT('id') FROM articles_posts WHERE $parent='$aid'");[/syntax]
should be

[syntax=php]$count = mysql_query("SELECT COUNT('id') FROM articles_posts WHERE $parent='$aid'");
$count = mysql_result($count, 0);[/syntax]
Image
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: Page system

Post by Carbine »

I tried that, but then no numbers appeared at all...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Page system

Post by jacek »

If you output $count, is it the number you expect ?
Image
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: Page system

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Page system

Post by jacek »

yeah but just from the query above, the $count variable should contain the number of rows in the table.

[syntax=php]echo $count;[/syntax]
does that give you the right number ?
Image
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: Page system

Post by Carbine »

Ahhh, that helped a lot, it outputted 0 and i figured out this was the problem: [syntax=php]$count = mysql_query("SELECT * FROM articles_posts WHERE $parent='$aid'");[/syntax]
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.
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Page system

Post 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:

[syntax=php]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>';
}
}[/syntax]

where $first and $last are the first and last database entries to display.
Please check out my CodeCanyon items.
Post Reply