Page 1 of 1

People, help a novice in building his simple php forum

Posted: Fri Aug 12, 2005 1:52 am
by Bekzod
:( Please if possible, without any checking for the forms,
simply the most important php code of the forum, with explnations please, as I am buildin my own web site, i would like to add my own forum as well for my uzecoforum.com

Posted: Fri Aug 12, 2005 3:20 am
by s.dot
Basing my opinion on that post, I'd say you're nowhere near ready to build a PHP forum yourself. Luckily, there are pre-made forums for you that you can configure. Check out PHPBB. Then while that's up, read some php tutorials! I heard spoono.com has some good ones. I learned from http://www.scriptschool.com/php and trial & error.

Not too far though

Posted: Sat Aug 13, 2005 3:57 am
by Bekzod
8) I have done my forum, but would like some if possible simpler one.
I have add topic.php:

Code: Select all

<?php
 //check for required fields from the form
 if ((!$topic_owner) || ($topic_title)
    || (!$post_text)) {
   echo "PLEASE INSERT SOMETHING!";
 }

 //connect to server and select database
$conn = mysql_connect("192.168.1.77", "bekzod", "123");
  
 mysql_select_db("forum",$conn) or die(mysql_error());

 //create and issue the first query
 $add_topic = "insert into forum_topics values ('', '$topic_title',
     now(), '$topic_owner')";
 mysql_query($add_topic,$conn) or die(mysql_error());

 //get the id of the last query
 $topic_id = mysql_insert_id();

 //create and issue the second query
 $add_post = "insert into forum_posts values ('', '$topic_id',
    '$post_text', now(), '$topic_owner')";
 mysql_query($add_post,$conn) or die(mysql_error());

 //create nice message for user
 $display_block = "<P>The <strong>$topic_title</strong> topic has been created.</p>";
 ?>
 <h1>New Topic Added</h1>
 <?php echo $display_block; ?>
 </body>
 </html>


//show topic list
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?
	$conn = mysql_connect("192.168.1.77", "bekzod", "123") or die(mysql_error());
 mysql_select_db("forumxp",$conn) or die(mysql_error());
//gather the topics
$get_topic="select topic_id, topic_title,
 date_format(topic_create_time, '%b %e %Y at %r') as fmt_topic_create_time,
 topic_owner from forum_topics order by topic_create_time desc";
$get_topic_res=mysql_query($get_topic, $conn);
if(mysql_num_rows($get_topic_res)<1)
	{
		$display="<P>No Topic Exists</P>";
	}
else
	{
		$display="<table cellpadding=3 cellspacing=1 border=1 ><tr><td>Author</td><td>Topic Title</td><td># of Posts</td></tr>";
		while($row=mysql_fetch_array($get_topic_res))
			{
				$topic_id=$row['topic_id'];
				$topic_title=stripslashes($row['topic_title']);
				$topic_create_time=$row['fmt_topic_create_time'];
				$topic_owner=stripslashes($row['topic_owner']);
				//get the number of posts for each topic
				//get number of posts
     			$get_num_posts="select count(post_id) from forum_posts where topic_id=$topic_id";
       			$get_num_posts_res = mysql_query($get_num_posts,$conn) or die(mysql_error());
		        $num_posts = mysql_result($get_num_posts_res,0,'count(post_id)');

				$display.="<tr><td>$topic_owner<br>created on $topic_create_time</td><td><a href=\"showtopicecology.php?topic_id=$topic_id\">$topic_title</a></td>
				<td>$num_posts</td></tr>";
			}
			$display.="</table>";
	}

?>

<html>
<head>
<title>Ecology</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p align="center"><? echo "$display";?></p>
</body>
</html>
// show topic
<?php
 //check for required info from the query string
if (!$_GET[topic_id]) {
    $display_block="<P>Choose a topic!</P>";
 }

 //connect to server and select database
	$conn = mysql_connect("192.168.1.77", "bekzod", "123") or die(mysql_error());
 mysql_select_db("forumxp",$conn) or die(mysql_error());


 //verify the topic exists
 $verify_topic = "select topic_title from forum_topics where
     topic_id = $_GET[topic_id]";
 $verify_topic_res = mysql_query($verify_topic, $conn)
     or die(mysql_error());

 if (mysql_num_rows($verify_topic_res) < 1) {
     //this topic does not exist
    $display_block = "<P><em>You have selected an invalid topic.
    Please <a href=\"topiclist1.php\">try again</a>.</em></p>";
 } else {
    //get the topic title
    $topic_title = stripslashes(mysql_result($verify_topic_res,0,
           'topic_title'));

    //gather the posts
     $get_posts = "select post_id, post_text, date_format(post_create_time,
          '%b %e %Y at %r') as fmt_post_create_time, post_owner from
           forum_posts where topic_id = $_GET[topic_id]
           order by post_create_time asc";

     $get_posts_res = mysql_query($get_posts,$conn) or die(mysql_error());

    //create the display string
    $display_block = "
   <P>Showing posts for the <strong>$topic_title</strong> topic:</p>

    <table width=100% cellpadding=3 cellspacing=1 border=1>
   <tr>
    <th>AUTHOR</th>
     <th>POST</th>
     </tr>";

     while ($posts_info = mysql_fetch_array($get_posts_res)) {
         $post_id = $posts_info['post_id'];
         $post_text = nl2br(stripslashes($posts_info['post_text']));
         $post_create_time = $posts_info['fmt_post_create_time'];
         $post_owner = stripslashes($posts_info['post_owner']);

         //add to display
         $display_block .= "
         <tr>
         <td width=35% valign=top>$post_owner<br>[$post_create_time]</td>
         <td width=65% valign=top>$post_text<br><br>
         <a href=\"replytopost1.php?post_id=$post_id\"><strong>REPLY TO
         POST</strong></a></td>
         </tr>";
     }

     //close up the table
     $display_block .= "</table>";
 }
 ?>
 <html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<link rel="stylesheet" type="text/css" href="link.css">
<body topmargin="0" leftmargin="0" style="font-family: Courier" bgcolor="#547edf">
<img src="back.gif" width="100%" >

<?php 
include 'navigation.inc.txt'; 
?> 

<table border="0" width="100%" height="5" style="margin-top:0;" cellpadding="2" cellspacing="0" >
 <tr>
   <td colspan="5" align="center" valign="top" background="line.gif"></td>
  </tr>
 <tr>
   <td width="20%" valign="top" align="center" bgcolor="#547edf">What are you up to now? </td>
	<td background="line.gif" width="1%">
	</td>

	<td width="58%" valign="top" align="center" bgcolor="#547edf"><P>THE NEWS(INDEX)</p>
		<?php echo "$display_block"; ?>

      <!----------------------------------------THE TEXT FOR THE ABOUT MESSAGE BOX-------------------------------------->
</td>
	<td width="1%"  background="line.gif">
		<!-----------------------------------------END OF THE ABOUT MESSAGE BOX------------------------------------------->
	
	</td>
	<td width="20%" align="center" valign="top" bgcolor="#547edf"><!--------------------------SUBSCRIBE BOX----------------------------------------------->
	<?php include 'sub1.php'; ?>
	
	THE OTHER RELEVANT PAGES!       <!------------------------END OF SUBSCRIBE BOX------------------------------------------->
	<?php include 'sponsor.txt'; ?>
	</td>
	

	
		
		
</tr>
</table>
<?php include 'map.txt'; ?>




</body>
</html>
//reply to post 
<?php
 //connect to server and select database; we'll need it soon
$conn = mysql_connect("192.168.1.77", "bekzod", "123")  or die(mysql_error());
 mysql_select_db("forum",$conn) or die(mysql_error());

 //check to see if we're showing the form or adding the post
 if ($add!= "addpost") {
    // showing the form; check for required item in query string
    if (!$post_id) {
         header("Location: topiclist.php");
         exit;
    }

       //still have to verify topic and post
    $verify = "select ft.topic_id, ft.topic_title from
     forum_posts as fp left join forum_topics as ft on
     fp.topic_id = ft.topic_id where fp.post_id = $post_id";

    $verify_res = mysql_query($verify, $conn) or die(mysql_error());
     if (mysql_num_rows($verify_res) < 1) {
        //this post or topic does not exist
        header("Location: topiclist.php");
        exit;
    } else {
        //get the topic id and title
        $topic_id = mysql_result($verify_res,0,'topic_id');
        $topic_title = stripslashes(mysql_result($verify_res,
         0,'topic_title'));

       echo "
       <html>
       <head>
        <title>Post Your Reply in $topic_title</title>
        </head>
        <body>
       <h1>Post Your Reply in $topic_title</h1>
      <form method=post action=\"$_SERVER[PHP_SELF]\">

        <p><strong>Your E-Mail Address:</strong><br>
        <input type=\"text\" name=\"post_owner\" size=40 maxlength=150>

        <P><strong>Post Text:</strong><br>
        <textarea name=\"post_text\" rows=8 cols=40 wrap=virtual></textarea>

        <input type=\"hidden\" name=\"add\" value=\"addpost\">
        <input type=\"hidden\" name=\"topic_id\" value=\"$topic_id\">
        <P><input type=\"submit\" name=\"submit\" value=\"Add Post\"></p>
        </form>
        </body>
      </html>";
    }
} else if ($add == "addpost") {
   //check for required items from form
   if ( (!$topic_id) || (!$post_text) ||
   (!$post_owner)) {
      header("Location: topiclist.php");
       exit;
    }

    //add the post
    $add_post = "insert into forum_posts values ('', '$topic_id',
     '$post_text', now(), '$post_owner')";
    mysql_query($add_post,$conn) or die(mysql_error());

    //redirect user to topic
    header("Location: showtopic.php?topic_id=$topic_id");
    exit;
 }
 ?>
///////


But still would like some new idea easier way to accomplish, people say objects are the best way to get the work done as easily as possible, but I don't know how to make it with objects.
:( Any advice?



feyd | Please post large blocks of php code in

Code: Select all

tags.[/color]

Posted: Sat Aug 13, 2005 8:50 am
by neophyte
Firstly put your code in

Code: Select all

tags

Code: Select all

so we can read it.

Yeah, don't worry about objects yet. Learn the basics first. There are plenty of good PHP apps that don't use PHP objects at all.