Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#9

AW: [PHP] Klassenstruktur für Adressdatenbank

  Alt 8. Feb 2012, 16:06
Könnte ich damit glücklich werden:
Code:
<?php

class DB_Exception extends Exception {}

class Contact extends DB_Exception{

   private $connection;
   private $resultset;
   public $id = array();
   public $name = array();
   public $vorname = array();
   
   function __construct() {
   
      include_once("config.php");
      
      $this->connection = mysql_connect($dbserver, $user, $password);
      if ($this->connection == false)
      {      
         die(mysql_error());
      }
      $db = mysql_select_db("usr_l3s11195_1");
      if ($db == false)
      {      
         die(mysql_error());
      }
   }
   
   function __destruct() {
      mysql_free_result($this->resultset);
      mysql_close($this->connection);
   }
   
   public function getByName() {         
      $query = "SELECT
               a.*, k.name as k_name
               FROM adressen_data a
               LEFT OUTER JOIN adressen_kat k on k.id = a.kategorie_id
               WHERE a.name LIKE '%".$this->name[0]."%'
               ORDER BY k.id, a.name, a.vorname, a.gesch_firma";
      $this->resultset = mysql_query($query);
      if (!$this->resultset) {
         throw new DB_Exception(@mysql_error());
      }      
      
      $this->clearFields();
      $this->fillFields();
   }
   
      public function getBySurname() {         
      $query = "SELECT
               a.*, k.name as k_name
               FROM adressen_data a
               LEFT OUTER JOIN adressen_kat k on k.id = a.kategorie_id
               WHERE a.vorname LIKE '%".$this->vorname[0]."%'
               ORDER BY k.id, a.name, a.vorname, a.gesch_firma";
      $this->resultset = mysql_query($query);
      if (!$this->resultset) {
         throw new DB_Exception(@mysql_error());
      }      
      
      $this->clearFields();
      $this->fillFields();
   }
   
   public function getById() {         
      $query = "SELECT
               a.*, k.name as k_name
               FROM adressen_data a
               LEFT OUTER JOIN adressen_kat k on k.id = a.kategorie_id
               WHERE a.id = '".$this->id[0]."'
               ORDER BY k.id, a.name, a.vorname, a.gesch_firma";      
      $this->resultset = mysql_query($query);
      if (!$this->resultset) {
         throw new DB_Exception(@mysql_error());
      }      
      
      $this->clearFields();
      $this->fillFields();
   }
   
   public function save() {
   
   }
   
   public function edit() {
   
   }
   
   private function fillFields() {
      while ($row = mysql_fetch_object($this->resultset)) {
         $this->id[] = $row->id;
         $this->name[] = $row->name;
         $this->vorname[] = $row->vorname;            
      }      
   }
   
   private function clearFields() {
      unset($this->id);
      unset($this->name);
      unset($this->vorname);
   }
}

?>
Code:
<?php

include_once("Contact.php");

   class View {
      private $contact;
      
      public function getContactByName($filterStr) {
         try {
            $this->contact = new Contact();
            $this->contact->name[0] = $filterStr;         
            $this->contact->getByName();
            $this->htmlOutPut();
         }
         catch(DB_Exception $e) {
            echo $e->getMessage();
         }
      }   
      
      public function getContactBySurname($filterStr) {
         try {
            $this->contact = new Contact();
            $this->contact->vorname[0] = $filterStr;         
            $this->contact->getBySurname();
            $this->htmlOutPut();
         }
         catch(DB_Exception $e) {
            echo $e->getMessage();
         }
      }

      public function getContactById($Id) {
         try {
            $this->contact = new Contact();
            $this->contact->id[0] = $Id;         
            $this->contact->getById();
            $this->htmlOutPut();
         }
         catch(DB_Exception $e) {
            echo $e->getMessage();
         }
      }
      
      private function htmlOutPut() {
         for($i=0; $i < count($this->contact->id); $i++) {
            if (count($this->contact->id) > 1) {
               echo "ID: ".$this->makeLink("index.php?action=show&id=".$this->contact->id[$i], $this->contact->id[$i])."<br>\n";
               echo "Name: ".$this->contact->name[$i]."<br>\n";
               echo "Vorame: ".$this->contact->vorname[$i]."<br>\n";
               echo "<br>\n";
            } else {
               echo "Name: ".$this->contact->name[$i]."<br>\n";
               echo "Vorame: ".$this->contact->vorname[$i]."<br>\n";
               echo "<br>\n";
               echo $this->makeLink("index.php", "Bearbeiten")." | ".$this->makeLink("index.php", "Löschen")."\n";
            }
         }   
         unset($contact);
      }
      
      private function makeLink($href, $caption) {
         return "<a href=\"".$href."\">".$caption."</a>";
      }
      
   }
?>
Code:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   </head>
   <body>
      <form action="index.php" method="GET">      
         <input type="hidden" name="action" value="search">         
         <select name="field" size="1">                        
            <option value="name">Name</option>
            <option value="vorname">Vorname</option>
         </select>      
         <input name="filterStr" type="text"></input>
         <input type="submit" value="Suchen"></input>
      </form>   
      
      <?php
         include_once("View.php");
         
         $action = $_GET['action'];
         $id = $_GET['id'];
         $filterStr = $_GET['filterStr'];
         $field = $_GET['field'];
         
         $view = new View();
         
         switch ($action) {            
            case "search":
               switch ($field) {
                  case "name":
                     $view->getContactByName($filterStr);
                     break;
                  case "vorname":
                     $view->getContactBySurname($filterStr);
                     break;
               }
               break;
            case "show":
               $view->getContactById($id);
               break;
            default:
               echo "<a href=\"index.php\"n>Neuer Kontakt</a>\n";
               break;
         }
      ?>
   </body>
</html>
Es kommen noch ein paar Suchabfragen dazu und die Datenbank hat so um die 20 Felder.

Könnte man auch noch was verbessern oder vereinfachen? Oder könnte ich irgendwo Probleme bekommen?
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat