Home arrow PHP arrow Page 3 - Building a Relational Content Management System in PHP/MySQL

The Common Functions - PHP

You may be familiar with relational databases, but what is a relational content management system? Read on to learn how to build this system, which helps you create a search engine friendly site fairly quickly.

TABLE OF CONTENTS:
  1. Building a Relational Content Management System in PHP/MySQL
  2. The Rewrite
  3. The Common Functions
  4. Managing Articles
  5. Displaying the Articles
By: Roger Stringer
Rating: starstarstarstarstar / 39
December 20, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

These are a group of functions that are used by the script. We use common.php to store all of the functions that we call throughout the script.

<?
    session_start();
    include("config.php");
    $dbh=mysql_connect ($dbhost, $dbuser, $dbpassword) or die ('I
cannot connect to the database because: ' . mysql_error());
    mysql_select_db ($dbname,$dbh);
    define("DBH",$dbh);

    function getPage($field='seoname',$value=''){
        $result = mysql_query("SELECT * FROM articles WHERE
{$field}='{$value}'", DBH);
        $page = mysql_fetch_assoc($result);
        return $page;
    }
    function getPages($field='parent',$value='0'){
        $result = mysql_query("SELECT * FROM articles WHERE
{$field}='{$value}'", DBH);
        $pages = array();
        while( $row = mysql_fetch_assoc($result) ){
            $pages[$row['id']] = $row;
        }
        return $pages;
    }
    function AddPage(){
        $_POST['akey'] = makeKey(10);
        unset($_POST['id']);
        $_POST['seoname'] = seoname($_POST['title']);
        $query = "INSERT INTO articles (".implode(", ",array_keys
($_POST)).") VALUES ('".implode("', '",array_map
("mysql_real_escape_string",$_POST))."')";
        mysql_query($query,DBH) or die( mysql_error() );
        return mysql_insert_id();
    }
    function UpdatePage($pid){
        $_POST['seoname'] = seoname($_POST['title']);
        $query = "UPDATE articles SET ";
        foreach($_POST as $field => $value) {
        $query .= "$field = '".mysql_real_escape_string
($value)."', ";
        }
        $query = substr($query, 0, strlen($query)-2)." WHERE id =
'{$pid}'";
        mysql_query($query,DBH) or die( mysql_error() );
        return mysql_affected_rows();
    }
    function DeletePage($pid){
        unset($_POST['step']);
        $query = "DELETE FROM articles WHERE id = '{$pid}'";
        mysql_query($query,DBH) or die( mysql_error() );
        return mysql_affected_rows();
    }
    function seoname($string){
        $string = ltrim($string);
        $string = preg_replace( "/ +/", " ", strtolower($string) );
        $string = str_replace(' - ', '-', $string);
        $string = str_replace(array('-
','%',';','/','?',':','@','&','=','+','$',',','#','(',')'), '',
$string);
        $search  = array(' ', '', '',
'','','','','','','',);
        $replace = array
('','ae','oe','ue','e','i','e','e','a','c');
        $string = str_replace($search, $replace, $string);
        $string = preg_replace("/[^a-z0-9_-]/", "", $string);
        $string = strtolower($string);
        return urlencode($string);
    }
?>



 
 
>>> More PHP Articles          >>> More By Roger Stringer
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: