24 / 05 / 2009

3 katmanlı mimari ve php -2-

ekleyen: Emre Çevik kategori: php| smarty

Artık haber sistemini yapmaya başlıyoruz. Öncelikle bu bölümde yapacağımız haber sistemini tanımlayalım. Haber sistemi 3 sayfadan oluşuyor.

1- Ana sayfa
2- Kategori safası
3- Haber detayları

makalenin birinci bölümünü oku

Ana sayfayı yapmaya başlayalım :

page

Yukarıdaki resimde ana sayfamız görünmektedir. Sol taraftaki haber listesini oluşturmak için fonksiyonumuzu yazalım.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function ShowHeadlines($limit=10,$start="",$category="") 
    { 
 
 
        $fields        = array("id","picture","title","info"); 
        $whr         = (!empty($category)) ? " category = '".int($category)."'" : " category != '0'"; // eğer kategori seçili ise 
        $where        = array($whr); 
        $order        = array("id DESC"); 
        $limit         = (!empty($start)) ? $start.",".int($limit) : int($limit); // eğer başlangıç değeri varsa 
        $table        = array($this->sql_table); 
 
        $sql                 = $this->db->Query($fields, $table, $where, $order, "", $limit); 
        $items                = array(); 
 
        $image_w    = 120; 
        $image_h    = 0; 
 
        while ($row = $sql->FetchRow())  
        { 
            $row->link = "index.php?page=news&action=view_news&id=".$row->id; 
 
            // CREATE THUMB 
 
            if(!empty($row->picture)) // Eğer haber resmi varsa 
            { 
                $hash = $this->CacheHash($row->picture,$image_h,$image_w); // resim adini oluştur 
                $row->image = "cache/".$hash.".jpg"; 
 
                if(!isfile(BASEDIR."/".$row->image)) // eğer resim cachelenmemişse resmi kaydet. 
                { 
                    $thumb = new Thumbnail($row->picture); 
                    $thumb->resize($image_w,$image_h); 
                    $thumb->crop(0,0,120,75); 
                    $thumb->save(BASEDIR."/"."cache/".$hash.".jpg"); 
                    $thumb->destruct(); 
                } 
            } 
            array_push($items,$row); // değerleri diziye aktar 
        } 
 
        return $items; 
    }

Şimdide sağ taraftaki haber listesi için fonksiyonumuzu yazalım.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    function ListHeadlines($limit=10,$start="",$category="") 
    { 
        $fields        = array("id","title"); 
        $whr         = (!empty($category)) ? " category = '".int($category)."'" : " category != '0'"; // eğer kategori seçili ise 
        $where        = array($whr); 
        $order        = array("id DESC"); 
        $limit         = (!empty($start)) ? $start.",".int($limit) : int($limit); // eğer başlangıç değeri varsa 
        $table        = array($this->sql_table); 
 
        $sql         = $this->db->Query($fields, $table, $where, $order, "", $limit); 
        $items            = $sql->MultipleRow();    // değerleri $item değişkenine aktar. 
 
        return $items; 
    }

Ana sayfamızı bitirdik. Aşağıda görünen kategori sayfasıda ana sayfa ile ilgili aynı bölümleri taşımaktadır. Onun için ayrı bir fonksiyon yazmamıza gerek yoktur.

page2

Şimdide haber detaylarını oluşturan fonksiyonumuzu yazalım :

page3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
 
    function SelectNews($id) 
    { 
        $id         = int($id); 
 
        $fields        = array("*"); 
        $where        = array("id='".$id."'","active='1'"); 
        $limit        = 1; 
        $table        = array($this->sql_table); 
 
        $query        = $this->db->Query($fields, $table, $where,"","",$limit); 
 
        return $query; 
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    function ShowNews($id) 
    { 
        $image_w    = 250; 
        $image_h    = 0; 
 
        $sql = $this->SelectNews($id); // Haber verilerini veritabanından al 
 
        if($sql->BoolQuery()) // Eğer veri varsa 
        { 
            $row = $sql->FetchRow();   
 
 
            if(!empty($row->picture)) // Eğer haber resmi varsa 
            { 
                $hash = $this->CacheHash($row->picture,$image_h,$image_w); // resim adini oluştur 
                $row->image = "cache/".$hash.".jpg"; 
 
                if(!isfile(BASEDIR."/".$row->image)) // eğer resim cachelenmemişse resmi kaydet. 
                { 
                    $thumb = new Thumbnail($row->picture); 
                    $thumb->resize($image_w,$image_h); 
                    $thumb->save(BASEDIR."/"."cache/".$hash.".jpg"); 
                    $thumb->destruct(); 
                }     
            } 
            return $row; 
        } else { 
            location(); // eğer veri yoksa ana sayfaya yönlendir. 
        } 
 
    }

Tüm fonksiyonlarımızı tamamladık şimdi haber sınıfımızı oluşturabiliriz.

class/news.class.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php 
Class News extends General 
{ 
    var $sql_table     = "news"; 
    var $cat_table     = "news_category"; 
 
    /** 
     * Constructor... 
     * 
     */ 
    function News(&$db) 
    { 
        $this->General(&$db); 
    } 
 
    /** 
     * veritabanı arasındaki bağlantı... 
     * 
     * @param int $id 
     * @param bool $active 
     * @return unknown 
     */ 
    function SelectNews($id) 
    { 
        $id         = int($id); 
 
        $fields        = array("*"); 
        $where        = array("id='".$id."'","active='1'"); 
        $limit        = 1; 
        $table        = array($this->sql_table); 
 
        $query        = $this->db->Query($fields, $table, $where,"","",$limit); 
 
        return $query; 
    } 
 
    /** 
     * Enter description here... 
     * 
     * @param int $id 
     * @param bool $active 
     * @return object 
     */ 
    function ShowNews($id) 
    { 
        $image_w    = 250; 
        $image_h    = 0; 
 
        $sql = $this->SelectNews($id); // Haber verilerini veritabanından al 
 
        if($sql->BoolQuery()) // Eğer veri varsa 
        { 
            $row = $sql->FetchRow();   
 
 
            if(!empty($row->picture)) // Eğer haber resmi varsa 
            { 
                $hash = $this->CacheHash($row->picture,$image_h,$image_w); // resim adini oluştur 
                $row->image = "cache/".$hash.".jpg"; 
 
                if(!isfile(BASEDIR."/".$row->image)) // eğer resim cachelenmemişse resmi kaydet. 
                { 
                    $thumb = new Thumbnail($row->picture); 
                    $thumb->resize($image_w,$image_h); 
                    $thumb->save(BASEDIR."/"."cache/".$hash.".jpg"); 
                    $thumb->destruct(); 
                }     
            } 
            return $row; 
        } else { 
            location(); // eğer veri yoksa ana sayfaya yönlendir. 
        } 
 
    }     
 
    /** 
     * Haber detayları ... 
     * 
     * @param int $limit 
     * @param int $start 
     * @param int $category 
     * @return array 
     */ 
    function ShowHeadlines($limit=10,$start="",$category="") 
    { 
 
 
        $fields        = array("id","picture","title","info"); 
        $whr         = (!empty($category)) ? " category = '".int($category)."'" : " category != '0'"; // eğer kategori seçili ise 
        $where        = array($whr); 
        $order        = array("id DESC"); 
        $limit         = (!empty($start)) ? $start.",".int($limit) : int($limit); // eğer başlangıç değeri varsa 
        $table        = array($this->sql_table); 
 
        $sql                 = $this->db->Query($fields, $table, $where, $order, "", $limit); 
        $items                = array(); 
 
        $image_w    = 120; 
        $image_h    = 0; 
 
        while ($row = $sql->FetchRow())  
        { 
            $row->link = "index.php?page=news&action=view_news&id=".$row->id; 
 
            // CREATE THUMB 
 
            if(!empty($row->picture)) // Eğer haber resmi varsa 
            { 
                $hash = $this->CacheHash($row->picture,$image_h,$image_w); // resim adini oluştur 
                $row->image = "cache/".$hash.".jpg"; 
 
                if(!isfile(BASEDIR."/".$row->image)) // eğer resim cachelenmemişse resmi kaydet. 
                { 
                    $thumb = new Thumbnail($row->picture); 
                    $thumb->resize($image_w,$image_h); 
                    $thumb->crop(0,0,120,75); 
                    $thumb->save(BASEDIR."/"."cache/".$hash.".jpg"); 
                    $thumb->destruct(); 
                } 
            } 
            array_push($items,$row); // değerleri diziye aktar 
        } 
 
        return $items; 
    } 
 
    /** 
     * Haberleri diziye ekler (resimsiz)... 
     * 
     * @param int $limit 
     * @param int $start 
     * @param int $category 
     * @return array 
     */ 
    function ListHeadlines($limit=10,$start="",$category="") 
    { 
        $fields        = array("id","title"); 
        $whr         = (!empty($category)) ? " category = '".int($category)."'" : " category != '0'"; // eğer kategori seçili ise 
        $where        = array($whr); 
        $order        = array("id DESC"); 
        $limit         = (!empty($start)) ? $start.",".int($limit) : int($limit); // eğer başlangıç değeri varsa 
        $table        = array($this->sql_table); 
 
        $sql         = $this->db->Query($fields, $table, $where, $order, "", $limit); 
        $items            = $sql->MultipleRow();    // değerleri $item değişkenine aktar. 
 
        return $items; 
    } 
 
 
} 
?>

Haber sınıfımızı tamamladıktan sonra bu sınıfı çağıracak php dosyamız ve template sistemini yapmak kaldı. Sırayla php dosyalarını ve template sistemini yapalım.

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
error_reporting(0); // hataları gizle
session_start(); // oturumu başlat
 
define("BASEDIR", dirname(__FILE__));
 
// sınıf ve gerekli dosyaları yükle
 
include_once(BASEDIR . "/class/thumb.class.php");
include_once(BASEDIR . "/class/functions.php");
include_once(BASEDIR . "/class/database.class.php");
include_once(BASEDIR . "/class/general.class.php");
include_once(BASEDIR .  "/libs/Smarty.class.php");
include_once(BASEDIR . "/class/page.class.php");
include_once(BASEDIR . "/class/news.class.php");
 
$database             = new DataLayer(); // veritabanı sınıfını oluştur
$theme                = new Page(); // tema sınıfını oluştur
 
@include("sistem/$theme->page.php"); // modülü yükle
echo $theme->fetch($theme->tpl); // temayı işle
 
?>

templates/main/main.tpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-9" />
<title>3 katmanli mimari</title>
<link href="templates/css/stil.css" _fcksavedurl=""templates/css/stil.css"" _fcksavedurl=""templates/css/stil.css"" _fcksavedurl=""templates/css/stil.css"" _fcksavedurl=""templates/css/stil.css"" _fcksavedurl=""templates/css/stil.css"" _fcksavedurl=""templates/css/stil.css"" _fcksavedurl=""templates/css/stil.css"" _fcksavedurl=""templates/css/stil.css"" rel="stylesheet" type="text/css" />
</head>
<body>
<center>
<div id="main">
<table width="100%" bgcolor="#003366" cellpadding="0" cellspacing="0" align="center">
 <tr>
  <td><img src="templates/images/main/logo.gif" _fcksavedurl=""templates/images/main/logo.gif"" _fcksavedurl=""templates/images/main/logo.gif"" _fcksavedurl=""templates/images/main/logo.gif"" _fcksavedurl=""templates/images/main/logo.gif"" alt=""></td>
 </tr>
 <tr>
  <td bgcolor="#003366" colspan="2">
  <div id="nav">
  <table width="100%" cellspacing="0" cellpadding="0">
   <tr>
     <td class="fize"><a href="index.php">Anasayfa</a></td>    
     <td class="fize"><a href="index.php?page=news&action=view_category&category=1">Sağlık</a></td>   
     <td class="fize"><a href="index.php?page=news&action=view_category&category=2">Ekonomi</a></td>   
     <td class="fize" width="400"></td>    
   </tr>
  </table>
  </div>
 
  </td>
 </tr>
 <tr>
  <td width="100%" bgcolor="#003366">
   <div id="content">
   <table width="100%" cellpadding="0" cellspacing="0">
    <tr>
     <td width="100%" valign="top">
      <div id="beyaz">{$content}</div>
     </td>
    </tr>
   </table>
   </div>  
  </td>
 </tr>
 <tr>
  <td>
   <div id="footer">
    <div id="first"><div class="yazilim">yazılım <br /> <a class="beyaz" href="mailto:info@internet.com.tr">Emre Çevik</a></div></div>
    <div id="second">Copyright© 2007 Benim Şirketim</div>
   </div>
  </td>
 </tr>
</table>
 
</div>
</center>
</body>
</html>

sistem/index.php

(index.php?page=index)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php 
 
$news_class = new News($database); // Haber sınıfını oluştur 
 
$items         = $news_class->ShowHeadlines(5);     // 5 tane ana haberi çağır (sağ kısım) 
$list         = $news_class->ListHeadlines(10,5); // 5 haberden sonra 10 tane kısa haber çağır (sol kısım) 
 
 
$theme->assign('items', $items); 
$theme->assign('list', $list); 
 
$fetch_tpl = "home/index.tpl";   
$theme->assign('content', $theme->fetch($fetch_tpl));         
 
?>

templates/home/index.tpl

(index.php?page=index)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<table width="100%" cellpadding="0" cellspacing="0" border="0">
 <tr>
  <td width="430" valign="top">
  {foreach from=$items item=news} 
  <table width="100%" cellpadding="0" cellspacing="0">
   <tr>
    <td>
     <div class="newsdiv">
      <div class="titlediv"><a href="{$news->link}">{$news->title}</a></div> 
      {if $news->image}<div class="news_img"><img src="{$news->image}" /></div>{/if}
      {$news->info}
      <div style="clear:both;"></div>
     </div>
    </td>
   </tr>
  </table>
  {/foreach}
 
  </td>
  <td valign="top">
  {if $list}
  <div class="newsdiv">
  <div class="titlediv" style="margin-bottom:1px;">Diğer haberler</div>
  {foreach from=$list item=ls} 
   <div class="listdiv"><a href="{$ls->link}">{$ls->title}</a></div>
  {/foreach}
  </div>
  {/if}
  </td>
 </tr>
</table>

sistem/news.php

Haber dosyamız : (index.php?page=news)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
 
$news_class = new News($database); // Haber sınıfını oluştur
 
switch ($_REQUEST["action"])
{
    case "view_news":
        $items = $news_class->ShowNews($_REQUEST["id"]); // Haber detaylarını çağır
        $theme->assign('items', $items);
        $fetch_tpl = "news/news_details.tpl";
    break;
 
    case "view_category":
        $categ = int($_REQUEST["category"]);
        if(!empty($categ))
        {
            $w_category = "category = '".$categ."' ";    
            $n_category = $categ;
            $s_category = "&category=".$categ;
        } else {
            $w_category = "";
            $n_category = "";
            $s_category = "";
        }
 
        if(empty($_REQUEST["p"]) or $_REQUEST["p"] == 0) $_REQUEST["p"] = 1;
 
 
        $fields        = array("COUNT(id) as total");
        $where        = array($w_category);
        $table        = array("news");
 
 
        $query     = $database->Query($fields, $table, $where);
        $sql     = $query->SingleRow();
 
        $news_page     = int($_REQUEST["p"]);
        $news_total = $sql->total;
        $news_limit    = 3;
        $start        = ($news_page - 1) * $news_limit;
 
 
        $items = $news_class->ShowHeadlines($news_limit,$start,$n_category); // Ana haberleri çağır
        $pagination = $news_class->Pagination("index.php","news","view_category",$s_category,$news_page,$news_total,$news_limit); // sayfalama çubuğunu çağır 
        $theme->assign('items', $items);
        $theme->assign('pagination', $pagination);
        $fetch_tpl = "news/news_archive.tpl";
    break;
}
 
$theme->assign('content', $theme->fetch($fetch_tpl));
 
?>

templates/news/news_archive.tpl

(index.php?page=news&action=view_category)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<table cellpadding="0" cellspacing="0">
 <tr>
  <td width="430" valign="top">
  {foreach from=$items item=news} 
  <table width="100%" cellpadding="0" cellspacing="0">
   <tr>
    <td>
     <div class="newsdiv">
      <div class="titlediv"><a href="{$news->link}">{$news->title}</a></div> 
      {if $news->image}<div class="news_img"><img src="{$news->image}" /></div>{/if}
      {$news->info}
      <div style="clear:both;"></div>
     </div>
    </td>
   </tr>
  </table>
  {/foreach}
  {$pagination}
  </td>
  <td valign="top">
 
  </td>
 </tr>
</table>

templates/news/news_details.tpl

(index.php?page=news&action=view_news)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<table width="100%" cellpadding="0" cellspacing="0">
 <tr>
  <td width="100%" valign="top">
     <div class="newsdiv">
      <div class="titlediv">{$items->title}</div>
      <div style="padding:5px">
      {if $items->image}<div class="news_img"><img src="{$items->image}" /></div>{/if}
      {$items->info}
      <br />
      {$items->more}
      </div>
      <div style="clear:both;"></div>
     </div>  
  </td>
 
 </tr>
</table>
dosya : 3 katmanlı mimari ve php

makalenin birinci bölümünü oku

  • Digg
  • del.icio.us
  • Facebook
  • Google
  • BlinkList
  • blogmarks
  • description
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • Technorati

benzer konular:

  1. 3 katmanlı mimari ve php -1-
  2. class : codeigniter [ ekle / sil / güncelle ]
  3. composite design pattern – php
  4. auth sınıfı kullanımı
  5. facade design pattern – php
  6. auth sınıfı
  7. smarty

etiketler: ,

(4 votes, average: 5.00 out of 5)
Loading ... Loading ...

henüz yorum yazılmadı

yorum yaz

ne yapıyoruz

eğleniyoruz?

anket

design pattern yazılarını nasıl buldunuz?

sonuçlar

Loading ... Loading ...