CMS falsch gesetzt

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • CMS falsch gesetzt

    Das kann im Moment noch warten ich habe ein anderes Problem mit einem CMS. I-was habe ich falsch gestezt etc. Ist nur ein Punkt oder so was, denn ch nicht finde.

    Code:

    Quellcode

    1. class modernCMS {
    2. var $host;
    3. var $username;
    4. var $password;
    5. var $db;
    6. function connect() {
    7. $con = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
    8. mysql_select_db($this->db, $con) or die(mysql_error());
    9. }
    10. function get_content($id = '') {
    11. if($id != ""):
    12. $id = mysql_real_escape_string($id);
    13. $sql = "SELECT * FROM modernCMS WHERE id = '$id'";
    14. $return = '<p><a href="index.php">Go Back to content</a></p>';
    15. else:
    16. $sql = "SELECT * FROM modernCMS ORDER BY id DESC";
    17. endif;
    18. $res = mysql_query($sql) or die(mysql_error());
    19. if(mysql_num_rows($res) != 0):
    20. while($row = mysql_fetch_assoc($res)) {
    21. echo '<h1><a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a></h1>';
    22. echo '<p>' . $row['body'] . '</p>';
    23. }
    24. else:
    25. echo '<p>Uh oh, this doesn\'t exist!</p>';
    26. endif;
    27. echo $return;
    28. }
    29. function add_content($p) {
    30. $title = mysql_real_escape_string($p['title']);
    31. $body = mysql_real_escape_string($p['body']);
    32. if(!$title || !$body):
    33. if(!title):
    34. echo "<p>The Title is Required</p>";
    35. endif;
    36. if(!body):
    37. echo "<p>The Body is Required</p>";
    38. endif;
    39. echo '<p><a href="add-content.php">Try Again!</a></p>';
    40. else:
    41. $sql = "INSERT INTO modernCMS VALUES (null, '$title', '$body')";
    42. $res = mysql_query($sql) or die(mysql_error());
    43. echo "Added Successfully!";
    44. endif;
    45. }
    46. function manage_content {
    47. echo '<div id="manage">';
    48. $sql = "SELECT * FROM modernCMS";
    49. $res = mysql_query($sql) or die(mysql_error());
    50. while($row = mysql_fetch_assoc($res)) :
    51. ?>
    52. <div>
    53. <h2 class="title"><?=$row['title']?></h2>
    54. <span class="actions"><a href="">Edit</a> | <a href="?delete=<?=$row['id'];?>">Delete</a></span>
    55. </div>
    56. <?php
    57. endwhile;
    58. echo '</div>'; // Closes Manage Div
    59. }
    60. function delete_content($id) {
    61. if(!$id) {
    62. return false;
    63. }else {
    64. $id = mysql_real_escape_string($id);
    65. $sql = "DELETE FROM modernCMS WHERE id = '$id'";
    66. $res = mysql_query($sql) or die(mysql_error());
    67. echo "Content Deleted Succesfully!";
    68. }
    69. }
    70. } // End the Class
    Alles anzeigen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Torben Brodt () aus folgendem Grund: syntax

  • Was ist den Dein eigentliches Problem bei der Klasse?

    Ich würde Dir jedoch gerne einen Vorschlag bei deinem Konzept geben. Du öffnest deine Datenbankverbindung direkt in der Klasse. Ich würde das ganze in eine eigene Klasse auslagern. Beispiel:

    Quellcode

    1. <?php
    2. class DatabaseException extends Exception {
    3. /**
    4. * constructor
    5. *
    6. * @param string $message[optional]
    7. * @param int $code[optional]
    8. */
    9. public function __construct($message='', $code=0) {
    10. parent::__construct($message, $code);
    11. die("<pre>".(string) $this."</pre>");
    12. }
    13. }
    14. class DatabaseConnectionException extends DatabaseException { }
    15. class DatabaseQueryException extends DatabaseException { }
    16. class Database {
    17. /**
    18. * database object
    19. *
    20. * @var Database $singleton
    21. */
    22. private static $singleton;
    23. /**
    24. * singleton pattern
    25. *
    26. * @return Database
    27. */
    28. public static function getInstance() {
    29. if(!is_object(self::$singleton) || !(self::$singleton instanceof Database)) {
    30. self::$singleton = new Database('localhost', 'root', '', 'test');
    31. }
    32. return self::$singleton;
    33. }
    34. /**
    35. * database connection configuration
    36. *
    37. * @var array $config
    38. */
    39. protected $config = array();
    40. /**
    41. * mysql connection
    42. *
    43. * @var ressource $connection
    44. */
    45. protected $connection;
    46. /**
    47. * constructor
    48. *
    49. * @param string $host
    50. * @param string $username
    51. * @param string $password
    52. * @param string $database
    53. * @param bool $autoConnect[optional]
    54. */
    55. public function __construct($host, $username, $password, $database, $autoConnect=true) {
    56. $this->config['host'] = $host;
    57. $this->config['username'] = $username;
    58. $this->config['password'] = $password;
    59. $this->config['database'] = $database;
    60. // connect automaticly
    61. if($autoConnect) {
    62. $this->connect($host, $username, $password);
    63. $this->selectDatabase($database);
    64. }
    65. }
    66. /**
    67. * destructor
    68. *
    69. */
    70. public function __destruct() {
    71. $this->close();
    72. }
    73. /**
    74. * mysql connect
    75. *
    76. * @param string $host
    77. * @param string $username
    78. * @param string $password
    79. * @return resource
    80. */
    81. public function connect($host, $username, $password) {
    82. $this->connection = mysql_connect($host, $username, $password);
    83. if(!$this->connection) {
    84. throw new DatabaseConnectionException;
    85. }
    86. else return $this->connection;
    87. }
    88. /**
    89. * mysql close
    90. *
    91. */
    92. public function close() {
    93. if($this->connection) {
    94. mysql_close($this->connection);
    95. }
    96. }
    97. /**
    98. * mysql select database
    99. *
    100. * @param string $database
    101. * @return bool
    102. */
    103. public function selectDatabase($database) {
    104. if(!$this->connection) {
    105. throw new DatabaseConnectionException;
    106. }
    107. if(mysql_select_db($database, $this->connection)) {
    108. return true;
    109. }
    110. else throw new DatabaseException;
    111. }
    112. /**
    113. * mysql query
    114. *
    115. * @param string $query
    116. * @return resource
    117. */
    118. public function query($query) {
    119. if(!$this->connection) {
    120. throw new DatabaseConnectionException;
    121. }
    122. $result = mysql_query($query, $this->connection);
    123. if($result) {
    124. return $result;
    125. }
    126. else throw new DatabaseQueryException;
    127. }
    128. /**
    129. * mysql fetch
    130. *
    131. * @param resource $result
    132. * @return array
    133. */
    134. public function fetch($result) {
    135. if(!is_resource($result)) {
    136. throw new DatabaseQueryConnection;
    137. }
    138. return mysql_fetch_assoc($result);
    139. }
    140. }
    141. ?>
    Alles anzeigen


    Beispiel

    Quellcode

    1. $result = Database::getInstance()->query("SELECT * FROM test");
    2. while($row = Database::getInstace()->fetch($result)) { }


    Natürlich kann man das ganze noch schöner machen und auch um einiges erweitern, habs jetzt aber nur mal schnell als Gedankenanreiz runtergetippt.