farril Posted November 7, 2008 Share Posted November 7, 2008 hi there. iv got a forum script which works perfectly (made from someones tutorial). i then tryed to add page numbers to it (again from someone elses tutorial). the page numbers count up, but all the posts seem to still go on the first page, and not carry down to the other pages. could someone please have a read over my code and see whats wrong? <?php include "connect.php"; //mysql db connection here print "<link rel='stylesheet' href='style.css' type='text/css'>"; print "<A href='post.php'>New Topic</a><br>"; $blog_postnumber = 9;if(!isset($_GET['page'])) { $page = 1; }else { $page = (int)$_GET['page']; } $from = (($page * $blog_postnumber) - $blog_postnumber); print "<table class='maintable'>"; print "<tr class='headline'><td width=50%>Topic</td><td width=20%>Topic Starter</td><td>Replies</td><td>Last replied time</td></tr>"; $getthreads="SELECT * from forumtutorial_posts where parentid='0' order by lastrepliedto DESC"; $getthreads2=mysql_query($getthreads) or die("Could not get threads"); while($getthreads3=mysql_fetch_array($getthreads2)) { $getthreads3[title]=strip_tags($getthreads3[title]); $getthreads3[author]=strip_tags($getthreads3[author]); print "<tr class='mainrow'><td><A href='message.php?id=$getthreads3[postid]'>$getthreads3[title]</a></td><td>$getthreads3[author]</td><td>$getthreads3[numreplies]</td><td>$getthreads3[showtime]<br>Last post by <b>$getthreads3[lastposter]</b></td></tr>"; } print "</table>"; $total_results = mysql_fetch_array(mysql_query("SELECT COUNT(*) as num FROM forumtutorial_posts where parentid='0'")); $total_pages = ceil($total_results['num'] / $blog_postnumber); if ($page > 1) { $prev = ($page - 1); echo "<a href=\"index.php?page=$prev\"><< Newer</a> "; } for($i = 1; $i <= $total_pages; $i++) { if ($page == $i) { echo "$i "; } else { echo "<a href=\"index.php?page=$i\">$i</a> "; } } if ($page < $total_pages) { $next = ($page + 1); echo "<a href=\"index.php?page=$next\">Older >></a>"; } ?> many many thanks if you can solve it! Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/ Share on other sites More sharing options...
ILMV Posted November 7, 2008 Share Posted November 7, 2008 Hello! I think it is important to learn how to indent your code, it's unreadable. Also, you shouldn't echo HTML, it wastes server resources. I will attempt to find your problem... Edit: For others, here is the code indented... <?php include "connect.php"; //mysql db connection here print "<link rel='stylesheet' href='style.css' type='text/css'>"; print "<A href='post.php'>New Topic</a><br>"; $blog_postnumber = 9;if(!isset($_GET['page'])) { $page = 1; } else { $page = (int)$_GET['page']; } $from = (($page * $blog_postnumber) - $blog_postnumber); print "<table class='maintable'>"; print "<tr class='headline'><td width=50%>Topic</td><td width=20%>Topic Starter</td><td>Replies</td><td>Last replied time</td></tr>"; $getthreads="SELECT * from forumtutorial_posts where parentid='0' order by lastrepliedto DESC"; $getthreads2=mysql_query($getthreads) or die("Could not get threads"); while($getthreads3=mysql_fetch_array($getthreads2)){ $getthreads3[title]=strip_tags($getthreads3[title]); $getthreads3[author]=strip_tags($getthreads3[author]); print "<tr class='mainrow'><td><A href='message.php?id=$getthreads3[postid]'>$getthreads3[title]</a></td><td>$getthreads3[author]</td><td>$getthreads3[numreplies]</td><td>$getthreads3[showtime]<br>Last post by <b>$getthreads3[lastposter]</b></td></tr>"; } print "</table>"; $total_results = mysql_fetch_array(mysql_query("SELECT COUNT(*) as num FROM forumtutorial_posts where parentid='0'")); $total_pages = ceil($total_results['num'] / $blog_postnumber); if ($page > 1) { $prev = ($page - 1); echo "<a href=\"index.php?page=$prev\"><< Newer</a> "; } for($i = 1; $i <= $total_pages; $i++) { if ($page == $i) { echo "$i "; } else { echo "<a href=\"index.php?page=$i\">$i</a> "; } } if ($page < $total_pages) { $next = ($page + 1); echo "<a href=\"index.php?page=$next\">Older >></a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/#findComment-684229 Share on other sites More sharing options...
dezkit Posted November 7, 2008 Share Posted November 7, 2008 you shouldn't echo HTML, it wastes server resources ...? Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/#findComment-684233 Share on other sites More sharing options...
ILMV Posted November 7, 2008 Share Posted November 7, 2008 You shouldn'y do this ... print "<table class='maintable'>"; PHP is an embedded language, therefore there is no need to echo it, it should be this... ?><table class='maintable'><?php The aim is to close the PHP tag, do a bit of HTML then reopen it. Do this rather than echoing it, all of your if statements etc should still work. To give you another example, see below... <?php if($_POST['foo'])!='bar') { echo("<p>Omg I am echoing HTML, this is wrong...</p>"); } else { ?><p>Now, I am allowing the HTML to be returned naturally to the browser</p><?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/#findComment-684394 Share on other sites More sharing options...
farril Posted November 7, 2008 Author Share Posted November 7, 2008 all that script above is taken from a tutorial, iv never used print or echo in any other case. any idea on what needs fixing? Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/#findComment-684445 Share on other sites More sharing options...
runnerjp Posted November 7, 2008 Share Posted November 7, 2008 You shouldn'y do this ... print "<table class='maintable'>"; PHP is an embedded language, therefore there is no need to echo it, it should be this... ?><table class='maintable'><?php The aim is to close the PHP tag, do a bit of HTML then reopen it. Do this rather than echoing it, all of your if statements etc should still work. To give you another example, see below... <?php if($_POST['foo'])!='bar') { echo("<p>Omg I am echoing HTML, this is wrong...</p>"); } else { ?><p>Now, I am allowing the HTML to be returned naturally to the browser</p><?php } ?> i disagree... echoing out html is fine.. its all dependant on the coder choice of doing it! Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/#findComment-684459 Share on other sites More sharing options...
Adam Posted November 7, 2008 Share Posted November 7, 2008 Hah well getting technical.. ?><table class='maintable'><?php should be.. ?><table class="maintable"><?php I think things like not printing HTML is just nit picking - never going to waste enough server resources to actually matter.. perhaps a microsecond or two, if that? Adam Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/#findComment-684466 Share on other sites More sharing options...
ILMV Posted November 7, 2008 Share Posted November 7, 2008 This is a quote from this forum... http://www.vbforums.com/showpost.php?p=2174390&postcount=5 PHP is/was originaly intended to be a templating language. Which is why, by default you are always in HTML mode. It is never a good idea to output large chunks of HTML using print and echo statements. Two of the reasons, readability and escaping quotes you have already identified; but here are a couple more: * When ever you use a string enclosed in double quotes, PHP needs to check the entire string for embedded variables. This gives the PHP interpreter unecessary processing and makes the script slower. * Any kind of output in the middle of a script is a bad idea. You should always keep the output clearly separate from the process. Limit any logic in output to display logic and keep the process logic, such as accessing databases in the main body of the script. Of course a human being is free to do whatever they like, so there will be no hard feelings regardless of anyone's choice of echoing HTML. However, the points above describe bad coding practices, and if you went into an organisation showing off those style of code they may take a negative view on your coding skills. Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/#findComment-684467 Share on other sites More sharing options...
Adam Posted November 7, 2008 Share Posted November 7, 2008 Fair point ILMV .. Anyway, coming back to his original question, your problem lies with your query. You need to look into using "LIMIT" in mysql.. For example: SELECT * from forumtutorial_posts where parentid='0' order by lastrepliedto DESC LIMIT 0, 10 That would display onnly the first 10 rows.. Adam Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/#findComment-684468 Share on other sites More sharing options...
farril Posted November 7, 2008 Author Share Posted November 7, 2008 thanks alot fixed with this line $getthreads="SELECT * from forumtutorial_posts where parentid='0' order by lastrepliedto desc LIMIT $from, $blog_postnumber"; also. is there any easier way to tidy up all my code and get it indented propperly? im using dreamweaver cs4. Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/#findComment-684475 Share on other sites More sharing options...
ILMV Posted November 7, 2008 Share Posted November 7, 2008 also. is there any easier way to tidy up all my code and get it indented propperly? im using dreamweaver cs4. I use CS3, and there is no way of tidying PHP code here, however there is a function to tidy HTML code. Its something that will learn as you go along, alternatively you could always download PHP designer, copy your php from CS4 and tidy it in this... http://www.mpsoftware.dk/phpdesigner.php ...Just to reassure you, I started off echoing html and not indenting my code. I was told by a fellow forum member (years ago), that he would not help me until i fixed these issues , its certainly a practice that you should learn. Good Luck! Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/#findComment-684481 Share on other sites More sharing options...
Adam Posted November 7, 2008 Share Posted November 7, 2008 Well when you develop the code just indent as you go along. You generally indent code when it's encased with curly braces: function myfunction() { // encased once so 1 indent if ($this == $that) { // encased twice so 2 indents } } There are a few exceptions which don't follow that, like when using a switch () .. but genrally anything encased just gets an extra indented. You'd be best following a coding standard, like PEAR's: http://pear.php.net/manual/en/standards.php .. can't go far wrong with following that! Indention is purely for readbility of the developer.. Adam Quote Link to comment https://forums.phpfreaks.com/topic/131725-pages-numbers-in-a-simple-forum/#findComment-684482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.