Created
October 8, 2012 02:22
-
-
Save BaylorRae/3850401 to your computer and use it in GitHub Desktop.
Source code for: http://baylorrae.com/the-basics-of-creating-a-cms-with-php/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="POST"> | |
<div class="field"> | |
<label for="url">URL:</label> | |
<input type="text" for="url" name="website[url]" value="<?php echo $website->url ?>" /> | |
</div> | |
<div class="field"> | |
<label for="title">Title:</label> | |
<input type="text" for="title" name="website[title]" value="<?php echo $website->title ?>" /> | |
</div> | |
<div class="field"> | |
<label for="author">Author:</label> | |
<input type="text" for="author" name="website[author]" value="<?php echo $website->author ?>" /> | |
</div> | |
<div class="field"> | |
<input type="submit" name="save_website" /> | |
</div> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // this goes in index.php ?> | |
<li> | |
<!-- output: <a href="__URL__">__TITLE</a> by: __AUTHOR__ --> | |
<a href="<?php echo $website->url ?>"><?php echo $website->title ?></a> | |
from: <?php echo $website->author ?> | | |
<a href="edit.php?id=<?php echo $website->id ?>">Edit</a> | |
</li> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* edit.php | |
*/ | |
include 'global.php'; | |
include 'models/website.php'; | |
// the id wasn't passed | |
if( !isset($_GET['id']) ) | |
header("Location: index.php"); | |
// the form wasn't submitted | |
// then find the current website | |
if( !isset($_POST['save_website']) ) { | |
// prepare the query to find the website by id | |
$stmt = $dbh->prepare("SELECT `id`, `url`, `title`, `author` FROM `websites` WHERE `id` = :id"); | |
// execute the query with the current id | |
$stmt->execute(array( | |
'id' => $_GET['id'] | |
)); | |
// set the website variable for the form | |
$website = $stmt->fetchObject('Website'); | |
}else { // the form was submitted | |
// prepare the query to update the website's information | |
$stmt = $dbh->prepare("UPDATE `websites` SET `url` = :url, `title` = :title, `author` = :author WHERE `id` = :id"); | |
// try to update the current website | |
if( $stmt->execute($_POST['website'] + array('id' => $_GET['id'])) ) { | |
header("Location: index.php"); | |
}else { | |
echo "<p>Failed to update the website</p>"; | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>My Favorite Website</title> | |
</head> | |
<body> | |
<?php include '_form.php' ?> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
├── global.php // will store our connection to the database | |
├── index.php // will list our favorite websites | |
└── models/ | |
└── website.php // will be a class to store each website into an object | |
1 directory, 3 files |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* global.php | |
*/ | |
// tries to open a mysql connection | |
try { | |
$dbh = new PDO('mysql:host=localhost;dbname=cmsTutorial', 'user', 'pass'); | |
}catch( PDOException $e ) { | |
echo $e->getMessage(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* index.php | |
*/ | |
include 'global.php'; | |
/********************* | |
* Remove this line and below | |
* once you've loaded the page | |
*********************/ | |
// Prepare our SQL query | |
$stmt = $dbh->prepare("INSERT INTO `websites` (`url`, `title`, `author`) VALUES (:url, :title, :author)"); | |
// An array of items to insert | |
// notice how the keys reflect our query's [:url, :title, :author] | |
$websites = array( | |
array( | |
'url' => 'http://css-tricks.com', | |
'title' => 'CSS-Tricks', | |
'author' => 'Chris Coyier' | |
), | |
array( | |
'url' => 'http://net.tutsplus.com', | |
'title' => 'NetTuts+', | |
'author' => 'Jeffrey Way' | |
) | |
); | |
// loop through each website and | |
// execute the query with the website data | |
foreach( $websites as $website ) { | |
if( !$stmt->execute($website) ) { | |
echo '<pre>', print_r($stmt->errorInfo(), true), '</pre>'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* index.php | |
*/ | |
include 'global.php'; | |
// this will give us access to the Website class | |
include 'models/website.php'; | |
// this time we'll run PDO::query because the SQL is static | |
$stmt = $dbh->query("SELECT `id`, `url`, `title`, `author` FROM `websites`"); | |
// get all websites through our Website class | |
$websites = $stmt->fetchAll(PDO::FETCH_CLASS, 'Website'); | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>My Favorite Website</title> | |
</head> | |
<body> | |
<ol> | |
<!-- loop through each website --> | |
<?php foreach( $websites as $website ) : ?> | |
<li> | |
<!-- output: <a href="__URL__">__TITLE__</a> by: __AUTHOR__ --> | |
<a href="<?php echo $website->url ?>"><?php echo $website->title ?></a> | |
from: <?php echo $website->author ?> | |
</li> | |
<?php endforeach; ?> | |
</ol> | |
<a href="new.php">New Website</a> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* models/website.php | |
*/ | |
class Website { | |
// for our own reference of what's available | |
public $url, | |
$title, | |
$author; | |
// allows us to easily set attributes | |
// on the class | |
function __construct(array $data = null) { | |
if( empty($data) ) | |
return; | |
foreach( $data as $field => $value ) { | |
$this->$field = $value; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* models/website.php | |
*/ | |
class Website { | |
// for our own reference of what's available | |
public $url, | |
$title, | |
$author; | |
// allows us to easily set attributes | |
// on the class | |
function __construct(array $data = null) { | |
if( empty($data) ) | |
return; | |
foreach( $data as $field => $value ) { | |
$this->$field = $value; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* new.php | |
*/ | |
include 'global.php'; | |
include 'models/website.php'; | |
// check if the form was submitted | |
if( isset($_POST['save_website']) ) { | |
// create the website object | |
$website = new Website($_POST['website']); | |
// prepare an SQL query | |
$stmt = $dbh->prepare("INSERT INTO `websites` (`url`, `title`, `author`) VALUES (:url, :title, :author)"); | |
// run the SQL query | |
if( $stmt->execute(array( | |
'url' => $website->url, | |
'title' => $website->title, | |
'author' => $website->author | |
)) | |
) { | |
// if successful then go back to home page | |
header("Location: index.php"); | |
}else { | |
// display an error if it failed | |
echo "<p>failed to add website</p>"; | |
} | |
}else { | |
// create the variabel so that we have access to it in our form | |
$website = new Website; | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>My Favorite Website</title> | |
</head> | |
<body> | |
<?php include '_form.php' ?> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `cmsTutorial`.`websites` ( | |
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , | |
`url` VARCHAR( 150 ) NOT NULL , | |
`title` VARCHAR( 100 ) NOT NULL , | |
`author` VARCHAR( 75 ) NOT NULL | |
) ENGINE = INNODB; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment