WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : News management class
Categories : PHP, PHP Classes, Beginner Guides Click here to Update Your Picture
leaping langoor
Date : Jun 30th 2005
Grade : 2 of 5 (graded 2 times)
Viewed : 5165
File : No file for this code example.
Images : No Images for this code example.
Search : More code by leaping langoor
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

This is basically a simple news mangement class for beginners. First Create the table(MySQL):

CREATE TABLE `news` (
  `news_id` int(11) NOT NULL auto_increment,
  `news_title` varchar(64) NOT NULL,
  `news_body` longtext NOT NULL,
  `news_time` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `news_author` varchar(64) NOT NULL,
  `news_author_email` varchar(64) NOT NULL,
  PRIMARY KEY  (`news_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


news.php:
<?php

/************************************
** Class News
**
** Author: leapinglangoor [ leapinglangoor@yahoo.co.in ]
** Date..: 21/5/05
** ver...: v1.00
************************************/
class news
{
    var
$db['link'];
    var
$db['table'];
    var
$db['prefix'];
       
    function
news( $db['host'], $db['user'], $db['pass'], $db['db_name'], $db['table']='news', $db['prefix']='news_' )
    {
        if( !isset(
$db['host'] || !isset( $db['db_name'] ) )
        {
            return
false;
        }
       
       
$this->db['table'] = $db['table'];
       
$this->db['prefix'] = $db['prefix'];
       
       
$this->db['link'] = @mysql_connect( $db['host'], $db['user'], $db['pass'] );
       
        if( !isset(
$this->db['link'] ) )
        {
            return
false;
        }
       
        if( !
mysql_select_db( $db['db_name'], $db['link'] )
        {
            return
false;
        }
       
        return
true;
    }

   
    function
verify_addr( $address )
    {
       
$ret = false;
        if (
preg_match ( '/^[\w.]+@([\w.]+)\.[a-z]{2,6}$/i', $address, $domain ) )
        {
           
$domain = explode ( '.', $domain[0] );
             
// Split the domain into sections wherein the last element is either 'co', 'org', or the likes, or the primary domain name
             
foreach ( $domain as $part )
            {
// Iterate through the parts
                 
if ( substr ($part, 0, 1 ) == '_' || substr ( $part, strlen ( $part ) - 1, 1 ) == '_' )
                {
                       
$return = false; // The first or last character is _
                 
}
                else
                {                   
                   
$return = true; // The parts are fine. The address seems syntactically valid
               
}
             }
           }
           return
$return;
    }
   

    function
insert_news( $news['title'], $news['body'], $news['author'], $news['author_email'] )
    {
        if( !isset(
news['title'] ) || !isset( $news['body'] ) || !isset( $news['author'] ) )
        {
            return
false;
        }
       
        if( !
$this->verify_addr( $news['author_email'] ) )
        {
            return
false;
        }
       
       
$news['title'] = addslashes( $news['title'] );
       
$news['body'] = addslashes( $news['body'] );
       
$news['author'] = addslashes( $news['author'] );
       
       
$sql = "INSERT INTO `" . $this->db['table'] . "` ( `news_title`, `news_body`, `news_author`, `news_author_email` ) VALUES ( '" . $news['title'] . "','" . $news['body'] . "','" . $news['author'] . "','" . $news['author_email'] . "' )";
        if( !
mysql_query( $sql, $db['link'] );
        {
            return
false;
        }
       
        return
true;
       
       
    }
   
    function
delete_news( $news['id'] )
    {
   
        if( !isset(
$news['id'] ) )
        {
            return
false;
        }
       
       
$sql = "DELETE     FROM " . $db['table'] . " WHERE `news_id` = " . $news['id'] . "LIMIT 1";
        if( !
mysql_query( $sql, $table['link'] ) )
        {
            return
false;
        }
       
        return
true;
    }
   
    function
get_news( $news['id'] )
    {
       
        if(
$news['id'] )
        {
           
$sql1 = " where news_id = " . $news['id'];
        }
       
       
$sql = "SELECT * FROM " . $news['table'] . $sql1;
        if( !
$result = mysql_query( $sql, $db['link'] )
        {
            return
false;
        }
       
       
$i=0;
        foreach(
$row as mysql_fetch_array( $result ) )
        {
           
$elements[$i] = $row;
           
$i++;
        }
       
        return
$elements;
    }
}

?>


Example:

<?php

require_once( 'news.php' );

$news = new news( 'host', 'user', 'passwd', 'db' ) // Optional extra params - table_name and prefix for fields

$foo = $news->get_news();

/*
Display using the $foo array
*/

// To add news:
$news->insert_news( 'title', 'content', 'author', 'author@email.com' );
// Stuff can be replaced by variables

// To delete news
$news->delete_news( $news_id );

?>



A beginner's session handling class
Categories : PHP, PHP Classes, Sessions, Beginner Guides
Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP, Form Processing, PHP Classes, Data Validation, Beginner Guides
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
PHP Paypal IPN Integration Class v1.0.0
Categories : PHP, PHP Classes, Payment Gateways
crop and resize image class using gd library function
Categories : PHP, PHP Classes, GD image library, Graphics
Basic Authentication with sessions
Categories : PHP, Beginner Guides, Authentication, Form Processing, Sessions
Introduction to Language Files
Categories : PHP, Filesystem, Beginner Guides
email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
A Timing Class
Categories : PHP, PHP Classes, Date Time
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
Expose - PHP template engine, supports server and client-sided caching,a plugin system, multiple languages, template script language is based on PHP itself.
Categories : PHP, PHP Classes, Templates, Complete Programs
Create HTML forms dynamicly using Javascript & PHP
Categories : PHP, PHP Classes, Java Script
RSS parser. Parses RSS into an array. Quick and nasty but does the job. No checking is done for correct Tags, only correct XML. PHP4 needed to display result (uses print_r).
Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS)
MS Word Mail Merge Automation (COM)
Categories : PHP, PHP Classes, COM