Social Networking Site Project Setup

In this tutorial, I will show you how to set up our project before we start developing a several web pages. First lets create a folder name “philsocial” inside our web server. Since we will be using a Twitter Bootstrap framework, and if you don’t have a copy of this, you can download it here. After downloading, you need to extract it and copy the following folder such as assets, css, fonts and js. Then paste it inside “philsocial” folder, and add three new folder such as “img” where we’re going to store our images to be used later on, the another folder and name it as “uploads”. This folder will be used as a storage area for our upload files and documents. And finally the “includes” folder that will hold our different classes and libraries. Here's the folder structure: philsocial/ ├── css/ │ ├── bootstrap.css │ ├── bootstrap.min.css │ ├── bootstrap-theme.css │ ├── bootstrap-theme.min.css ├── js/ │ ├── bootstrap.js │ ├── bootstrap.min.js └── fonts/ ├── glyphicons-halflings-regular.eot ├── glyphicons-halflings-regular.svg ├── glyphicons-halflings-regular.ttf └── glyphicons-halflings-regular.woff |--- assets/ | |--css | | |--pygments-manni.css | | |--docs.css | |--js | | |--application.js | | |--customizer.js | | |--filesaver.js | | |--holder.js | | |--html5shiv.js | | |--jquery.js | | |--jszip.js | | |--less.js | | |--raw-files.js | | |--respond.min.js | | |--uglify.js | |--ico |--- uploads |--- img |--- includes This time, we're going to create a new PHP file and name it as “config.php”, then save it inside includes folder. And add the following code:
  1. <?php
  2. /**
  3. * Description:  The main class for Database.
  4. * Author:               Joken Villanueva
  5. * Date Created: October 27, 2013
  6. * Revised By:          
  7. */
  8.  
  9. //Database Constants
  10. defined('DB_SERVER') ? null : define("DB_SERVER","localhost");//define our database server
  11. defined('DB_USER') ? null : define("DB_USER","root");             //define our database user   
  12. defined('DB_PASS') ? null : define("DB_PASS","");                         //define our database Password       
  13. defined('DB_NAME') ? null : define("DB_NAME","philsocialdb"); //define our database Name
  14. ?>
Next,together with our “config.php file”, we’re going to create a PHP file that will hold and manage our Database objects, and this lets save this file as “database.php”, then add the following code:
  1. <?php
  2. /**
  3. * Description:  The main class for Database.
  4. * Author:               Joken Villanueva
  5. * Date Created: october27, 2013
  6. * Revised By:          
  7. */
  8.  
  9. require_once(LIB_PATH.DS."config.php");
  10. class Database {
  11.         var $sql_string = '';
  12.         var $error_no = 0;
  13.         var $error_msg = '';
  14.         private $conn;
  15.         public $last_query;
  16.         private $magic_quotes_active;
  17.         private $real_escape_string_exists;
  18.        
  19.         function __construct() {
  20.                 $this->open_connection();
  21.                 $this->magic_quotes_active = get_magic_quotes_gpc();
  22.                 $this->real_escape_string_exists = function_exists("mysql_real_escape_string");
  23.         }
  24.        
  25.         public function open_connection() {
  26.                 $this->conn = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
  27.                 if(!$this->conn){
  28.                         echo "Problem in database connection! Contact administrator!";
  29.                         exit();
  30.                 }else{
  31.                         $db_select = mysql_select_db(DB_NAME,$this->conn);
  32.                         if (!$db_select) {
  33.                                 echo "Problem in selecting database! Contact administrator!";
  34.                                 exit();
  35.                         }
  36.                 }
  37.  
  38.         }
  39.        
  40.         function setQuery($sql='') {
  41.                 $this->sql_string=$sql;
  42.         }
  43.        
  44.         function executeQuery() {
  45.                 $result = mysql_query($this->sql_string, $this->conn);
  46.                 $this->confirm_query($result);
  47.                 return $result;
  48.         }      
  49.        
  50.         private function confirm_query($result) {
  51.                 if(!$result){
  52.                         $this->error_no = mysql_errno( $this->conn );
  53.                         $this->error_msg = mysql_error( $this->conn );
  54.                         return false;                          
  55.                 }
  56.                 return $result;
  57.         }
  58.        
  59.         function loadResultList( $key='' ) {
  60.                 $cur = $this->executeQuery();
  61.                
  62.                 $array = array();
  63.                 while ($row = mysql_fetch_object( $cur )) {
  64.                         if ($key) {
  65.                                 $array[$row->$key] = $row;
  66.                         } else {
  67.                                 $array[] = $row;
  68.                         }
  69.                 }
  70.                 mysql_free_result( $cur );
  71.                 return $array;
  72.         }
  73.        
  74.         function loadSingleResult() {
  75.                 $cur = $this->executeQuery();
  76.                        
  77.                 while ($row = mysql_fetch_object( $cur )) {
  78.                         $data = $row;
  79.                 }
  80.                 mysql_free_result( $cur );
  81.                 return $data;
  82.         }
  83.        
  84.         function getFieldsOnOneTable( $tbl_name ) {
  85.        
  86.                 $this->setQuery("DESC ".$tbl_name);
  87.                 $rows = $this->loadResultList();
  88.                
  89.                 $f = array();
  90.                 for ( $x=0; $x<count( $rows ); $x++ ) {
  91.                         $f[] = $rows[$x]->Field;
  92.                 }
  93.                
  94.                 return $f;
  95.         }      
  96.  
  97.         public function fetch_array($result) {
  98.                 return mysql_fetch_array($result);
  99.         }
  100.         //gets the number or rows      
  101.         public function num_rows($result_set) {
  102.                 return mysql_num_rows($result_set);
  103.         }
  104.  
  105.         public function insert_id() {
  106.     // get the last id inserted over the current db connection
  107.                 return mysql_insert_id($this->conn);
  108.         }
  109.  
  110.         public function affected_rows() {
  111.                 return mysql_affected_rows($this->conn);
  112.         }
  113.        
  114.          public function escape_value( $value ) {
  115.                 if( $this->real_escape_string_exists ) { // PHP v4.3.0 or higher
  116.                         // undo any magic quote effects so mysql_real_escape_string can do the work
  117.                         if( $this->magic_quotes_active ) { $value = stripslashes( $value ); }
  118.                         $value = mysql_real_escape_string( $value );
  119.                 } else { // before PHP v4.3.0
  120.                         // if magic quotes aren't already on then add slashes manually
  121.                         if( !$this->magic_quotes_active ) { $value = addslashes( $value ); }
  122.                         // if magic quotes are active, then the slashes already exist
  123.                 }
  124.                 return $value;
  125.         }
  126.        
  127.         public function close_connection() {
  128.                 if(isset($this->conn)) {
  129.                         mysql_close($this->conn);
  130.                         unset($this->conn);
  131.                 }
  132.         }
  133.        
  134. }
  135. $mydb = new Database();
  136.  
  137.  
  138. ?>
And we need also to create another PHP file, then save it as “function.php” and add the following code: This code is useful when we are cleaning some string or variable, redirecting to another page and automatic loading of different classes.
  1. <?php
  2.         function strip_zeros_from_date($marked_string="") {
  3.                 //first remove the marked zeros
  4.                 $no_zeros = str_replace('*0','',$marked_string);
  5.                 $cleaned_string = str_replace('*0','',$no_zeros);
  6.                 return $cleaned_string;
  7.         }
  8.         function redirect_to($location = NULL) {
  9.                 if($location != NULL){
  10.                         header("Location: {$location}");
  11.                         exit;
  12.                 }
  13.         }
  14.         function output_message($message="") {
  15.        
  16.                 if(!empty($message)){
  17.                 return "<p class=\"message\">{$message}</p>";
  18.                 }else{
  19.                         return "";
  20.                 }
  21.         }
  22.         function __autoload($class_name) {
  23.                 $class_name = strtolower($class_name);
  24.                 $path = LIB_PATH.DS."{$class_name}.php";
  25.                 if(file_exists($path)){
  26.                         require_once($path);
  27.                 }else{
  28.                         die("The file {$class_name}.php could not be found.");
  29.                 }
  30.                                        
  31.         }
  32.        
  33.                
  34. ?>
And finally, we will create another PHP file inside includes folder then, save it as “initialize.php” and add the following code: This code will simply do the initialization of our different classes.
  1. <?php
  2. /**
  3. * Description:  This includes for basic and core configurations.
  4. * Author:               Joken Villanueva
  5. * Date Created: october 27, 2013
  6. * Revised By:          
  7. */
  8.  
  9. //define the core paths
  10. //Define them as absolute peths to make sure that require_once works as expected
  11.  
  12. //DIRECTORY_SEPARATOR is a PHP Pre-defined constants:
  13. //(\ for windows, / for Unix)
  14. defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
  15.  
  16. defined('SITE_ROOT') ? null : define ('SITE_ROOT', $_SERVER['DOCUMENT_ROOT'].DS.'philsocial');
  17.  
  18. defined('LIB_PATH') ? null : define ('LIB_PATH',SITE_ROOT.DS.'includes');
  19.  
  20. // load config file first
  21. require_once(LIB_PATH.DS."config.php");
  22. //load basic functions next so that everything after can use them
  23. require_once(LIB_PATH.DS."functions.php");
  24. //later here where we are going to put our class session
  25.  
  26. //Load Core objects
  27. require_once(LIB_PATH.DS."database.php");
  28.  
  29. //load database-related classes
  30.  
  31.  
  32. ?>

Comments

nice one keep up

Add new comment