Nej det kan jeg egentlig godt se, en skrivefejl :P
Men er det ellers korrekt/normalt at have al HTML, samt data-håndtering statisk inde i klassen ?
Prøver lige at smide hele koden til klassen ind, så du kan se hvordan jeg har opbygget den, samt hvad der er statisk, og hvad der ikke er (fungerende kode):
class pagetext
{
# Variables
var $conn;
var $table;
var $url;
var $width;
var $html;
# Functions
# Set the connection-string
function set_conn($conn)
{
$this->conn = $conn;
}
# Set the database-table to be used
function set_table($table)
{
$this->table = $table;
}
# Set the url from where the script is run
function set_url($url)
{
$this->url = (strpos($url, '?') !== false) ? $url.'&' : $url.'?';
}
# Set the width of the table design
function set_width($width)
{
$this->width = $width;
}
# Check if database-table exists
function check_db_table()
{
if (!mysqli_query($this->conn, 'select page_id from '.$this->table.''))
{
return false;
}
else
{
return true;
}
}
# Create the database-table
function create_db_table()
{
$query = 'create table `'.$this->table.'` ('.
'`page_id` int(11) NOT NULL auto_increment,'.
'`page_name` varchar(200) default NULL,'.
'`page_type` varchar(50) default NULL,'.
'`page_status` varchar(50) default NULL,'.
'`page_content` longtext,'.
'`page_keywords` longtext,'.
'`page_description` longtext,'.
'`page_hits` int(11) default NULL,'.
'PRIMARY KEY (`page_id`))';
echo '
<h6>Pagetext - Create table in database</h6>
<br />';
if (mysqli_query($this->conn, $query))
{
echo '
<div class="nb_box_green">
Table created successfully
</div>';
}
else
{
echo '
<div class="nb_box_orange">
An error occured
<br /><br />
MySQL error-report:<br />
'.mysqli_error($this->conn).'
</div>';
}
echo '
<br />
<a href="'.$this->url.'">Click here to return to your page</a>';
}
# Delete the database-table
function delete_db_table()
{
$query = 'drop table if exists `'.$this->table.'`';
if (mysqli_query($this->conn, $query))
{
echo '
<h6>Pagetext - Delete table from database</h6>
<br />
<div class="nb_box_green">
Table deleted successfully
</div>';
}
else
{
echo '
<h6>Pagetext - Delete table from database</h6>
<br />
<div class="nb_box_orange">
An error occured
<br /><br />
MySQL error-report:<br />
'.mysqli_error($this->conn).'
</div>';
}
}
# Overview of pages (controller function)
function pagetext_overview()
{
if (!isset($_GET["action"]))
{
if (!$this->check_db_table())
{
echo '
<h6>Pagetext - Error</h6>
<br />
<div class="nb_box_orange">
The table "'.$this->table.'" was not found in the database.<br /><br />
<center>
Would you like to create it now ?<br />
<button class="form_submit" onclick="java script:window.location.href=\''.$this->url.'action=db_create_table\';">Yes, create the table now</button>
</center>
</div>';
}
else
{
$all_pages = mysqli_query($this->conn, 'select * from '.$this->table.'');
$this->html = '
<h6>Pagetext - Overview</h6>
<br />
<table width="'.$this->width.'" border="0" cellspacing="0" cellpadding="2">
<tr>
<td colspan="6">
<p>
Here you can see all the pages you currently have.<br />
If you want to create a new page, use the link below:<br />
<a href="'.$this->url.'action=new">Create new page</a>
</p>
<br />
</td>
</tr>
<tr>
<td align="left" style="border-bottom:1px solid #999999;">
<p>Id</p>
</td>
<td align="left" style="border-bottom:1px solid #999999;">
<p>Name</p>
</td>
<td align="left" style="border-bottom:1px solid #999999;">
<p>Type</p>
</td>
<td align="right" style="border-bottom:1px solid #999999;">
<p>Status</p>
</td>
<td align="right" style="border-bottom:1px solid #999999;">
<p>Hits</p>
</td>
<td align="right" style="border-bottom:1px solid #999999;">
<p>Actions</p>
</td>
</tr>';
if (mysqli_num_rows($all_pages) < 1)
{
$this->html.= '
<tr onmouseover="java script:this.bgColor=\'#E6E6E6\';" onmouseout="java script:this.bgColor=\'\';">
<td align="left" colspan="6" style="border-bottom:1px solid #CCCCCC;">
<p class="nb_red">No pages were found</p>
</td>
</tr>';
}
while ($page = mysqli_fetch_assoc($all_pages))
{
$this->html.= '
<tr onmouseover="java script:this.bgColor=\'#E6E6E6\';" onmouseout="java script:this.bgColor=\'\';">
<td align="left" style="border-bottom:1px solid #CCCCCC;">
<p>'.$page['page_id'].'</p>
</td>
<td align="left" style="border-bottom:1px solid #CCCCCC;">
<p>'.$page['page_name'].'</p>
</td>
<td align="left" style="border-bottom:1px solid #CCCCCC;">
<p>'.$page['page_type'].'</p>
</td>
<td align="right" style="border-bottom:1px solid #CCCCCC;">
<p>'.$page['page_status'].'</p>
</td>
<td align="right" style="border-bottom:1px solid #CCCCCC;">
<p>'.$page['page_hits'].'</p>
</td>
<td align="right" style="border-bottom:1px solid #CCCCCC;">
<a href="'.$this->url.'action=edit&id='.$page['page_id'].'">edit</a>
<br />
<a href="'.$this->url.'action=delete&id='.$page['page_id'].'">delete</a>
</td>
</tr>';
}
$this->html.= '</table>';
}
}
else
{
switch ($_GET["action"])
{
case "new":
$this->pagetext_new();
break;
case "edit":
$this->pagetext_edit($_GET["id"]);
break;
case "delete":
$this->pagetext_delete($_GET["id"]);
break;
case "db_create_table":
$this->create_db_table();
break;
}
}
}
# Create new page
function pagetext_new()
{
if (isset($_POST['save']))
{
$name = $_POST['page_name'];
$type = $_POST['page_type'];
$status = $_POST['page_status'];
$content = $_POST['page_content'];
$keywords = $_POST['page_keywords'];
$description = $_POST['page_description'];
$errors = array();
if (empty($name))
{
$errors[] = 'Enter a name';
}
if (empty($type))
{
$errors[] = 'Select a type';
}
if (empty($status))
{
$errors[] = 'Select a status';
}
if (empty($content))
{
$errors[] = 'Enter some content';
}
if (sizeof($errors) > 0)
{
$nb = '<div class="nb_box_red">
Correct the following errors:
<ul>';
foreach ($errors as $error)
{
$nb.= '<li>'.$error.'</li>';
}
$nb.= '</ul>
</div>';
}
else
{
if (mysqli_query($this->conn, 'insert into '.$this->table.' (page_name,page_type,page_status,page_content,page_keywords,page_description,page_hits) values ("'.$name.'","'.$type.'","'.$status.'","'.$content.'","'.$keywords.'","'.$description.'",0)'))
{
$nb = '<div class="nb_box_green">
The page has been created
</div>';
unset($name);
unset($type);
unset($status);
unset($content);
unset($keywords);
unset($description);
}
else
{
$nb = '<div class="nb_box_orange">
An error occured !
</div>';
}
}
}
if (isset($_POST["cancel"]))
{
header('location: '.$this->url.'');
}
$main_pages = mysqli_query($this->conn, 'select page_name from '.$this->table.' where page_type="Main page"');
$type_list = '
<option value="Main page" '.((@$type == 'Main page') ? 'selected="selected"' : '').'>Main page</option>
<option value="">---------</option>';
while ($main_page = mysqli_fetch_assoc($main_pages))
{
$type_list.= '<option value="Subpage -> '.$main_page["page_name"].'" '.((@$type == 'Subpage -> '.$main_page["page_name"]) ? 'selected="selected"' : '').'>Subpage -> '.$main_page["page_name"].'</option>';
}
$this->html = '
<h6>Pagetext - Create</h6>
<br />
<form method="post" action="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'">
<table width="'.$this->width.'" border="0" cellspacing="0" cellpadding="2">
<tr>
<td align="right" colspan="3">
'.@$nb.'
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page name</p>
</td>
<td width="10"></td>
<td align="right" valign="top">
<input type="text" name="page_name" value="'.@$name.'" class="form_text" />
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page type</p>
</td>
<td></td>
<td align="right" valign="top">
<select size="1" name="page_type" class="form_select">
'.$type_list.'
</select>
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page status</p>
</td>
<td></td>
<td align="right" valign="top">
<p>Active <input type="radio" name="page_status" value="active" '.((@$status == 'active' || !isset($status)) ? 'checked="checked"' : '').' class="form_radio" /></p>
<p>In-active <input type="radio" name="page_status" value="inactive" '.((@$status == 'inactive') ? 'checked="checked"' : '').' class="form_radio" /></p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page content</p>
</td>
<td></td>
<td align="right" valign="top">
<textarea name="page_content" id="editor" class="form_textarea">'.@$content.'</textarea>
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Meta-keywords</p>
</td>
<td></td>
<td align="right" valign="top">
<textarea name="page_keywords" class="form_textarea">'.@$keywords.'</textarea>
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Meta-description</p>
</td>
<td></td>
<td align="right" valign="top">
<textarea name="page_description" class="form_textarea">'.@$description.'</textarea>
</td>
</tr>
<tr>
<td align="right" colspan="3">
<br />
<button type="submit" name="save" class="form_submit_green"><div class="form_submit_ok">Save</div></button>
<button type="submit" name="cancel" class="form_submit_red"><div class="form_submit_cancel">Cancel</div></button>
</td>
</tr>
</table>
</form>';
}
# Edit page
function pagetext_edit($id)
{
if (isset($_POST['save']))
{
$name = $_POST['page_name'];
$type = $_POST['page_type'];
$status = $_POST['page_status'];
$content = $_POST['page_content'];
$keywords = $_POST['page_keywords'];
$description = $_POST['page_description'];
$errors = array();
if (empty($name))
{
$errors[] = 'Enter a name';
}
if (empty($type))
{
$errors[] = 'Select a type';
}
if (empty($status))
{
$errors[] = 'Select a status';
}
if (empty($content))
{
$errors[] = 'Enter some content';
}
if (sizeof($errors) > 0)
{
$nb = '<div class="nb_box_red">
Correct the following errors:
<ul>';
foreach ($errors as $error)
{
$nb.= '<li>'.$error.'</li>';
}
$nb.= '</ul>
</div>';
}
else
{
if (mysqli_query($this->conn, 'update '.$this->table.' set page_name="'.$name.'",page_type="'.$type.'",page_status="'.$status.'",page_content="'.$content.'",page_keywords="'.$keywords.'",page_description="'.$description.'" where page_id='.$id.''))
{
$nb = '<div class="nb_box_green">
The page has been edited
</div>';
}
else
{
$nb = '<div class="nb_box_orange">
An error occured !
</div>';
}
}
}
if (isset($_POST["cancel"]))
{
header('location: '.$this->url.'');
}
$old_data = mysqli_query($this->conn, 'select * from '.$this->table.' where page_id='.$id.'');
$old_data = mysqli_fetch_assoc($old_data);
$main_pages = mysqli_query($this->conn, 'select page_name from '.$this->table.' where page_type="main"');
$type_list = '
<option value="Main page" '.(($old_data["page_type"] == 'Main page') ? 'selected="selected"' : '').'>Main page</option>
<option value="">---------</option>';
while ($main_page = mysqli_fetch_assoc($main_pages))
{
$type_list.= '<option value="Subpage -> '.$main_page["page_name"].'" '.(($old_data["page_type"] == 'Subpage -> '.$main_page["page_name"]) ? 'selected="selected"' : '').'>Subpage -> '.$main_page["page_name"].'</option>';
}
$this->html = '
<h6>Pagetext - Edit</h6>
<br />
<form method="post" action="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'">
<table width="'.$this->width.'" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="right" colspan="3">
'.@$nb.'
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page name</p>
</td>
<td width="10"></td>
<td align="right" valign="top">
<input type="text" name="page_name" value="'.$old_data["page_name"].'" class="form_text" />
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page type</p>
</td>
<td></td>
<td align="right" valign="top">
<select size="1" name="page_type" class="form_select">
'.$type_list.'
</select>
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page status</p>
</td>
<td></td>
<td align="right" valign="top">
<p>Active <input type="radio" name="page_status" value="active" '.(($old_data["page_status"] == 'active') ? 'checked="checked"' : '').' class="form_radio" /></p>
<p>In-active <input type="radio" name="page_status" value="inactive" '.(($old_data["page_status"] == 'inactive') ? 'checked="checked"' : '').' class="form_radio" /></p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page content</p>
</td>
<td></td>
<td align="right" valign="top">
<textarea name="page_content" id="editor" class="form_textarea">'.$old_data["page_content"].'</textarea>
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Meta-keywords</p>
</td>
<td></td>
<td align="right" valign="top">
<textarea name="page_keywords" class="form_textarea">'.$old_data["page_keywords"].'</textarea>
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Meta-description</p>
</td>
<td></td>
<td align="right" valign="top">
<textarea name="page_description" class="form_textarea">'.$old_data["page_description"].'</textarea>
</td>
</tr>
<tr>
<td align="right" colspan="3">
<br />
<button type="submit" name="save" class="form_submit_green"><div class="form_submit_ok">Save</div></button>
<button type="submit" name="cancel" class="form_submit_red"><div class="form_submit_cancel">Cancel</div></button>
</td>
</tr>
</table>
</form>';
}
# Delete page
function pagetext_delete($id)
{
if (isset($_POST['delete']))
{
if (mysqli_query($this->conn, 'delete from '.$this->table.' where page_id='.$id.''))
{
$nb = '<div class="nb_box_green">
The page has been delete
</div>';
}
else
{
$nb = '<div class="nb_box_orange">
An error occured !
</div>';
}
}
if (isset($_POST["cancel"]))
{
header('location: '.$this->url.'');
}
$old_data = mysqli_query($this->conn, 'select * from '.$this->table.' where page_id='.$id.'');
$old_data = mysqli_fetch_assoc($old_data);
$this->html = '
<h6>Pagetext - Edit</h6>
<br />
<form method="post" action="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'">
<table width="'.$this->width.'" border="0" cellspacing="0" cellpadding="2">
<tr>
<td align="right" colspan="3">
'.@$nb.'
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page name</p>
</td>
<td width="10"></td>
<td align="right" valign="top">
<p>'.(($old_data["page_name"]) ? $old_data["page_name"] : '------').'</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page type</p>
</td>
<td width="10"></td>
<td align="right" valign="top">
<p>'.(($old_data["page_type"]) ? $old_data["page_type"] : '------').'</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page status</p>
</td>
<td width="10"></td>
<td align="right" valign="top">
<p>'.(($old_data["page_status"]) ? $old_data["page_status"] : '------').'</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<p>Page hits</p>
</td>
<td width="10"></td>
<td align="right" valign="top">
<p>'.((is_numeric($old_data["page_hits"])) ? $old_data["page_hits"] : '------').'</p>
</td>
</tr>
<tr>
<td align="right" colspan="3">
<br />
<button type="submit" name="delete" class="form_submit_green" '.(($old_data["page_name"]) ? '' : 'disabled="disabled"').'><div class="form_submit_ok">Delete</div></button>
<button type="submit" name="cancel" class="form_submit_red"><div class="form_submit_cancel">Cancel</div></button>
</td>
</tr>
</table>
</form>';
}
# Display HTML output
function display_html()
{
echo $this->html;
}
}
Bruger den således:
<?php
# Include pagetext-class file
include('pagetext.php');
# Make a new instance of the class
$pagetext = new pagetext;
# Connection-string to your database
$pagetext->set_conn(mysqli_connect('localhost','root','1234','classes'));
# Table name for the table to contain the pages
$pagetext->set_table('pagetext');
# URL to the file the system is to run from
$pagetext->set_url('demo.php');
# Width of the table-design
$pagetext->set_width(450);
# Initiate
$pagetext->pagetext_overview();
?>
<!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=utf-8" />
<title>Demo - PagetextClass</title>
<link rel="stylesheet" type="text/css" href="../_style/style.css" />
</head>
<body>
<?php
# Display HTML
$pagetext->display_html();
?>
</body>
</html>
Beklager al koden, men jeg bryder mig virkelig ikke selv om at se andre spørgsmål/svar med links til kode et andet sted, som for længst er flyttet/fjernet o.l.
/xicrow