frankx: Kartenbuchung/Reservierungsprogramm

Beitrag lesen

Ahoi,

Eigentlich müsstest Du jedem freien Feld die Möglichkeit geben, es zu belegen. Dazu wäre auch eine checkbox tauglich. Die ganze Tabelle wäre dann ein Formular, was du so an den Server schickst, damit die Bestellung erstmal im Warenkorb landet.

in etwa so:

  
<?php  
/**  
 * 24.3.2009  
 * @author R. Sauer-Ernst  
 */  
  
class Model_Ticketsystem //will become an absctract class sometime  
{  
    private $_rows = 0;  
    private $_seatsPerRow = 0;  
    private $_categories = array();  
    private $_tickets = array();  
  
    public function __construct() {  
       define ("ROW_SEAT_SEPARATOR","_");  
       // could be the function to overwrite when extending abstract class  
       $this->_init();  
    }  
  
    private function _createTickets() {  
        for ($rowNr = 1; $rowNr <= $this->_rows; $rowNr++) {  
            for ($seatNr = 1; $seatNr <= $this->_seatsPerRow; $seatNr++) {  
                // id is commaseparated rownumber and seatnumber  
                $id = $rowNr . ROW_SEAT_SEPARATOR . $seatNr;  
                $this->_tickets[$id]["row"] = $rowNr;  
                $this->_tickets[$id]["seat"] = $seatNr;  
                $this->_tickets[$id]["category"] = $this->_getCategory($rowNr);  
            }  
        }  
        $this->_readData();  
        $this->setStatus();  
    }  
  
    private function _getCategory ($rowNr) {  
       $categoryCount = count($this->_categories);  
       $rowsPerCat = $this->_rows/$categoryCount;  
       $categoryNr = floor(($rowNr-1)/$rowsPerCat);  
        return $this->_categories[$categoryNr];  
    }  
  
    public function setStatus ($statusList = array()) {  
        //read data base  
            foreach ($this->_tickets as $id => $values) {  
            if (isset($statusList[$id])) {  
                $this->_tickets[$id]["status"] = $statusList[$id];  
            } elseif (!isset($this->_tickets[$id]["status"])) {  
                $this->_tickets[$id]["status"] = "frei";  
            }  
        }  
    }  
  
    private function _readData() {  
        //read data base  
        $ticketsRead = unserialize(file_get_contents(DB_FILE_NAME));  
        foreach ($this->_tickets as $id => $value) {  
            if(isset($ticketsRead[$id])) {  
                $this->_tickets[$id] = $ticketsRead[$id];  
            }  
        }  
    }  
  
    public function saveData() {  
        //read data base  
        file_put_contents(DB_FILE_NAME, serialize($this->_tickets));  
    }  
    protected function _init() {  
        // writing config here  
        $this->_rows = ROWS;  
        $this->_seatsPerRow = SEATS_PER_ROW;  
        $this->_categories = $GLOBALS["categories"]; // former: array("Parkett","Rang","Empore");  
        // setting unique key and assign categories  
        $this->_createTickets();  
    }  
  
    public function getTickets() {  
        return $this->_tickets;  
    }  
}  
  
class TicketsystemController  
{  
    private $_model;  
    public function __construct($model, $view) {  
       $this->_model = $model;  
       $this->_view = $view;  
       $this->_view->tickets = $this->_model->getTickets();  
    }  
    public function index() {  
        $this->_view->message = "Index";  
    }  
    public function read() {  
    }  
    public function clear() {  
        foreach ($this->_view->tickets as $id => $value) {  
            $clear[$id] = "frei";  
        }  
        $this->_model->setStatus($clear);  
        $this->_model->saveData();  
        $this->_view->tickets = $this->_model->getTickets();  
    }  
    public function save() {  
        $this->_model->setStatus($_POST);  
        $this->_model->saveData();  
       $this->_view->tickets = $this->_model->getTickets();  
    }  
    public function __call($name, $arguments) {  
       $this->_view->message = "no action " . $name ;  
    }  
}  
  
class View_Ticketsystem  
{  
  public function __get($name) {  
        return $this->$name;  
  }  
  public function __set($name, $value) {  
        $this->$name = $value;  
  }  
}  
  
class Dispatcher_Ticketsystem  
{  
    private static $_instance = Null;  
    public function __construct() {  
        $this->_model = new Model_Ticketsystem;  
        $this->_view = new View_Ticketsystem;  
        $controller = new TicketsystemController($this->_model, $this->_view);  
        call_user_method($this->_route(), $controller);  
    }  
  
    private function _route() {  
        if (! isset($_GET["action"])) {  
            return "index";  
        }  
        return $_GET["action"];  
    }  
    public static function dispatch() {  
        self::$_instance = new self();  
        return self::$_instance;  
    }  
    public function getView() {  
        return $this->_view;  
    }  
}  
// database  
define ("DB_FILE_NAME","testtickets.ser.php");  
// for this purpose define rows and seats and categories  
define ("ROWS",9);  
define("SEATS_PER_ROW",14);  
$GLOBALS["categories"] = array("Parkett","Rang","Empore");  
// setup db if on first run  
if(!file_exists(DB_FILE_NAME)) {  
    file_put_contents(DB_FILE_NAME, serialize(array()));  
}  
$view = Dispatcher_Ticketsystem::dispatch()->getView();  
?><html>  
<head>  
<style type="text/css">  
.message {border:1px solid red}  
table {border: 1px solid brown}  
.Parkett {border: 2px solid green; background-color: green;}  
.Rang {border: 2px solid blue; background-color: blue;}  
.Empore {border: 2px solid pink; background-color: pink;}  
</style>  
</head>  
<body>  
<p class="menu">  
<?php foreach (array("read","index","clear") as $link):?>  
[<a href="?action=<?=$link?>"><?=$link?></a> ]  
<?php endforeach?>  
</p>  
<h1>Ticketsystem</h1>  
<?php switch($_GET["action"]):  
default:?>  
<p class="message">Message: <?=$view->message?></p>  
<?php case "index":?>  
<h2>index</h2>  
<?php break;?>  
<?php case "save":?>  
<p>data should be saved:</p>  
<pre>  
<?php var_dump($_POST)?>  
</pre>  
<?php case "clear"?>  
<p>data cleared</p>  
<?php case "read":?>  
<h2>reading data</h2>  
<form action="?action=save" method="post">  
<table>  
<tr>  
<?php foreach ($view->tickets as $id => $ticket):?>  
<?php if ($ticket["status"] == "on") {  
    $checked = 'checked="checked" disabled="disabled"';  
} else {  
    $checked = "";  
}  
?>  
<?php if ($ticket["row"] > $prevRow):?>  
</tr><tr>  
<?php $prevRow = $ticket["row"]?>  
<?php endif?>  
<td class="<?=$ticket["category"]?>">  
<label><?=$id?></label>  
<input name="<?=$id?>" type="checkbox" <?=$checked;?>>  
</td>  
<?php endforeach?>  
</tr>  
</table>  
<input type="submit">  
</form>  
<p>  
<?php foreach ($GLOBALS["categories"] as $category):?>  
[<span class="<?=$category?>"><?=$category?></span>]  
<?php endforeach;?>  
<?php case "clear"?>  
<pre><?php var_dump($view->tickets)?></pre>  
<?php break;?>  
<?php endswitch;?>  
</body>  

Dank und Gruß,

frankx