• 18-05-2009, 17:06:16
    #1
    Üyeliği durduruldu
    Ilkönce veritabani bilgilerini vereyim.. kat_seq sequenz ile calistigim icin gerekmektedir. Tabloyu buradaki gibi olusturmazsaniz ve veri girmeye calisirsaniz "constraint violation" hatasi alirsiniz.
    Tablo yapisi ve verileri
    CREATE TABLE IF NOT EXISTS `kat_seq` (
      `sequence` int(11) NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`sequence`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=30 ;
    --
    -- Daten für Tabelle `kat_seq`
    --
    INSERT INTO `kat_seq` (`sequence`) VALUES
    (29);
    --
    -- Tabellenstruktur für Tabelle `kat`
    --
    CREATE TABLE kat(
    id bigint( 20 ) NOT NULL ,
    isim varchar( 50 ) NOT NULL ,
    ust_fk bigint( 20 ) ,
    FOREIGN KEY ( ust_fk ) REFERENCES kat( id ) ON DELETE SET NULL ,
    PRIMARY KEY ( id )
    ) ENGINE = InnoDB;
    --
    -- Daten für Tabelle `kat`
    --
    INSERT INTO `kat` (`id`, `isim`, `ust_fk`) VALUES
    (1, 'Google', NULL),
    (2, 'E-Ticaret', NULL),
    (3, 'Programlama', NULL),
    (4, 'Google Adsense', 1),
    (5, 'Google Optimizasyon', 1),
    (6, 'Google PageRank', 1),
    (7, 'Adsense Ödemeleri', 4),
    (8, 'Meta-Tag', 5),
    (9, 'Sitemap', 5),
    (10, 'Google-APi', 5),
    (11, 'SandBox', 5),
    (12, 'AlisVeris', 2),
    (13, 'Ilan Panosu', 2),
    (14, 'Site Satisi', 12),
    (15, 'Script Satisi', 12),
    (16, 'is Arayanlar', 13),
    (17, 'Is Verenler', 13),
    (18, 'Seo Danismanligi', 2),
    (19, 'Client side Programlama', 3),
    (20, 'Server Side Programlama', 3),
    (21, 'JavaScript', 19),
    (22, 'HTML-CSS-XML', 19),
    (23, 'Ajax', 21),
    (24, 'DOM - JSON', 21),
    (25, 'ASP', 20),
    (26, 'PHP', 20),
    (27, 'ASP HazirKod', 25),
    (28, 'ASP.Net', 25);
    CRUD_Kat.php Dosyasi
    <?php
    	/**
    	* Qonyali
    	* msn  : halil@tiklaa.com
    	* mail : qonyali@gmail.com
    	*/
    class CRUD_Kat {
    	protected $id;
    	protected $isim;
    	protected $ust_fk;
    	//GETTER
    	public function getId() { return $this->id; } 
    	public function getIsim() { return $this->isim; }
    	public function getUst_fk() { return $this->ust_fk; } 
    	//SETTER
    	public function setIsim($x) { $this->isim = $x; }
    	public function setUst_fk($x) { $this->ust_fk = $x; }  
    	function __construct($dic) {
    		$this->id = $dic['id'];
    		$this->isim = $dic['isim'];
    		$this->ust_fk = $dic['ust_fk'];
    	}
    	public function __toString(){
    		$str ="$this->id : $this->isim $this->ust_fk";
    		return $str;
    	}
    	public static function create($dic){
    		$dic['id'] = MDB2_Util::create_id('kat');
    		if(array_key_exists('ust_fk',$dic)) {
    			$dic['ust_fk'] = $dic['ust_fk'];
    			if(is_object($dic['ust_fk']))
    				$dic['ust_fk'] = $dic['ust_fk']->getId();
    		} else {
    			$dic['ust_fk'] = null;
    		}
    		$sql = "insert into kat (id, isim, ust_fk) values(?,?,?)";
    		$data = array($dic['id'], $dic['isim'], $dic['ust_fk']);
    		MDB2_Util::query($sql, $data);
    		return new Kat($dic);
    	}
    	public function update(){
    		$sql = "update kat 
    					set isim = ?, ust_fk = ? where id = ?";
    		$data = array($this->isim, $this->ust_fk, $this->id);
    		MDB2_Util::query($sql, $data);
    	}
    	public function delete(){
    		$sql = "delete from kat 
    					where id = ?";
    		$result = MDB2_Util::query($sql, array($this->id));
    		if($result != 1)
    			die("$id numarali veri silinemedi!");
    		unset($this);
    	}
    	public static function getById($id) {
    		$sql = "select id, isim, ust_fk from kat where id = ?";
    		$object_list = MDB2_Util::object_query($sql, 'Kat', array($id));
    		if (count($object_list)>0) {
    			return $object_list[0];
    		} else
    			die ("$id numarali veri bulunamadi");
    	}
    	public static function findAll() {
    	    $sql = "select id, isim, ust_fk from kat";
    	    return MDB2_Util::object_query($sql, 'Kat');
    	}
    	public static function deleteById($id) {
    	    $sql = "delete from kat where id = ?";
    	    $result = MDB2_Util::query($sql, array($id));
    	    if ($result != 1) die("$id numarali veri silinemedi!");
    	}
    	public static function findAnaKat(){
    		$sql = "select id, isim, ust_fk from kat where ust_fk is null";
    		return MDB2_Util::object_query($sql, 'Kat');
    	}
    	public static function fetchAltKatById($id, $pre = 1){
    		$sql = mysql_query("SELECT * FROM kat WHERE ust_fk='$id'");
    		while($res = mysql_fetch_array($sql)){
    			if(!empty($res)){
    				echo str_repeat('&nbsp;', $pre*$pre);
    				echo '>';
    				echo '<a href="testrun.php?do=delete&id='.$res['id'].'">[delete]</a>';
    				echo '<a href="testrun.php?do=update&id='.$res['id'].'">[update]</a>';
    				echo $res['id'] .' - '.$res['isim'];
    				echo '<br />';
    				self::fetchAltKatById($res['id'], ($pre+1));
    			}
    		}
    	}
    }
    ?>
    Kat.php dosyasi
    <?php
    	/**
    	* Qonyali
    	* msn  : halil@tiklaa.com
    	* mail : qonyali@gmail.com
    	*/
    require_once('CRUD_Kat.php');
    class Kat extends CRUD_Kat {
    	/**
    	 * Konstruktor
    	 * @param $dic
    	 */
    	public function __construct($dic) {
    		parent::__construct($dic);
    	}
    }
    ?>
    testrun.php dosyasi
    <?php
    	/**
    	* Qonyali
    	* msn  : halil@tiklaa.com
    	* mail : qonyali@gmail.com
    	*/
    $do = $_REQUEST['do'];
    $id = $_REQUEST['id'];
    $ka = $_REQUEST['kat'];
    $f = $_REQUEST['f'];
    // Imports
    require_once 'MDB2_Util.php';
    require_once 'Kat.php';
    // connect to database
    $prop = new Property('connect.prop');
    MDB2_Util::Connect($prop->dsn);  
    echo '<a href="testrun.php?do=add">[add]</a>';
    switch ($do){
    	case 'add':
    		if($ka['isim'] != ""){
    			Kat::create(array('isim'=>$ka['isim'], 'ust_fk'=>$ka['ust_fk']));
    			echo "Kategori eklendi";
    		}
    		echo <<<END
    			<form method="post" action="testrun.php?do=add">
    			<label>Isim :</label> <input name="kat[isim]" type="text">
    			<label>ÜstKat :<select name="kat[ust_fk]">
    			<option value="">Yeni AnaKat</option>
    END;
    			$kat = Kat::findAll();
    			foreach($kat as $k){
    				echo '<option value="'.$k->getId().'">'.$k->getId().' - '.$k->getIsim().'</option>';
    			}
    			echo '</select>';
    			echo '<input type="submit" value="Ekle">
    			</form>';
    	break;
    	case 'delete':
    		Kat::deleteById($id);
    		echo "$id numarali Kategori silindi";
    	break;
    	case 'update':
    		$edit = Kat::getById($id);
    		if($f == "ok"){
    			$edit->setIsim($ka['isim']);
    			$edit->setUst_fk($ka['ust_fk']);
    			$edit->update();
    		}
    		echo '<form method="post" action="testrun.php?do=update&f=ok">';
    		echo '<label>Isim :</label> <input name="kat[isim]" type="text" value="'.$edit->getIsim().'">';
    		echo '<label>ÜstKat :<select name="kat[ust_fk]">';
    		echo '<option value="">Yeni AnaKat</option>';
    		$kat = Kat::findAll();
    		foreach($kat as $k){
    			if($k->getId() == $edit->getUst_fk())
    				echo '<option value="'.$k->getId().'" selected>'.$k->getId().' - '.$k->getIsim().'</option>';
    			else
    				echo '<option value="'.$k->getId().'">'.$k->getId().' - '.$k->getIsim().'</option>';
    		}
    		echo '</select>';
    		echo '<input type="hidden" name="id" value="'.$edit->getId().'">';
    		echo '<input type="submit" value="Düzenle">
    			</form><br />';
    	break;
    }
    echo "<br /><br />";
    $ana = Kat::findAnaKat();
    foreach($ana as $a){
    	echo $a->getId().' - '.$a->getIsim()."<br />";
    	Kat::fetchAltKatById($a->getId());
    }
    ?>
    Bunlarin disinda gerekli olan dosyalari zip dosyasi seklinde foruma yüklüyorum..
    yapmaniz gerekenler:
    ya connect.prop icindeki DSN'i düzenleyin, yada denemeler isimli bi veritabani olusturup yukaridaki veritabani bilgilerini girin. sonrada testrun.php dosyasini calistirin.
    birde ekran görüntüsü
  • 19-05-2009, 21:45:18
    #2
    süpersin
  • 19-05-2009, 22:33:25
    #3
    sagol dostun + rep
  • 23-05-2009, 23:41:03
    #4
    güzel paylaşım teşekkürler...
  • 24-05-2009, 03:07:15
    #5
    hocam eline sağlı süper olmuş.. herkese faydalı olacaktır umarım.
  • 25-05-2009, 09:07:02
    #6
    elerine saglık yararlı çalısma