Php ile Forum Yazmak | Kendi Forumunuzu Kodlayın

12.07.2009 15:20 — Programlama, Webmaster,

Bu yazıda php kodlama diliyle bir forum nasıl kodlanır onu anlatmaya çalışacağım. Adım adım uygularsanız iÅŸlem sonunda basit bir forum scripti elde etmiÅŸ olacaksınız. Bu forumu geliÅŸtirmek size kalmış. Bu bir baÅŸlangıçtır ve mantığını kavrama amaçlı verilmiÅŸtir. GeliÅŸmiÅŸ özellikler eklemek sizin kodlama bilginize kalmıştır. Hadi baÅŸlayalım canlarım... ilk Önce Tablolarimizi Olusturuyoruz; Katagori Tablolari

Kod:
create table categories(
  id int(4) not null auto_increment,
  title varchar(255) not null,
  description varchar(255) not null,
  primary key(id)
);

Forum Tablolari

Kod:
create table forums(
  id int(4) not null auto_increment,
  cid int(4) not null,
  title varchar(255) not null,
  description longtext not null,
  last_post_title varchar(255) not null,
  last_post_username varchar(32) not null,
  topics int(9) not null,
  replies int(9) not null,
  primary key(id)
);

Gönderilen Basliklar Tablolari

Kod:
create table topics(
  id int(9) not null auto_increment,
  timestamp int(20) not null,
  fid int(4) not null,
  title varchar(255) not null,
  post longtext not null,
  username varchar(32) not null,
  last_post_username varchar(32) not null,
  replies int(9) not null,
  views int(9) not null,
  primary key(id)
);

Gönderilen Mesajlar Tablolari

Kod:
create table replies(
  id int(9) not null auto_increment,
  tid int(9) not null,
  post longtext not null,
  username varchar(32) not null,
  primary key(id)
);

Tablolarimizi Olusturduk Simdi Baglanti dosyalari,index,katagori ekleme,forum ekleme,forum görüntüle,mesaj ekleme,mesaj görüntüle gibi dosyalari olusturacagiz database.php [ Baglanti ]

Kod:
<?
mysql_connect("localhost", "db ismi", "db sifresi");
// alt kisimada Olusturdugumuz databese ismini yaziyoruz.
mysql_select_db("database");
?>

index.php [ Ana Sayfa ]

Kod:
<?
// Baglantiyi include ediyoruz.
include ("database.php"); 

// Bu kisimda Baglantigi kurdukdan Sonra tablomuza Baglanip kategori tablomuza baglaniyoruz
$result = mysql_query("select * from categories order by id asc") or die(mysql_error()); 

   while($r = mysql_fetch_array($result))
   {
      // redefine our category row variables.
      $category_id = $r['id'];
      $category_title = $r['title'];
      $category_description = $r['description'];
?>
<table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;">
  <tr>
    <td colspan="4" style="background-color:#eee;"><? echo $category_title; ?><br />
    <? echo $category_description; ?></td>
  </tr>
<? 

     $result2 = mysql_query("select * from forums where cid = '$category_id'"); 

     while($r2 = mysql_fetch_array($result2))
     { 

        $forum_id = $r2['id'];
        $forum_title = $r2['title'];
        $forum_description = $r2['description'];
        $forum_last_post_title = $r2['last_post_title'];
        $forum_last_post_username = $r2['last_post_username'];
        $forum_topics = $r2['topics'];
        $forum_replies = $r2['replies'];
?>
  <tr>
    <td style="width:50%;background-color:#fafafa;">
    <a href="viewforum.php?f=<? echo $forum_id; ?>"><? echo $forum_title; ?></a><br />
    <? echo $forum_description; ?>
    </td>
    <td style="width:30%;background-color:#fafafa;">
    <? echo $forum_last_post_title; ?><br />
    <? echo $forum_last_post_username; ?>
    </td>
    <td style="width:10%;background-color:#fafafa;"><? echo $forum_topics; ?></td>
    <td style="width:10%;background-color:#fafafa;"><? echo $forum_replies; ?></td>
  </tr>
<?
      }
?>
  <tr>
    <td style="background-color:#eee;font-size:0;height:15px;" colspan="4">&nbsp;</td>
  </tr>
</table>
<?
}
?>

add_category.php [ Kategori Ekle ]

Kod:
<?
// Baglanti include Ediyoruz.
include ("database.php"); 

if(isset($_POST['addcategory']) && !empty($_POST['title']))
{ 

   $title = $_POST['title'];
   $description = $_POST['description']; 

   mysql_query("insert into categories values('null', '$title', '$description')");
   echo "<b> The Category Was added Successfully.</b><br /> \n";
}
?>
<h1>Kategori Ekle</h1>
<form action="add_category.php" method="post">
Kategori Basligi:<br />
<input type="text" name="title" /><br />
Aciklamasi:<br />
<input type="text" name="description" /><br />
<input type="submit" value="Add Category" name="addcategory" />
</form>

add_forum.php [ Forum Ekle ]

Kod:
<?
// Her Zamanki gibi baglanti include ediyoruz.
include ("database.php"); 

if(isset($_POST['addforum']) && !empty($_POST['title']))
{ 

   $title = $_POST['title'];
   $description = $_POST['description'];
   $cid = $_POST['cid']; 

   mysql_query("insert into forums (id, cid, title, description)
      values('null', '$cid', '$title', '$description')");
   echo "<b> The forum was added successfully.</b><br /> \n";
}
?>
<h1>Forum Ekle</h1>
<? 

$result = mysql_query("select id, title from categories order by title asc"); 

if(mysql_num_rows($result) < 1)
{
   echo "You need to add categories first. \n";
}
// else, display the form.
else
{
?>
<form action="add_forum.php" method="post">
Add to which category?<br />
<select name="cid">
<? 

   while($r = mysql_fetch_array($result))
   {
      echo "<option value=\"". $r['id'] ."\">". $r['title'] ."</option> \n";
   }
?>
</select><br />
FORUM Baslik:<br />
<input type="text" name="title" /><br />
FORUM Aciklama:<br />
<input type="text" name="description" /><br />
<input type="submit" value="Add Category" name="addforum" />
</form>
<?
}
?>

viewforum.php [ Forum Görüntüle ]

Kod:
<?
// Baglanti include.
include ("database.php"); 

$f = $_GET['f']; 

$result = mysql_query("select title from forums where id = '$f'"); 

$result2 = mysql_query("select * from topics where fid = '$f' order by timestamp desc"); 

$r = mysql_fetch_array($result); 

$forum_title = $r['title'];
?> 

<b>Görüntüleme: <? echo $forum_title; ?></b><br /><br /> 

<a href="addthread.php?f=<? echo $f; ?>">Cevap Yaz</a><br /> 

<table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;">
  <tr>
    <td style="background-color:#eee;">Baslatan</td>
    <td style="background-color:#eee;">Son Mesaj</td>
    <td style="background-color:#eee;">Cevaplar</td>
    <td style="background-color:#eee;">Görüntüleme</td>
  </tr>
<? 

while($r2 = mysql_fetch_array($result2))
{ 

   $thread_id = $r2['id'];
   $thread_title = $r2['title'];
   $thread_username = $r2['username'];
   $thread_last_post_username = $r2['last_post_username'];
   $thread_replies = $r2['replies'];
   $thread_views = $r2['views'];
?>
  <tr>
    <td style="width:50%;background-color:#fafafa;">
    <a href="viewthread.php?t=<? echo $thread_id; ?>"><? echo $thread_title; ?></a>
    <br />started by: <b><? echo $thread_username; ?></b>
    </td>
    <td style="width:30%;background-color:#fafafa;"><? echo $thread_last_post_username; ?></td>
    <td style="width:10%;background-color:#fafafa;"><? echo $thread_replies; ?></td>
    <td style="width:10%;background-color:#fafafa;"><? echo $thread_views; ?></td>
  </tr>
<?
}
?>
  <tr>
    <td style="background-color:#eee;font-size:0;height:15px;" colspan="4">&nbsp;</td>
  </tr>
</table>

addthread.php [ Mesaj Ekle ]

Kod:
<?
// Baglanti include
include ("database.php"); 

$f = $_GET['f']; 

$result = mysql_query("select title from forums where id = '$f'");
$r = mysql_fetch_array($result);
?>
<? 

if(isset($_POST['preview']))
{
?>
<table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;">
  <tr>
    <td colspan="2" style="background-color:#eee;"><? echo $_POST['title']; ?></td>
  </tr>
  <tr>
    <td style="width:25%;background-color:#fafafa;" valign="top">
    <? echo $_POST['username']; ?></td>
    <td style="width:75%;background-color:#fafafa;" valign="top">
    <? 

    echo nl2br(htmlspecialchars($_POST['post']));
    ?>
    </td>
  </tr>
  <tr>
    <td style="background-color:#eee;font-size:0;height:15px;" colspan="4">&nbsp;</td>
  </tr>
</table>
<br />
<?
}
?>
<? 

if(isset($_POST['addthread']) && !empty($_POST['title']) && !empty($_POST['username'])
   && !empty($_POST['post']))
{ 

   $f = $_POST['f'];
   $title = addslashes(htmlspecialchars($_POST['title']));
   $username = addslashes(htmlspecialchars($_POST['username']));
   $post = addslashes(htmlspecialchars($_POST['post'])); 

   $timestamp = time(); 

   mysql_query("insert into topics (id, fid, title, username, post, last_post_username, timestamp)
      values('null', '$f', '$title', '$username', '$post', '$username', '$timestamp')"); 

   $add_count_result = mysql_query("select topics from forums where id = '$f'");
   $add_count_row = mysql_fetch_array($add_count_result);
   $add_count = $add_count_row['topics']+1; 

   .
   mysql_query("update forums set topics = '$add_count' where id = '$f'"); 

   $thread_id_result = mysql_query("select id from topics order by id desc limit 1");
   $thread_id_row = mysql_fetch_array($thread_id_result);
   $t = $thread_id_row['id'];
?>
<table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;">
  <tr>
    <td style="background-color:#eee;">Mesajiniz Eklendi</td>
  </tr>
  <tr>
    <td style="width:75%;background-color:#fafafa;" valign="top">
    Your Thread was added successfully.<br />
    <a href="viewthread.php?t=<? echo $t; ?>">Görüntülemek icin tiklayin</a>
    </td>
  </tr>
  <tr>
    <td style="background-color:#eee;font-size:0;height:15px;" colspan="4">&nbsp;</td>
  </tr>
</table>
<?
}
else{ 

<form action="addthread.php" method="post">
<input type="hidden" value="<? if(isset($_POST['f'])){ echo $_POST['f']; } else{ echo $f; } ?>"
name="f" />
<table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;">
  <tr>
    <td colspan="2"style="background-color:#eee;">Adding Forum to: <? echo $r['title']; ?></td>
  </tr>
  <tr>
    <td style="width:15%;background-color:#fafafa;">isim:</td>
    <td style="width:85%;background-color:#fafafa;">
    <input type="text" name="username"
    value="<? if(isset($_POST['username'])){ echo $_POST['username']; } ?>" />
    </td>
  </tr>
  <tr>
    <td style="width:15%;background-color:#fafafa;">Cevap Basligi:</td>
    <td style="width:85%;background-color:#fafafa;">
    <input type="text" name="title"
     value="<? if(isset($_POST['title'])){ echo $_POST['title']; } ?>" />
    </td>
  </tr>
  <tr>
    <td style="width:15%;background-color:#fafafa;" valign="top">Mesajiniz:</td>
    <td style="width:85%;background-color:#fafafa;">
<textarea name="post" cols="" rows="" style="width:80%;height:200px;">
<? if(isset($_POST['post'])){ echo $_POST['post']; } ?>
</textarea>
    </td>
  </tr>
  <tr>
    <td style="width:15%;background-color:#fafafa;" valign="top">&nbsp;</td>
    <td style="width:85%;background-color:#fafafa;">
    <input type="submit" name="preview" value="Önizleme" />
    <input type="submit" name="addthread" value="Gönder" />
    </td>
  </tr>
  <tr>
    <td style="background-color:#eee;font-size:0;height:15px;" colspan="4">&nbsp;</td>
  </tr>
</table>
</form>
<?
}
?>

viewthread.php [ Mesaj Görüntüleme ]

Kod:
<?
// baglanti.
include ("database.php"); 

$t = $_GET['t']; 

$result = mysql_query("select * from topics where id = '$t'"); 

$result2 = mysql_query("select * from replies where tid = '$t'"); 

$r = mysql_fetch_array($result); 

$topic_title = $r['title']; 

$add_count = $r['views']+1;
mysql_query("update topics set views = '$add_count' where id = '$t'");
?> 

<a href="addreply.php?t=<? echo $t; ?>">Cevap Ekle</a><br />
<table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;">
  <tr>
    <td style="background-color:#eee;" colspan="2"><? echo $topic_title; ?></td>
  </tr>
  <tr>
    <td style="width:20%;background-color:#fafafa;" valign="top">
    <? 

    echo stripslashes($r['username']); ?></td>
    <td style="width:80%;background-color:#fafafa;">
    <?  

    echo nl2br(stripslashes($r['post'])); ?></td>
  </tr>
</table> 

<? 

while($r2 = mysql_fetch_array($result2))
{
?>
<br />
<table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;">
  <tr>
    <td style="background-color:#eee;" colspan="2">re: <? echo $topic_title; ?></td>
  </tr>
  <tr>
    <td style="width:20%;background-color:#fafafa;" valign="top">
    <? echo stripslashes($r2['username']); ?></td>
    <td style="width:80%;background-color:#fafafa;">
    <? echo nl2br(stripslashes($r2['post'])); ?></td>
  </tr>
</table>
<?
}
?>

addreply.php [ Mesaj Ekle ]

Kod:
<?
// Baglanti include.
include ("database.php"); 

$t = $_GET['t']; 

$result = mysql_query("select title, fid from topics where id = '$t'");
$r = mysql_fetch_array($result);
$f = $r['fid'];
?>
<? 

if(isset($_POST['preview']))
{
?>
<table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;">
  <tr>
    <td colspan="2" style="background-color:#eee;">re: <? echo $_POST['title']; ?></td>
  </tr>
  <tr>
    <td style="width:25%;background-color:#fafafa;" valign="top">
    <? echo $_POST['username']; ?></td>
    <td style="width:75%;background-color:#fafafa;" valign="top">
    <? 

    echo nl2br(htmlspecialchars($_POST['post']));
    ?>
    </td>
  </tr>
  <tr>
    <td style="background-color:#eee;font-size:0;height:15px;" colspan="4">&nbsp;</td>
  </tr>
</table>
<br />
<?
}
?>
<? 

if(isset($_POST['addreply']) && !empty($_POST['username'])
   && !empty($_POST['post']))
{ 

   $t = $_POST['t'];
   $f = $_POST['f'];
   $username = addslashes(htmlspecialchars($_POST['username']));
   $post = addslashes(htmlspecialchars($_POST['post'])); 

   $timestamp = time(); 

   mysql_query("insert into replies (id, tid, username, post)
      values('null', '$t', '$username', '$post')"); 

   $add_count_forum_result = mysql_query("select replies from forums where id = '$f'");
   $add_count_topic_result = mysql_query("select replies from topics where id = '$t'");
   $add_count_forum_row = mysql_fetch_array($add_count_forum_result);
   $add_count_topic_row = mysql_fetch_array($add_count_topic_result);
   $add_count_forum = $add_count_forum_row['replies']+1;
   $add_count_topic = $add_count_topic_row['replies']+1; 

   mysql_query("update forums set replies = '$add_count_forum' where id = '$f'");
   mysql_query("update topics set replies = '$add_count_topic', timestamp = '$timestamp'
                where id = '$t'");
?>
<table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;">
  <tr>
    <td style="background-color:#eee;">Mesaj Eklendi</td>
  </tr>
  <tr>
    <td style="width:75%;background-color:#fafafa;" valign="top">
    Your Reply was added successfully.<br />
    <a href="viewthread.php?t=<? echo $t; ?>">Görüntülemek icin tiklayiniz.</a>
    </td>
  </tr>
  <tr>
    <td style="background-color:#eee;font-size:0;height:15px;" colspan="4">&nbsp;</td>
  </tr>
</table>
<?
}
else{ 

<form action="addreply.php" method="post">
<input type="hidden"
value="<? if(isset($_POST['t'])){ echo $_POST['t']; } else{ echo $t; } ?>" name="t" />
<input type="hidden"
value="<? if(isset($_POST['f'])){ echo $_POST['f']; } else{ echo $f; } ?>" name="f" />
<table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;">
  <tr>
    <td colspan="2"style="background-color:#eee;">Mesaj Buraya Ekleniyor: <? echo $r['title']; ?></td>
  </tr>
  <tr>
    <td style="width:15%;background-color:#fafafa;">isim:</td>
    <td style="width:85%;background-color:#fafafa;">
    <input type="text" name="username"
    value="<? if(isset($_POST['username'])){ echo $_POST['username']; } ?>" />
    </td>
  </tr>
  <tr>
    <td style="width:15%;background-color:#fafafa;" valign="top">Mesajiniz:</td>
    <td style="width:85%;background-color:#fafafa;">
<textarea name="post" cols="" rows="" style="width:80%;height:200px;">
<? if(isset($_POST['post'])){ echo $_POST['post']; } ?>
</textarea>
    </td>
  </tr>
  <tr>
    <td style="width:15%;background-color:#fafafa;" valign="top">&nbsp;</td>
    <td style="width:85%;background-color:#fafafa;">
    <input type="submit" name="preview" value="Önizleme" />
    <input type="submit" name="addreply" value="Postala" />
    </td>
  </tr>
  <tr>
    <td style="background-color:#eee;font-size:0;height:15px;" colspan="4">&nbsp;</td>
  </tr>
</table>
</form>
<?
}
?>

İşlemler bu kadar.

    Yorumlar...
    (Toplam 7 yorum var.)

    Bu yorumu gerçekten şikayet etmek istiyor musunuz ???


    Bir sebep belirtmek isterseniz alttaki kutucuÄŸa yazabilirsiniz...




    Pencereyi Kapat...
  1. 26.06.2010 23:11
    Emre


    Misafir




    Güzele benziyor teşekkürler
  2. 02.02.2011 13:43
    Unreal


    Misafir




    güzel fakat kullanıcı sistemi neden yok? üyelik üye girişi falan.
  3. 03.02.2011 09:37


    Admin
    Yorum: 262
    Forum Mesaj: 212




    @ Unreal: Forum mantığını kavramaya yönelik bir örnek olduğu için o kısımlar yok. Hali hazırda forumlar zaten mevcut fakat onların karmaşasından mantığı kavramak çok zor olur. Kendi forumunu yazmak isteyen site sahipleri için ideal bir örnek...
  4. 04.02.2011 16:13
    Unreal


    Misafir




    @ admin: evet farkındayım zaten çok güzel bir örnek bu ama üyelik sistemide entegreli bir şekilde anlatılmış olsaydı geliştiriciler için çok güzel bir kaynak olurdu :)) ama kendi yazdığım üyelik sistemini buna entegre etmek de çok zor değil.
  5. 04.02.2011 16:53


    Admin
    Yorum: 262
    Forum Mesaj: 212




    @ Unreal: Entegreli halini bekleriz :)
  6. 10.02.2012 20:02
    QFUSION


    Misafir




    bir ÅŸey soracagım peki indexe nasıl editleme yapabiliriz bir yol gösterebilirmisiniz


  7. 11.02.2012 21:48


    Admin
    Yorum: 262
    Forum Mesaj: 212




    @ QFUSION:

    Nasıl bir editleme?


1

Yorum Yazın