Jump to content

making my forum more advanced..


astralaaron

Recommended Posts

From what you posted, this is your SQL statement:$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id' LIMIT";That it not a valid statement, that's why you are getting the error. The LIMIT clause requires 2 parameters after that for the starting row and the number of rows, that's what the $start and $per_page variables are for.As for getting the page variable, I would prefer to do it like this:
$page = intval($_GET['page']);if ($page < 1)  $page = 1;

I don't understand what your confusion is with the page variable. That code, and the code that Mr. Chisol posted, is how you get it from the URL. You already showed how you write it to the URL, so I don't understand what the confusion is about. For the next and back links, you want to print either $page + 1 or $page - 1 in the URL.It might be that a forum is a bit too much to bite off without learning the basics first. You might want to just make a page that gets form input and sends an email, and then a page that writes it to a file, and maybe adds it to a database. Once you understand how to process a form, then something like a forum might be a little more realistic. But if you're trying to make a forum and you don't know how to pass variables from one page to another, you might want to think about reading some more about how to use PHP before you tackle a project like this.

i do understand how to pass a variable to another page, and I understand how to process a form.my forum works already it just doesnt seperate posts on to different pages..did you read how I was making the page variable? and sending it to the viewtopic? was I doing that right?
Link to comment
Share on other sites

From what you posted, this is your SQL statement:$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id' LIMIT";
I actually wrote it like this when I got the error:$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id' LIMIT $start, $per_page";I just forgot to delete the limit part when I posted that!
Link to comment
Share on other sites

okay something new is happening atleast, its not giving me an error anymore.and it is only allowing 15 posts on the page, but it is not posting to a new page after thatany ideas? EDIT: wait I typed in page=2 on my url and the next posts are actually there! its looking like it will work, something is still missingbecause it doesnt bring me to page2 when I keep posting

Link to comment
Share on other sites

Thanks alot you guys, it is working as far as 15posts per page, I just need a little help understanding how to get the next / back buttons to showup / work when 15posts have come up. because right now it just posts the next posts to page=2 but it puts the user at page=1 when they post and also theres no indication that there are more posts on page 2. I am very greatfull for all the time you guys have taken to help me so far! if you can help me get some next / back buttons up to man that would be great!

Link to comment
Share on other sites

Ok, first:Here's how to handle previous/next, first and last (including the code I postd before...)

$total_pages = $total_count / $per_page;if ($total_pages > 1) {	  $tags = array();	  if ($page == 1) {			$tags[] = array( '<span class="disabled_page">', '</span>' );			$tags[] = array( '<span class="disabled_page">', '</span>' );	  } else {			$tags[] = array( '<a href="viewtopic.php?id=' . $id . '&page=1">', '</a>' );		 // First			$tags[] = array( '<a href="viewtopic.php?id=' . $id . '&page=' . ($page - 1) . ">', '</a>' );	 // Previous	  }	  echo $tags[0][0] . '<< First' . $tags[0][1] . '   ';	  echo $tags[1][0] . '< Previous' . $tags[1][1] . '   ';}for ($i=1; $i <= $total_pages; $i++) {   if ($i == $page)	   echo '<span class="disabled_page">';  // changed the class   else	   echo '<a href="viewtopic.php?id=' . $id . '&page=' . $i .'">';   echo $i;   if ($i == $page)	   echo '</span>';   else	   echo '</a>';   echo '  ';}if ($total_pages > 1) {	  $tags = array();	  if ($page >= $total_pages) {			$tags[] = array( '<span class="disabled_page">', '</span>' );			$tags[] = array( '<span class="disabled_page">', '</span>' );	  } else {			$tags[] = array( '<a href="viewtopic.php?id=' . $id . '&page=' . $total_pages . '">', '</a>' ); // Last			$tags[] = array( '<a href="viewtopic.php?id=' . $id . '&page=' . ($page + 1) . ">', '</a>' ); // Next	  }	  echo $tags[1][0] . 'Next >' . $tags[1][1] . '   ';	  echo $tags[0][0] . 'Last >> . $tags[0][1] . '   ';}

That's not perhaps the perfect example/code (from a visitors p.o.v.), but it works fine...

To take the user to their post you could do something like this (I guess that this is how this forum works [but it uses metarefresh, I think]...):
// The INSERT-query etc here...if($result2){	$lastID = mysql_insert_id();	$page = 1;		$cnt_res = mysql_query( "SELECT COUNT(*) FROM $tbl_name2 WHERE question_id=$id" );		$total_count = intval(mysql_result( $cnt_res, 0 ));		if ($total_count > $per_page) {		  $page = intval($total_count / $per_page)	}		// reload to post...	header( 'Location: mmabjj_viewtopic.php?id=' . $id . '&page=' . $page . '#entry' . $lastID );}

Note that if that should work properly tou need to have an anchor by wvery post like

<a name="enrty<?php echo $rows['id']?>"></a>

(Noted that you use while( $rows = mysql_fetc... ), nothing wrong with that, just that rows is pluralis and you only get one row :) )I think that's all, just ask again if there's more :?)

Link to comment
Share on other sites

wow Thanks for taking the time to write all of that, I did not think it was going to be that complicated! I am not sure exactlywhere to put that code, buit I am going to mess with it now and see if I can get it working, thanks again!!

Link to comment
Share on other sites

hey I think there might be a couple small syntax errors in that code and I am not sure how to fix it ;-(
Well i think there was a few (')'s missing i put them in and the code didnt have errors, and the posts load, but there is still no next button.in the first line: $total_pages = $total_count / $per_page;where does that total_count variable come from?
Link to comment
Share on other sites

Well i think there was a few (')'s missing i put them in and the code didnt have errors, and the posts load, but there is still no next button.in the first line: $total_pages = $total_count / $per_page;where does that total_count variable come from?
Sorry if there was some ' missing, my browser doesn't show ' and " good together in the textarea (guess I need to change the settings...).Sorry for the total_count, I tought that we had used it before (maybe another post), but you can use the same code as in the 2nd code-block to set the total_count:
$cnt_res = mysql_query( "SELECT COUNT(*) FROM $tbl_name2 WHERE question_id=$id" );		$total_count = intval(mysql_result( $cnt_res, 0 ));

The first/last, next/prev links should be shown if $total_count has the right value...

Link to comment
Share on other sites

Sorry if there was some ' missing, my browser doesn't show ' and " good together in the textarea (guess I need to change the settings...).Sorry for the total_count, I tought that we had used it before (maybe another post), but you can use the same code as in the 2nd code-block to set the total_count:
$cnt_res = mysql_query( "SELECT COUNT(*) FROM $tbl_name2 WHERE question_id=$id" );		$total_count = intval(mysql_result( $cnt_res, 0 ));

The first/last, next/prev links should be shown if $total_count has the right value...

okay let me try this, thanks again!
Link to comment
Share on other sites


Here I will post the whole code so far, maybe you can spot the problem

<?php// Start sessionsession_start();// Check if the user already is logged in if ((!isset( $_SESSION['loggedin'] )) ||    (!$_SESSION['loggedin'])) {    header( 'Location: login.php' );    exit();}?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD>	<TITLE>MMA and BJJ message forum</TITLE> <META name="abstract" content=""> <META name="description" content=""> <META name="keywords" content=""> <META name="Revisit-After" content="10"> <META HTTP-EQUIV="Content-Language" content="EN"> <META name="distribution" content="global"> <link rel="stylesheet" type="text/css" href="forum.css"> <link rel="stylesheet" type="text/css" href="input.css"> </HEAD><BODY><div id="top"><?php if ((!isset( $_SESSION['loggedin'] )) ||    (!$_SESSION['loggedin'])) { echo "<p class=\"log\">Welcome Guest <a href=\"login.php\">Log In</a>";  } else { echo ("<p class=\"log\">Welcome ". ($_SESSION['user']) . " <a href=\"logout.php\">log out</a></p>");  } ?></div><div id="sitemap"><p><a class="menu" href="index.php" title="VikingBJJ home page">VikingBJJ.com</a> >>> <a class="menu" href="mmabjj.php" title="Back to MMA & BJJ forum">MMA & BJJ Forum</a></p></div><div id="forumbody"><?php$host="localhost"; // Host name $username="root"; // Mysql username $password="********"; // Mysql password $db_name="vikingbjj"; // Database name $tbl_name="mmabjj_question"; // Table name // Connect to server and select databse.mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB");// get value of id that sent from address bar $id=intval($_GET[id]);$page = intval($_GET

);if ($page < 1)  $page = 1;$per_page = 15;$start = (($page - 1) * $per_page);$sql="SELECT * FROM $tbl_name WHERE id=$id";$result=mysql_query($sql);$rows=mysql_fetch_array($result);?><table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFF00"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" "bgcolor="#000055"><tr><td bgcolor="#000055"><p><?php echo $rows['topic']; ?></p></td></tr><tr><td bgcolor="#000055"><p><?php echo wordwrap($rows['detail'], 26, "\n", true); ?></p></td></tr><tr><td bgcolor="#000055"><p>By : <?php echo $rows['name']; ?> Email : <?php echo $rows['email'];?></p></td></tr><tr><td bgcolor="#000055"><p>Date/time : <?php echo $rows['datetime']; ?></td></tr></table></td></tr></table><BR><?php$tbl_name2="mmabjj_answer"; // Switch to table "forum_answer" $sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id' LIMIT {$start}, {$per_page}";$result2=mysql_query($sql2);while($rows=mysql_fetch_array($result2)){?><table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFF00"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFF00"><tr><td bgcolor="#000055" align="right"><p>ID:</p></td><td bgcolor="#000055"><p><?php echo $rows['a_id']; ?></p></td></tr><tr><td width="10%" bgcolor="#000055" align="right"><p>Name:</p></td><td width="90%" bgcolor="#000055"><p><?php echo $rows['a_name']; ?></p></td></tr><tr><td width="10%" bgcolor="#000055" align="right"><p>Email:</p></td><td bgcolor="#000055"><p><?php echo $rows['a_email']; ?></p></td></tr><tr><td width="10%" bgcolor="#000055" align="right"><p>Reply:</p></td><td width="90%" bgcolor="#000055" width="50%"><p><?php echo $rows['a_answer']; ?></p></td></tr><tr><td width="10%" bgcolor="#000055" align="right"><p>Date/Time:</p></td><td width="90%" bgcolor="#000055"><p><?php echo $rows['a_datetime']; ?></p></td></tr></table></td></tr></table><br><?php}$cnt_res = mysql_query( "SELECT COUNT(*) FROM $tbl_name2 WHERE question_id=$id" );        $total_count = intval(mysql_result( $cnt_res, 0 ));$total_pages = $total_count / $per_page;if ($total_pages > 1) {      $tags = array();      if ($page == 1) {            $tags[] = array( '<span class="disabled_page">', '</span>' );            $tags[] = array( '<span class="disabled_page">', '</span>' );      } else {            $tags[] = array( '<a href="viewtopic.php?id=' . $id . '&page=1">', '</a>' );         // First            $tags[] = array( '<a href="viewtopic.php?id=' . $id . '&page=' . ($page - 1) . '">', '</a>' );     // Previous      }      echo $tags[0][0] . '<< First' . $tags[0][1] . '   ';      echo $tags[1][0] . '< Previous' . $tags[1][1] . '   ';}for ($i=1; $i <= $total_pages; $i++) {   if ($i == $page)       echo '<span class="disabled_page">';  // changed the class   else       echo '<a href="viewtopic.php?id=' . $id . '&page=' . $i .'">';   echo $i;   if ($i == $page)       echo '</span>';   else       echo '</a>';   echo '  ';}if ($total_pages > 1) {      $tags = array();      if ($page >= $total_pages) {            $tags[] = array( '<span class="disabled_page">', '</span>' );            $tags[] = array( '<span class="disabled_page">', '</span>' );      } else {            $tags[] = array( '<a href="viewtopic.php?id=' . $id . '&page=' . $total_pages . '">', '</a>' ); // Last            $tags[] = array( '<a href="viewtopic.php?id=' . $id . '&page=' . ($page + 1) . '">', '</a>' ); // Next      }      echo $tags[1][0] . 'Next >' . $tags[1][1] . '   ';      echo $tags[0][0] . 'Last >>' . $tags[0][1] . '   ';}	$sql3="SELECT view FROM $tbl_name WHERE id=$id";$result3=mysql_query($sql3);$rows=mysql_fetch_array($result3);$view=$rows['view'];// if have no counter value set counter = 1if(empty($view)){$view=1;$sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id=$id";$result4=mysql_query($sql4);}// count more value$addview=$view+1;$sql5="update $tbl_name set view='$addview' WHERE id=$id";$result5=mysql_query($sql5);mysql_close();?><table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFF00"><tr><form name="form1" method="post" action="mmabjj_answer.php"><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#000055"><tr><td width="18%" bgcolor="#000055"><p>Name:</td><td width="3%" bgcolor="#000055"> </td><td width="79%" bgcolor="#000055"><p><?php echo ($_SESSION['user']); ?><input name="a_name" type="text" id="a_name" size="45" value="<?php echo ($_SESSION['user']); ?>" style="visibility=hidden;" /></td></tr><tr><td bgcolor="#000055"><p>Email:</td><td bgcolor="#000055"> </td><td><p><?php echo ($_SESSION['email']); ?><input name="a_email" type="text" id="a_email" size="45" value="<?php echo ($_SESSION['email']); ?>" style="visibility : hidden;" /></td></tr><tr><td valign="top" bgcolor="#000055"><p>Reply:</p></td><td valign="top" bgcolor="#000055"> </td><td><textarea class="style" wrap="hard" name="a_answer" cols="80" rows="10" id="a_answer"></textarea></td></tr><tr><td> </td><td bgcolor="#000055"><p><input name="id" type="hidden" value="<?php echo $id; ?>"><input name="page" type="hidden" value="<?php echo $page; ?>"></p></td><td bgcolor="#000055"align="center"><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td></tr></table></td></form></tr></table></div></BODY></HTML>

Link to comment
Share on other sites

but the 'first' 'previous' and '1' are not links, and the next and last sent me to a "page cannot be displayed"
First, Previous and 1 shouldn't be links as you are on the first page (use the CSS-class disabled_page to make this more "visible").What is the file called (I thought it was called viewtopic.php or mmabjj_viewtopic.php) You need to have the right filename in the link (or remove the filename and just use ?id=...), make sure you have it (do you have this site live?)What bothers me is that you can't see the second page, but you can see the first, prev etc. Oh I think I know ht the problem is... (Stupid mistake...)Ok, to fix this add ceil() around $total_pages here (EDIT):
$total_pages = ceil($total_count / $per_page);

Again, sorry for my mistakes, I have alot to think about right now.. :?|ps Hope you look at the code I learn (from my mistakes...) ;?) ds

Link to comment
Share on other sites

First, Previous and 1 shouldn't be links as you are on the first page (use the CSS-class disabled_page to make this more "visible").What is the file called (I thought it was called viewtopic.php or mmabjj_viewtopic.php) You need to have the right filename in the link (or remove the filename and just use ?id=...), make sure you have it (do you have this site live?)What bothers me is that you can't see the second page, but you can see the first, prev etc. Oh I think I know ht the problem is... (Stupid mistake...)Ok, to fix this add ceil() around $total_pages in the for-loop:
...for ($i=1; $i <= ceil($total_pages); $i++) {	if ($i == $page)...

Again, sorry for my mistakes, I have alot to think about right now.. :?|ps Hope you look at the code I learn (from my mistakes...) ;?) ds

yeah I realized that about the first and previous on the first page ahah, I am a bit tired right now!the only problem is the LAST buttonit is sending me to pages like thishttp://localhost/vikingbjj/mmabjj_viewtopi...23&page=2.2instead of page=3
Link to comment
Share on other sites

Ok, to fix this add ceil() around $total_pages here (EDIT):
$total_pages = ceil($total_count / $per_page);

Again, sorry for my mistakes, I have alot to think about right now.. :?|

It's that when we get total_pages we divide total posts with how many posts you want on a page which give you a float, and I (as stupid as I am right now, apparentely) forget about that and happily taught that you get a nice integer... :?|
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...