More Related Content Similar to Intro to #memtech PHP 2011-12-05 Similar to Intro to #memtech PHP 2011-12-05 (20) More from Jeremy Kendall (9) Intro to #memtech PHP 2011-12-0513. . . . you can be coding in about 5 minutes 14. Why Did You (or why do you want to) Start Programming? 34. index.php – db connection <?php $db = realpath ( dirname ( __FILE__ ) . '/data/db/bookshelf.db' ); $dsn = "sqlite: $db " ; $options = array ( PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION , PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC ); try { $dbh = new PDO ( $dsn , null , null , $options ); } catch ( PDOException $e ) { echo "Error!: " . $e -> getMessage() . "<br /> " ; die (); } $books = $dbh -> query( " SELECT * FROM bookshelf ORDER BY title" ) -> fetchAll(); 35. index.php – db connection <?php $db = realpath ( dirname ( __FILE__ ) . '/data/db/bookshelf.db' ); $dsn = "sqlite: $db " ; $options = array ( PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION , PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC ); try { $dbh = new PDO ($dsn, null , null , $options); } catch ( PDOException $e) { echo "Error!: " . $e -> getMessage() . "<br />"; die (); } $books = $dbh -> query(" SELECT * FROM bookshelf ORDER BY title") -> fetchAll() ; 36. index.php – db connection <?php $db = realpath ( dirname ( __FILE__ ) . '/data/db/bookshelf.db'); $dsn = "sqlite:$db"; $options = array ( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); try { $dbh = new PDO ( $dsn , null , null , $options ); } catch ( PDOException $e ) { echo "Error!: " . $e -> getMessage() . "<br /> " ; die (); } $books = $dbh -> query(" SELECT * FROM bookshelf ORDER BY title") -> fetchAll(); 37. index.php – db connection <?php $db = realpath ( dirname ( __FILE__ ) . '/data/db/bookshelf.db'); $dsn = "sqlite:$db"; $options = array ( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); try { $dbh = new PDO ($dsn, null , null , $options); } catch ( PDOException $e) { echo "Error!: " . $e -> getMessage() . "<br />"; die (); } $books = $dbh -> query( " SELECT * FROM bookshelf ORDER BY title" ) -> fetchAll(); 38. index.php – books table <?php if ( count ( $books ) > 0 ): ?> < table > < tr > < th > Title </ th >< th > Author </ th > </ tr > <?php foreach ( $books as $book ): ?> < tr > < td > < a href= "book-form.php?id=<?php echo $book ['id']; ?>" > <?php echo $book [ 'title' ]; ?> </ a > </ td > < td > <?php echo $book [ 'author' ]; ?> </ td > </ tr > <?php endforeach ; ?> </ table > <?php else : ?> < p > We have no books! </ p > <?php endif ; ?> 39. index.php – books table <?php if ( count ( $books ) > 0 ): ?> < table > < tr > < th > Title </ th >< th > Author </ th > </ tr > <?php foreach ( $books as $book ): ?> < tr > < td > < a href= "book-form.php?id=<?php echo $book ['id']; ?>" > <?php echo $book [ 'title' ]; ?> </ a > </ td > < td > <?php echo $book [ 'author' ]; ?> </ td > </ tr > <?php endforeach ; ?> </ table > <?php else : ?> < p >We have no books!</ p > <?php endif ; ?> 40. index.php – books table <?php if ( count ($books) > 0): ?> < table > < tr > < th >Title</ th >< th >Author</ th > </ tr > <?php foreach ( $books as $book ): ?> < tr > < td > < a href= "book-form.php?id=<?php echo $book ['id']; ?>" > <?php echo $book [ 'title' ]; ?> </ a > </ td > < td > <?php echo $book [ 'author' ]; ?> </ td > </ tr > <?php endforeach ; ?> </ table > <?php else : ?> < p >We have no books!</ p > <?php endif ; ?> 41. index.php – books table <?php if ( count ($books) > 0): ?> < table > < tr > < th >Title</ th >< th >Author</ th > </ tr > <?php foreach ($books as $book): ?> < tr > < td > < a href="book-form.php?id=<?php echo $book['id']; ?>"> <?php echo $book['title']; ?> </ a > </ td > < td > <?php echo $book['author']; ?> </ td > </ tr > <?php endforeach ; ?> </ table > <?php else : ?> < p > We have no books! </ p > <?php endif ; ?> 42. book-form.php <?php $id = empty ( $_GET [ 'id' ]) ? null : $_GET [ 'id' ]; if ( $id ) { // Database connection code $book = $dbh -> query( " SELECT title, author FROM bookshelf WHERE id = $id " ) -> fetch(); $title = $book [ 'title' ]; $author = $book [ 'author' ]; } 43. book-form.php <?php $id = empty ( $_GET [ 'id' ]) ? null : $_GET [ 'id' ]; if ($id) { // Database connection code $book = $dbh -> query(" SELECT title, author FROM bookshelf WHERE id = $id") -> fetch(); $title = $book['title']; $author = $book['author']; } 44. book-form.php <?php $id = empty ($_GET['id']) ? null : $_GET['id']; if ( $id ) { // Database connection code $book = $dbh -> query( " SELECT title, author FROM bookshelf WHERE id = $id " ) -> fetch(); $title = $book [ 'title' ]; $author = $book [ 'author' ]; } 45. book-form.php < form method= "post" action= "process-book.php" > < input type= "hidden" id= "id" name= "id" value= "<?php echo $id ; ?>" /> < dl > < dt > < label for= "title" > Title </ label > </ dt > < dd > < input type= "text" id= "title" name= "title" value= "<?php echo $title ; ?>" /> </ dd > < dt > < label for= "author" > Author </ label > </ dt > < dd > < input type= "text" id= "author" name= "author" value= "<?php echo $author ; ?>" /> </ dd > < dt > </ dt > < dd > < input type= "submit" value= "Submit" /> </ dd > </ dl > </ form > 46. book-form.php < form method= "post" action= "process-book.php" > < input type="hidden" id="id" name="id" value= "<?php echo $id ; ?>" /> < dl > < dt > < label for="title">Title</ label > </ dt > < dd > < input type="text" id="title" name="title" value= "<?php echo $title ; ?>" /> </ dd > < dt > < label for="author">Author</ label > </ dt > < dd > < input type="text" id="author" name="author" value= "<?php echo $author ; ?>" /> </ dd > < dt > </ dt > < dd > < input type="submit" value="Submit" /> </ dd > </ dl > </ form > 47. process-book.php <?php if ( strtolower ( $_SERVER [ 'REQUEST_METHOD' ]) == 'get' ) { header ( "Location: /" ); } // Database connection code if ( empty ( $_POST [ 'id' ])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{ $_POST ['title']}', '{ $_POST ['author']}')" ; $dbh -> exec( $sql ); } else { $sql = " UPDATE bookshelf SET title = '{ $_POST ['title']}', " . "author = '{ $_POST ['author']}' WHERE id = { $_POST ['id']}" ; $dbh -> exec( $sql ); } header ( "Location: /" ); 48. process-book.php <?php if ( strtolower ( $_SERVER [ 'REQUEST_METHOD' ]) == 'get' ) { header ( "Location: /" ); } // Database connection code if ( empty ($_POST['id'])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{$_POST['title']}', '{$_POST['author']}')"; $dbh -> exec($sql); } else { $sql = " UPDATE bookshelf SET title = '{$_POST['title']}', " . "author = '{$_POST['author']}' WHERE id = {$_POST['id']}"; $dbh -> exec($sql); } header ("Location: /"); 49. process-book.php <?php if ( strtolower ($_SERVER['REQUEST_METHOD']) == 'get') { header ("Location: /"); } // Database connection code if ( empty ($_POST['id'])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{$_POST['title']}', '{$_POST['author']}')"; $dbh -> exec($sql); } else { $sql = " UPDATE bookshelf SET title = '{$_POST['title']}', " . "author = '{$_POST['author']}' WHERE id = {$_POST['id']}"; $dbh -> exec($sql); } header ("Location: /"); 50. process-book.php <?php if ( strtolower ($_SERVER['REQUEST_METHOD']) == 'get') { header ("Location: /"); } // Database connection code if ( empty ( $_POST [ 'id' ])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{ $_POST ['title']}', '{ $_POST ['author']}')" ; $dbh -> exec( $sql ); } else { $sql = " UPDATE bookshelf SET title = '{$_POST['title']}', " . "author = '{$_POST['author']}' WHERE id = {$_POST['id']}"; $dbh -> exec($sql); } header ("Location: /"); 51. process-book.php <?php if ( strtolower ($_SERVER['REQUEST_METHOD']) == 'get') { header ("Location: /"); } // Database connection code if ( empty ($_POST['id'])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{$_POST['title']}', '{$_POST['author']}')"; $dbh -> exec($sql); } else { $sql = " UPDATE bookshelf SET title = '{ $_POST ['title']}', " . "author = '{ $_POST ['author']}' WHERE id = { $_POST ['id']}" ; $dbh -> exec( $sql ); } header ("Location: /"); 52. process-book.php <?php if ( strtolower ($_SERVER['REQUEST_METHOD']) == 'get') { header ("Location: /"); } // Database connection code if ( empty ($_POST['id'])) { $sql = " INSERT INTO bookshelf (title, author) " . "VALUES ('{$_POST['title']}', '{$_POST['author']}')"; $dbh -> exec($sql); } else { $sql = " UPDATE bookshelf SET title = '{$_POST['title']}', " . "author = '{$_POST['author']}' WHERE id = {$_POST['id']}"; $dbh -> exec($sql); } header ( "Location: /" ); 61. base.php <?php date_default_timezone_set ( 'America/Chicago' ); error_reporting ( - 1 ); ini_set ( 'display_errors' , 1 ); ini_set ( 'display_startup_errors' , 1 ); $db = realpath ( dirname ( __FILE__ ) . '/../data/db/bookshelf.db' ); $dsn = "sqlite: $db " ; $options = array ( PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION , PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC ); try { $dbh = new PDO ( $dsn , null , null , $options ); } catch ( PDOException $e ) { throw $e ; echo "Error!: " . $e -> getMessage() . "<br /> " ; die (); } 62. base.php <?php date_default_timezone_set ( 'America/Chicago' ); error_reporting ( - 1); ini_set ('display_errors', 1); ini_set ('display_startup_errors', 1); $db = realpath ( dirname ( __FILE__ ) . '/../data/db/bookshelf.db'); $dsn = "sqlite:$db"; $options = array ( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); try { $dbh = new PDO ($dsn, null , null , $options); } catch ( PDOException $e) { throw $e; echo "Error!: " . $e -> getMessage() . "<br />"; die (); } 63. base.php <?php date_default_timezone_set ('America/Chicago'); error_reporting ( - 1 ); ini_set ( 'display_errors' , 1 ); ini_set ( 'display_startup_errors' , 1 ); $db = realpath ( dirname ( __FILE__ ) . '/../data/db/bookshelf.db'); $dsn = "sqlite:$db"; $options = array ( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); try { $dbh = new PDO ($dsn, null , null , $options); } catch ( PDOException $e) { throw $e; echo "Error!: " . $e -> getMessage() . "<br />"; die (); } 64. base.php <?php date_default_timezone_set ('America/Chicago'); error_reporting ( - 1); ini_set ('display_errors', 1); ini_set ('display_startup_errors', 1); $db = realpath ( dirname ( __FILE__ ) . '/../data/db/bookshelf.db' ); $dsn = "sqlite: $db " ; $options = array ( PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION , PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC ); try { $dbh = new PDO ( $dsn , null , null , $options ); } catch ( PDOException $e ) { throw $e ; echo "Error!: " . $e -> getMessage() . "<br /> " ; die (); } 68. We echo $title and $author < form method="post" action="process-book.php"> < input type="hidden" id="id" name="id" value="<?php echo $id; ?>" /> < dl > < dt > < label for="title">Title</ label > </ dt > < dd > < input type="text" id="title" name="title" value= "<?php echo $title ; ?>" /> </ dd > < dt > < label for="author">Author</ label > </ dt > < dd > < input type="text" id="author" name="author" value= "<?php echo $author ; ?>" /> </ dd > < dt > </ dt > < dd > < input type="submit" value="Submit" /> </ dd > </ dl > </ form > 69. Without defining $title and $author require_once dirname ( __FILE__ ) . '/library/base.php' ; $id = empty ( $_GET [ 'id' ]) ? null : $_GET [ 'id' ]; if ( $id ) { $book = $dbh -> query( . . . ) -> fetch(); $title = $book [ 'title' ]; $author = $book [ 'author' ]; } 70. Super easy to fix require_once dirname ( __FILE__ ) . '/library/base.php'; $id = empty ($_GET['id']) ? null : $_GET['id']; $title = null ; $author = null ; if ($id) { $book = $dbh -> query( . . . ) -> fetch(); $title = $book['title']; $author = $book['author']; } 80. book-form.php $statement = $dbh -> prepare( ' SELECT title, author FROM bookshelf WHERE id = :id' ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); $book = $statement -> fetch(); 81. book-form.php $statement = $dbh -> prepare (' SELECT title, author FROM bookshelf WHERE id = :id'); $statement -> bindParam(':id', $id); $statement -> execute(); $book = $statement -> fetch(); $title = $book['title']; $author = $book['author']; 82. book-form.php $statement = $dbh -> prepare( ' SELECT title, author FROM bookshelf WHERE id = :id' ); $statement -> bindParam(':id', $id); $statement -> execute(); $book = $statement -> fetch(); 83. book-form.php $statement = $dbh -> prepare(' SELECT title, author FROM bookshelf WHERE id = :id'); $statement -> bindParam( ':id' , $id ); $statement -> execute(); $book = $statement -> fetch(); 84. book-form.php $statement = $dbh -> prepare(' SELECT title, author FROM bookshelf WHERE id = :id'); $statement -> bindParam(':id', $id); $statement -> execute(); $book = $statement -> fetch(); 85. process-book.php if ( $id ) { $statement = $dbh -> prepare( " UPDATE bookshelf SET title = :title, author = :author WHERE id = :id" ); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); } else { $statement = $dbh -> prepare( " INSERT INTO bookshelf (title, author) VALUES (:title, :author)" ); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> execute(); } 86. process-book.php if ( $id ) { $statement = $dbh -> prepare( " UPDATE bookshelf SET title = :title, author = :author WHERE id = :id" ); $statement -> bindParam(':title', $title); $statement -> bindParam(':author', $author); $statement -> bindParam(':id', $id); $statement -> execute(); } else { $statement = $dbh -> prepare( " INSERT INTO bookshelf (title, author) VALUES (:title, :author)" ); $statement -> bindParam(':title', $title); $statement -> bindParam(':author', $author); $statement -> execute(); } 87. process-book.php if ($id) { $statement = $dbh -> prepare(" UPDATE bookshelf SET title = :title, author = :author WHERE id = :id"); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); } else { $statement = $dbh -> prepare(" INSERT INTO bookshelf (title, author) VALUES (:title, :author)"); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> execute(); } 88. process-book.php if ($id) { $statement = $dbh -> prepare(" UPDATE bookshelf SET title = :title, author = :author WHERE id = :id"); $statement -> bindParam(':title', $title); $statement -> bindParam(':author', $author); $statement -> bindParam(':id', $id); $statement -> execute(); } else { $statement = $dbh -> prepare(" INSERT INTO bookshelf (title, author) VALUES (:title, :author)"); $statement -> bindParam(':title', $title); $statement -> bindParam(':author', $author); $statement -> execute(); } 102. BookshelfService.php namespace Bookshelfervice; class BookshelfService { private $_dbh ; public function __construct (PDO $dbh ) { } public function find ( $id ) { } public function findAll () { } public function save ( array $options ) { } } 104. BookshelfService.php public function find ( $id ) { $sql = ' SELECT * FROM bookshelf WHERE id = :id' ; $statement = $this -> _dbh -> prepare( $sql ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); return $statement -> fetch(); } public function findAll () { $sql = ' SELECT * FROM bookshelf ORDER BY title' ; return $this -> _dbh -> query( $sql ) -> fetchAll(); } 105. BookshelfService.php public function save ( array $options ) { if ( $options [ 'id' ]) { $statement = $this -> _dbh -> prepare( " UPDATE bookshelf SET title = :title, author = :author WHERE id = :id" ); $statement -> execute( $options ); } else { unset ( $options [ 'id' ]); $statement = $this -> _dbh -> prepare( " INSERT INTO bookshelf (title, author) VALUES (:title, :author)" ); $statement -> execute( $options ); } } 108. base.php function autoload ( $className ) { $className = ltrim ( $className , ' ' ); $fileName = '' ; $namespace = '' ; if ( $lastNsPos = strripos ( $className , ' ' )) { $namespace = substr ( $className , 0 , $lastNsPos ); $className = substr ( $className , $lastNsPos + 1 ); $fileName = str_replace ( ' ' , DIRECTORY_SEPARATOR , $namespace ) . DIRECTORY_SEPARATOR ; } $fileName .= str_replace ( '_' , DIRECTORY_SEPARATOR , $className ) . '.php' ; require $fileName ; } spl_autoload_register ( 'autoload' ); 109. base.php // database connection code $bookshelf = new BookshelferviceBookshelfService ( $dbh ); 111. index.php: Before <?php require_once dirname ( __FILE__ ) . '/library/base.php' ; $books = $dbh -> query( " SELECT * FROM bookshelf ORDER BY title" ) -> fetchAll(); ?> 112. index.php: After <?php require_once dirname ( __FILE__ ) . '/library/base.php' ; $books = $bookshelf -> findAll(); ?> 113. book-form.php: Before if ( $id ) { $statement = $dbh -> prepare( ' SELECT title, author FROM bookshelf WHERE id = :id' ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); $book = $statement -> fetch(); $title = $book['title']; $author = $book['author']; } 114. book-form.php: After if ( $id ) { $book = $bookshelf -> find( $id ); $title = $book['title']; $author = $book['author']; } 115. process-book.php: Before if ( $id ) { $statement = $dbh -> prepare( " UPDATE bookshelf SET title = :title, author = :author WHERE id = :id" ); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> bindParam( ':id' , $id ); $statement -> execute(); } else { $statement = $dbh -> prepare( " INSERT INTO bookshelf (title, author) VALUES (:title, :author)" ); $statement -> bindParam( ':title' , $title ); $statement -> bindParam( ':author' , $author ); $statement -> execute(); } 125. PHP Manual: http://www.php.net/manual/en/index.php 127. PHP 101 Suggestions: http://csiphp.com/blog/2011/07/19/stop-doing-it-wrong-and-learn-to-code-good-too/ 128. PHP 101: PHP for the Absolute Beginner: http://devzone.zend.com/6/php-101-php-for-the-absolute-beginner/