user model
username}'";

$db = $this->_open_connection();

$find_user = mysql_query($select,$db);
$data_user = mysql_num_rows($find_user);

if($data_user == 0)
{

$sql = "INSERT INTO user(username,password) VALUES('{$model_user->username}','{$model_user->password}')";
$result = mysql_query($sql,$db);
return $result;
return true;

}
else
return false;
}

//LOGIN
public function user_select($model_user)
{
$select = "SELECT id FROM user WHERE username = '{$model_user->username}' AND password = '{$model_user->password}'";

$db = $this->_open_connection();

$find_user = mysql_query($select,$db);
$data_user = mysql_num_rows($find_user);
$data = mysql_fetch_array($find_user);

if($data_user == 1)
{

$_SESSION['uid'] = $data['id'];
return true;
}

else
return false;
}

//GET USER_id
public function userID($model_user)
{
$select = "SELECT id FROM user WHERE username = '{$model_user->username}'";

$db = $this->_open_connection();

$find_user = mysql_query($select,$db);
$data = mysql_fetch_array($find_user);

return $data['id'];
}

//UPDATE PROFILE
public function editProfile($model_user)
{

$sql = "UPDATE user SET name='{$model_user->name}',email='{$model_user->email}',password='{$model_user->password}',sex='{$model_user->sex}'
,city='{$model_user->city}',country='{$model_user->country}',contact='{$model_user->contact}',aboutme='{$model_user->aboutme}'
WHERE id = '{$model_user->id}'";
$db=$this->_open_connection();
$result=mysql_query($sql,$db);

$_SESSION['upload_msg'] = '';
$_SESSION['saveprofile_msg'] = "profile succesfully save";
header('Location: user.php?view=edit-profile');
}

//UPLOAD PROFILE PICTURE
public function profilepic($model_user)
{
$target_path = $_SERVER['DOCUMENT_ROOT'].'/mvc2/upload/';

$target_path = $target_path . basename( $_FILES['file']['name']);

$picname = $_FILES["file"]["name"];

if($_FILES["file"]["type"] == "image/jpeg"){
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {

$_SESSION['upload_msg'] = "The file ". basename( $_FILES['file']['name']).
" has been uploaded";


$sql = "UPDATE user SET profilepic='{$picname}' WHERE id = '{$model_user->id}'";
$db=$this->_open_connection();
$result=mysql_query($sql,$db);


header('Location: user.php?view=edit-profile');
}
}
else{
$_SESSION['upload_msg'] = "There was an error uploading the file, please try again!";
header('Location: user.php?view=edit-profile');
}

}


//DISPLAY USER DATA
public function user_data($id)
{
$sql="SELECT * FROM user WHERE id='{$id}'";
$db=$this->_open_connection();
$result=mysql_query($sql,$db);
$row=mysql_fetch_array($result);
$data=array('profilepic'=>$row['profilepic'],
'name'=>$row['name'],
'email'=>$row['email'],
'password'=>$row['password'],
'sex'=>$row['sex'],
'city'=>$row['city'],
'country'=>$row['country'],
'contact'=>$row['contact'],
'aboutme'=>$row['aboutme'],);

return $data;
}

//ALL USER DATA DISPLAY
public function all_user()
{
$sql="SELECT * FROM user";
$db=$this->_open_connection();
$result=mysql_query($sql,$db);
while($row=mysql_fetch_array($result))
{
$data=array('profilepic'=>$row['profilepic'],
'name'=>$row['name'],
'email'=>$row['email'],
'password'=>$row['password'],
'sex'=>$row['sex'],
'city'=>$row['city'],
'country'=>$row['country'],
'contact'=>$row['contact'],
'aboutme'=>$row['aboutme'],);

if( $row['id'] != $_SESSION['uid']){
echo "";
echo $row['username']."

"; }
}
return $data;
}

//ADD COMMENT
public function addComments($model_user)
{
if(!empty($model_user->comment))
{
$sql = "INSERT INTO commnets(comments,my_id,post_id) VALUES('{$model_user->comment}','{$model_user->myid}','{$model_user->postid}' )";
$db=$this->_open_connection();
$result = mysql_query($sql,$db);
return true;;
}
else
return false;
}

//DATABASE CONNECTION
private function _open_connection(){
$con=mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if(!$con)
die('Error Connection:'.mysql_error());
$db_select=mysql_select_db(DB_NAME,$con);
if(!$db_select)
die('Error Selection'.mysql_error());
return $con;
}
}
?>

CONTROLLER
username = $_POST['username'];
$model_user->password = $_POST['password'];

return $model_user->user_registration($model_user);
}

//login
public function user_login()
{
require_once($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/model/user_model.php');

$model_user = new user_model();
$model_user->username = $_POST['username'];
$model_user->password = $_POST['password'];

return $model_user->user_select($model_user);

}

//get userID
public function userID()
{
require_once($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/model/user_model.php');

$model_user = new user_model();
$model_user->username = $_POST['username'];
$model_user->password = $_POST['password'];

return $model_user->userID($model_user);
}

//profile edit
public function profileEdit()
{

require_once($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/model/user_model.php');

$model_user = new user_model();
$model_user->id = $_POST['id'];
$model_user->name = $_POST['name'];
$model_user->email = $_POST['email'];
$model_user->password = $_POST['password'];
$model_user->name = $_POST['name'];
$model_user->sex = $_POST['sex'];
$model_user->city = $_POST['city'];
$model_user->country = $_POST['country'];
$model_user->contact = $_POST['contact'];
$model_user->aboutme = $_POST['aboutme'];

return $model_user->editProfile($model_user);
}

//upload pic
public function uploadProfilePic()
{
require_once($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/model/user_model.php');

$model_user = new user_model();

$model_user->file = $_POST['file'];
$model_user->id = $_POST['id'];

return $model_user->profilepic($model_user);

}

//add comment
public function addComment()
{
require_once($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/model/user_model.php');

$model_user = new user_model();

$model_user->comment = $_POST['comment'];
$model_user->myid = $_POST['myid'];
$model_user->postid = $_SESSION['postid'];


return $model_user->addComments($model_user);
}

//menu
public function menus()
{
echo "My PROFILE ";
echo "REGISTERED USERS ";
echo "LOG OUT ";
}


}



$user = new user();

//view ********************************

if(!empty($_GET['view']))
{
$view = $_GET['view'];
{
switch($view)
{
case 'user-signup':
$page = $user->pageIndex();
break;

case 'user-profile':
$page = $user->profilePage();
break;

case 'edit-profile':
$page = $user->profileViewEdit();
break;

case 'all-user':
$page = $user->AllUserProfile();
break;

case 'visit-profile':
$page = $user->visitProfile();
break;
}
include($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/view/'.$page.'.php');
}
}

//model *******************************************
if(!empty ($_GET[model]))
$model = $_GET['model'];
{
switch($model)
{

//REGISTRATION
case 'user-register':
$result = $user->user_registration();
if($result)
{
$_SESSION['reg_message'] = 'Success register';
}
else
{
$_SESSION['reg_message'] = 'username already exist';
}
$_SESSION['login_message'] = '';
header('Location:user.php?view=user-signup');
break;

//LOG-IN
case 'user-log':
$page = $user->profilePage();
$result = $user->user_login();
if($result)
{
$id = $user->userID();
header('Location:user.php?view=user-profile&id='.$id);
}
else
{
$_SESSION['login_message'] = 'worng username or password, please try again';
header('Location:user.php?view=user-signup');
}
$_SESSION['reg_message'] = '';
break;

//PROFILE EDIT
case 'profile-edit':
$user->profileEdit();

break;

//UPLOAD PICTURE
case 'upload-pic';
$user->uploadProfilePic();
break;
//ADD COMMENT
case 'add-comment';
$user->addComment();
break;
}
}
?>