Before reading this post , To understand this post it's better to read:
http://webprogram4beginners.blogspot.com/2011/04/how-to-show-posts-inside-of-forum.html
http://webprogram4beginners.blogspot.com/2011/04/how-to-add-newtopic-ability-to-forum.html
We want to direct user to the post that he clicks on its title. First of all , we make a hyperlink to "showtopic.php" (the page displays the requested post).
The hyperlink have the following format :
code:
echo "<a href=\"showtopic.php?Textlink=" .$Textlink. " \">" . $TextTitle . "</a>";
The hyperlink that is shown by this code is look like:
Now , we should add codes for "showtopic.php" page . This page will show the requested post by the help of "Textlink" variable. In fact this variable is the identity of the post.
The followings are the "showtopic.php" page codes.
code:
<?php
include "auth.inc"; // we check that if user has loged in or not
include "funcs.php"; // the functions look like : connect (to connect to the database) and creatcode (for verification text) can be put in a file and then we include that file wherever it is required.
?>
<a href="logout.php">log out</a>
<br>
// the following html codes show a menu-bar on the page . they have been created by Frontpage . You can omit them.
<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title> show topic </title>
</head>
<body bgcolor="#99FF66" >
<table border="0" cellpadding="0" cellspacing="0" width="1055" height="625">
<!-- MSTableType="layout" -->
<tr>
<td valign="top" colspan="3" height="148">
<!-- MSCellType="ContentHead" -->
<p align="center">
<br>
<!--webbot bot="Navigation" S-Type="sequence" S-Orientation="horizontal" S-Rendering="graphics" S-Theme="poetic 0000" B-Include-Home="FALSE" B-Include-Up="FALSE" U-Page="sid:1001" --><a href="index.php"><img src="_derived/index.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="index.php" align="middle"></a> <a href="about.php"><img src="_derived/about.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="about.php" align="middle"></a> <a href="search.php"><img src="_derived/search.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="search.php" align="middle"></a><!--webbot bot="Navigation" i-checksum="38819" endspan --><br>
</p>
<p><font size="6"><b> show topic </b></font></td>
</tr>
<tr>
<td valign="top" width="300">
<!-- MSCellType="NavBody" -->
// the following is a hyperlink to reply a post , As you can see obviously , the variable "Textlink" is sent to reply page.
<a href="reply.php?Textlink= <?php echo $_GET["Textlink"]; ?> ">Reply this Topic</a>
<br>
<br>
// the following is just a list of forums
<font size="3"><b> main titles </b></font>
<?php
echo"
<br>
<br>
1.<a href=\"showforum.php?link=satire\">satire</a>
<br>
<br>
2.<a href=\"showforum.php?link=drama\">drama</a>
<br>
<br>
3.<a href=\"showforum.php?link=tragic\">tragic</a>
<br>
<br>
4.<a href=\"showforum.php?link=joke\">joke</a>
<br>
<br>
5.<a href=\"showforum.php?link=others\">others</a>
";
?>
</td>
<td valign="top" width="747">
<!-- MSCellType="ContentBody" -->
<p align="justify"><b><font size="4">
<br>
<br>
<br>
<?php
// First we extraxt the post link (Textlink)
$topiclink = $_GET["Textlink"];
// some times the browser may add some space characters (or %20) to the adress bar (I don't know why , but it happens when user refreshes the page) , to avoid it we do "trim" and "str_place" editions on the variable.
$topiclink = trim($topiclink);
$topiclink = str_replace("%20", " ", $topiclink);
connect();
// extract the post with the link of Textlink from the database , there's just one post with this link
$sql = "SELECT * FROM topics WHERE link='$topiclink' ";
$SearchResult = mysql_query($sql);
// put the results in some variables to use , these variables totally depends on the database that you have created , you can have less variables than here . most of the variables here , doesn't have any use . hahahahaha
$Textauthor = mysql_result($SearchResult, $i, 0); // the post author
$Textfield = mysql_result($SearchResult, $i, 1); // the post forum field
$Textdate = mysql_result($SearchResult, $i, 2); // the post date
$TextTitle = mysql_result($SearchResult, $i, 3); // the post title
$Textfoldername = mysql_result($SearchResult, $i, 4);
$Textlink = mysql_result($SearchResult, $i, 5);
$Textnumreply = mysql_result($SearchResult, $i, 6);
$number_of_view = mysql_result($SearchResult, $i, 7);
$number_of_report_abuse = mysql_result($SearchResult, $i, 8);
$data = mysql_result($SearchResult, $i, 9); // this is the post body
// the number of times that a post is viewed by the users , is a good parameter to hold an account for popular authors (most viewed) , so we use "$number_of_view" to hold this.
$number_of_view = $number_of_view +1;
// each time we update the "$number_of_view" variable on the database.
$sql = "UPDATE topics SET view = '$number_of_view' WHERE link='$topiclink' ";
mysql_query($sql);
// the following lines are showing the post on the screen , you can show the results in a beautiful format and inside of a table.
echo "Text Title = $TextTitle";
echo "<br> <br>";
echo "Text author = $Textauthor";
echo "<br> <br>";
echo "Text field = $Textfield";
echo "<br> <br>";
echo "Text date = $Textdate";
echo "<br> <br>";
echo "this story has been viewed " .$number_of_view. " time";
echo "<br> <br>";
print ("<PRE>" . $data . "</PRE> <br> <br>");
?>
<?php
/////////////////////////////REPLIES SHOWS HERE/////////////////////////////////////////////
// Now we extract the replies related to this post from the database.
$sql = "SELECT * FROM reply WHERE link='$topiclink' ";
$SearchResult = mysql_query($sql);
$TotalResults = mysql_num_rows($SearchResult);
if($TotalResults != 0){
// look at the "for loop" parameters , I used a combination of variables in a way that the replies are shown by the date order ( the last reply is shown just under the post body)
for($i = $TotalResults - 1; $i != -1; $i-- )
{
$reply_author = mysql_result($SearchResult, $i, 0);
$reply_text = mysql_result($SearchResult, $i, 2);
$reply_date = mysql_result($SearchResult, $i, 3);
echo "<PRE>";
echo "reply_author = " . $reply_author . "<br>";
echo "reply_date = " . $reply_date . "<br>";
echo "</PRE>";
echo "<br> <br>";
print ("<PRE>" . $reply_text . "</PRE> <br> <br>");
}
}
//////////////////////////////////////////////////////////////////////////////////////////
?>
</td>
<td valign="top" height="427" width="147">
<!-- MSCellType="NavBody2" -->
</td>
</tr>
<tr>
<td valign="top" colspan="3" height="50">
<!-- MSCellType="ContentFoot" -->
</td>
</tr>
</p>
</table>
</body>
</html>
<p>
<br>
</p>
<html dir="ltr">
Enough comments has been added to the codes so there's no more need to explain.
If you don't want to go over all of my weblog you may need to read the following posts to understand the current post :
http://webprogram4beginners.blogspot.com/2011/02/download-php-codes-of-complete-forum.html
http://webprogram4beginners.blogspot.com/2011/02/how-to-add-variable-to-url-adress.html
http://webprogram4beginners.blogspot.com/2011/02/php-orders-to-work-with-database-and.html
This is a blog contain php , HTML and mysql codes to make a complete professional web site. I explain the codes in a very simple language for people to help them improve their abilities. On the other hand I LOVE PHP.
Showing posts with label How to show posts inside of a forum. Show all posts
Showing posts with label How to show posts inside of a forum. Show all posts
How to show posts inside of a forum
Assume that our forum has five fields : (for example visit : http://story-world.99k.org/)
1.Satire Stories
2.Drama Stories
3.Tragic Stories
4.Jokes
5.other not listed catagories
We want to direct our user to the posts under the clicked field. We can do it just by one common page , but we send a variable to the common page to inform it which field the user requested.
To know how to add variables to a url address see :
http://webprogram4beginners.blogspot.com/2011/02/how-to-add-variable-to-url-adress.html
Put these hyperlinks (for above fields) , in your index page :
code:
<br>
1.<a href="showforum.php?link=satire">Satire Stories</a>
<br>
<br>
2.<a href="showforum.php?link=drama">Drama Stories</a>
<br>
<br>
3.<a href="showforum.php?link=tragic">Tragic Stories</a>
<br>
<br>
4.<a href="showforum.php?link=joke">Jokes</a>
<br>
<br>
5.<a href="showforum.php?link=others">other not listed catagories</a>
Here we add the variable "link" to our url address. The user is directed to the common page "showforum.php" and there , base on the variable "link" the appropriate posts are shown.
Now , build a new php file , rename it to "showforum.php" and put the following codes in it.
code:
<?php
include "auth.inc"; // we check that if user has loged in or not
include "funcs.php"; // the functions look like : connect (to connect to the database) and creatcode (for verification text) can be put in a file and then we include that file wherever it is required. Here we want to summon the function "get_pages_html". This function produces page number links. If we have 40 posts under a field just 10 of them will be shown on each page and to visit the next 10 posts , the user should go to page 2. To know about this function go to :
http://webprogram4beginners.blogspot.com/2011/04/how-to-show-page-number-for-forum.html
?>
<?php
$forumlink = $_SERVER["REQUEST_URI"]; // this is not needed hahaha
// first extract the variable "link" , and put it in a local variabe.
$forumlink = $_GET["link"]; // the field of the forum that is sent from the index page
?>
<a href="logout.php">log out</a>
<br>
// the following html codes show a menu-bar on the page . they have been created by Frontpage . You can omit them.
<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title> <?php echo $forumlink; ?> </title>
</head>
<body bgcolor="#99FF66" >
<table border="0" cellpadding="0" cellspacing="0" width="1055" height="625">
<!-- MSTableType="layout" -->
<tr>
<td valign="top" colspan="3" height="148">
<!-- MSCellType="ContentHead" -->
<p align="center">
<br>
<!--webbot bot="Navigation" S-Type="sequence" S-Orientation="horizontal" S-Rendering="graphics" S-Theme="poetic 0000" B-Include-Home="FALSE" B-Include-Up="FALSE" U-Page="sid:1001" --><a href="index.php"><img src="_derived/index.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="index.php" align="middle"></a> <a href="about.php"><img src="_derived/about.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="about.php" align="middle"></a> <a href="search.php"><img src="_derived/search.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="search.php" align="middle"></a><!--webbot bot="Navigation" i-checksum="38819" endspan --><br>
</p>
<p><font size="6"><b> <?php echo $forumlink; ?> </b></font></td>
</tr>
<tr>
<td valign="top" width="300">
<!-- MSCellType="NavBody" -->
// the codes here shows a button to create a new post on the forum it is described under "how to add newtopic ability to a forum" post.
<FORM ACTION="newtopic.php" METHOD=POST>
<INPUT TYPE="hidden" NAME="originforum" VALUE= "<?php echo $forumlink ?>" >
the button redirects you to the form text box
<BR>
<INPUT TYPE="SUBMIT" NAME="BUTTON_new_topic" VALUE="add_new_story">
</FORM>
<br>
<br>
<br>
<font size="3"><b> main titles </b></font>
<?php
echo"
<br>
// the following is a menu of the fields in our website.
<br>
1.<a href=\"showforum.php?link=satire\">satire</a>
<br>
<br>
2.<a href=\"showforum.php?link=drama\">drama</a>
<br>
<br>
3.<a href=\"showforum.php?link=tragic\">tragic</a>
<br>
<br>
4.<a href=\"showforum.php?link=joke\">joke</a>
<br>
<br>
5.<a href=\"showforum.php?link=others\">others</a>
";
?>
<br>
</td>
<td valign="top" width="747">
<!-- MSCellType="ContentBody" -->
<p align="justify"><b><font size="4">
<br>
<br>
<br>
<?php
connect();
$sql = "SELECT * FROM topics WHERE field='$forumlink' "; // extract all of the posts in the forum field
$SearchResult = mysql_query($sql);
$TotalResults = mysql_num_rows($SearchResult);
$num_show_post = 20; //select an even number for the number of posts are showed in a page
if($TotalResults%2 == 0){
// based on the number of results we calculate the number of pages that is needed to show the results
$page_num = intval($TotalResults/$num_show_post); // "intval" returns the integer value of its variable
}else{
$page_num = intval($TotalResults/$num_show_post)+1;
}
$p = $_GET["p"]; // the current page number
$page_num = $GLOBALS['page_num']; // this line is not needed , you can omit it , it changes "$page_num" into a global variable.
if ($p == "")
$p = 1;
$counter = $TotalResults - (($p-1)*$num_show_post); // based on the page number and number of posts to show on a page , we calculate this counter
for($i = $counter - 1; $i != $counter - $num_show_post -1; $i-- )
// by the calculated counter , show the appropriate posts on a page
{
$TextTitle = mysql_result($SearchResult, $i, 3);
$Textauthor = mysql_result($SearchResult, $i, 0);
$Textlink = mysql_result($SearchResult, $i, 5);
echo "<a href=\"showtopic.php?Textlink=" .$Textlink. " \">" . $TextTitle . "</a>"; // make a hyper link to the post that is clicked
echo "<br> <br>";
}
?>
</td>
<td valign="top" height="427" width="147">
<!-- MSCellType="NavBody2" -->
</td>
</tr>
<tr>
<td valign="top" colspan="3" height="50">
<!-- MSCellType="ContentFoot" -->
<br>
<br>
<p align="center">
<?php
echo get_pages_html(); // the page number function "get_pages_html" is summoned here.
?>
</td>
</tr>
</p>
</table>
</body>
</html>
<p>
<br>
</p>
<html dir="ltr">
Enough comments has been added to the codes so there's no more need to explain.
The resulting forum page is look like :
If you don't want to go over all of my weblog you may need to read the following posts to understand the current post :
http://webprogram4beginners.blogspot.com/2011/04/how-to-show-page-number-for-forum.html
http://webprogram4beginners.blogspot.com/2011/02/download-php-codes-of-complete-forum.html
http://webprogram4beginners.blogspot.com/2011/02/how-to-add-variable-to-url-adress.html
http://webprogram4beginners.blogspot.com/2011/02/php-orders-to-work-with-database-and.html
1.Satire Stories
2.Drama Stories
3.Tragic Stories
4.Jokes
5.other not listed catagories
We want to direct our user to the posts under the clicked field. We can do it just by one common page , but we send a variable to the common page to inform it which field the user requested.
To know how to add variables to a url address see :
http://webprogram4beginners.blogspot.com/2011/02/how-to-add-variable-to-url-adress.html
Put these hyperlinks (for above fields) , in your index page :
code:
<br>
1.<a href="showforum.php?link=satire">Satire Stories</a>
<br>
<br>
2.<a href="showforum.php?link=drama">Drama Stories</a>
<br>
<br>
3.<a href="showforum.php?link=tragic">Tragic Stories</a>
<br>
<br>
4.<a href="showforum.php?link=joke">Jokes</a>
<br>
<br>
5.<a href="showforum.php?link=others">other not listed catagories</a>
Here we add the variable "link" to our url address. The user is directed to the common page "showforum.php" and there , base on the variable "link" the appropriate posts are shown.
Now , build a new php file , rename it to "showforum.php" and put the following codes in it.
code:
<?php
include "auth.inc"; // we check that if user has loged in or not
include "funcs.php"; // the functions look like : connect (to connect to the database) and creatcode (for verification text) can be put in a file and then we include that file wherever it is required. Here we want to summon the function "get_pages_html". This function produces page number links. If we have 40 posts under a field just 10 of them will be shown on each page and to visit the next 10 posts , the user should go to page 2. To know about this function go to :
http://webprogram4beginners.blogspot.com/2011/04/how-to-show-page-number-for-forum.html
?>
<?php
$forumlink = $_SERVER["REQUEST_URI"]; // this is not needed hahaha
// first extract the variable "link" , and put it in a local variabe.
$forumlink = $_GET["link"]; // the field of the forum that is sent from the index page
?>
<a href="logout.php">log out</a>
<br>
// the following html codes show a menu-bar on the page . they have been created by Frontpage . You can omit them.
<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title> <?php echo $forumlink; ?> </title>
</head>
<body bgcolor="#99FF66" >
<table border="0" cellpadding="0" cellspacing="0" width="1055" height="625">
<!-- MSTableType="layout" -->
<tr>
<td valign="top" colspan="3" height="148">
<!-- MSCellType="ContentHead" -->
<p align="center">
<br>
<!--webbot bot="Navigation" S-Type="sequence" S-Orientation="horizontal" S-Rendering="graphics" S-Theme="poetic 0000" B-Include-Home="FALSE" B-Include-Up="FALSE" U-Page="sid:1001" --><a href="index.php"><img src="_derived/index.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="index.php" align="middle"></a> <a href="about.php"><img src="_derived/about.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="about.php" align="middle"></a> <a href="search.php"><img src="_derived/search.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="search.php" align="middle"></a><!--webbot bot="Navigation" i-checksum="38819" endspan --><br>
</p>
<p><font size="6"><b> <?php echo $forumlink; ?> </b></font></td>
</tr>
<tr>
<td valign="top" width="300">
<!-- MSCellType="NavBody" -->
// the codes here shows a button to create a new post on the forum it is described under "how to add newtopic ability to a forum" post.
<FORM ACTION="newtopic.php" METHOD=POST>
<INPUT TYPE="hidden" NAME="originforum" VALUE= "<?php echo $forumlink ?>" >
the button redirects you to the form text box
<BR>
<INPUT TYPE="SUBMIT" NAME="BUTTON_new_topic" VALUE="add_new_story">
</FORM>
<br>
<br>
<br>
<font size="3"><b> main titles </b></font>
<?php
echo"
<br>
// the following is a menu of the fields in our website.
<br>
1.<a href=\"showforum.php?link=satire\">satire</a>
<br>
<br>
2.<a href=\"showforum.php?link=drama\">drama</a>
<br>
<br>
3.<a href=\"showforum.php?link=tragic\">tragic</a>
<br>
<br>
4.<a href=\"showforum.php?link=joke\">joke</a>
<br>
<br>
5.<a href=\"showforum.php?link=others\">others</a>
";
?>
<br>
</td>
<td valign="top" width="747">
<!-- MSCellType="ContentBody" -->
<p align="justify"><b><font size="4">
<br>
<br>
<br>
<?php
connect();
$sql = "SELECT * FROM topics WHERE field='$forumlink' "; // extract all of the posts in the forum field
$SearchResult = mysql_query($sql);
$TotalResults = mysql_num_rows($SearchResult);
$num_show_post = 20; //select an even number for the number of posts are showed in a page
if($TotalResults%2 == 0){
// based on the number of results we calculate the number of pages that is needed to show the results
$page_num = intval($TotalResults/$num_show_post); // "intval" returns the integer value of its variable
}else{
$page_num = intval($TotalResults/$num_show_post)+1;
}
$p = $_GET["p"]; // the current page number
$page_num = $GLOBALS['page_num']; // this line is not needed , you can omit it , it changes "$page_num" into a global variable.
if ($p == "")
$p = 1;
$counter = $TotalResults - (($p-1)*$num_show_post); // based on the page number and number of posts to show on a page , we calculate this counter
for($i = $counter - 1; $i != $counter - $num_show_post -1; $i-- )
// by the calculated counter , show the appropriate posts on a page
{
$TextTitle = mysql_result($SearchResult, $i, 3);
$Textauthor = mysql_result($SearchResult, $i, 0);
$Textlink = mysql_result($SearchResult, $i, 5);
echo "<a href=\"showtopic.php?Textlink=" .$Textlink. " \">" . $TextTitle . "</a>"; // make a hyper link to the post that is clicked
echo "<br> <br>";
}
?>
</td>
<td valign="top" height="427" width="147">
<!-- MSCellType="NavBody2" -->
</td>
</tr>
<tr>
<td valign="top" colspan="3" height="50">
<!-- MSCellType="ContentFoot" -->
<br>
<br>
<p align="center">
<?php
echo get_pages_html(); // the page number function "get_pages_html" is summoned here.
?>
</td>
</tr>
</p>
</table>
</body>
</html>
<p>
<br>
</p>
<html dir="ltr">
Enough comments has been added to the codes so there's no more need to explain.
The resulting forum page is look like :
If you don't want to go over all of my weblog you may need to read the following posts to understand the current post :
http://webprogram4beginners.blogspot.com/2011/04/how-to-show-page-number-for-forum.html
http://webprogram4beginners.blogspot.com/2011/02/download-php-codes-of-complete-forum.html
http://webprogram4beginners.blogspot.com/2011/02/how-to-add-variable-to-url-adress.html
http://webprogram4beginners.blogspot.com/2011/02/php-orders-to-work-with-database-and.html
Subscribe to:
Posts (Atom)

