The list of all PHP and Mysql posts

How to show a certain post by user demand

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">
&nbsp;
<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>
&nbsp;</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>
";
?>
  &nbsp;</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" -->

  &nbsp;</td>
 </tr>
 <tr>
  <td valign="top" colspan="3" height="50">
  <!-- MSCellType="ContentFoot" -->

  &nbsp;</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

No comments:

Post a Comment