DB:
Código PHP:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET latin1 NOT NULL,
`password` varchar(255) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;
CREATE TABLE IF NOT EXISTS `frnd_req` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`from` int(11) NOT NULL,
`to` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `frnds` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_one` int(11) NOT NULL,
`user_two` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;
Código PHP:
<html>
<head>
<title>Profile - Friend System</title>
<link rel='stylesheet' href='style.css' />
</head>
<body>
<? include 'connect.php'; ?>
<? include 'functions.php'; ?>
<? include 'header.php'; ?>
<div class='container'>
<?php
if(isset($_GET['user']) && !empty($_GET['user'])){
$user = $_GET['user'];
} else {
$user = $_SESSION['user_id'];
}
$my_id = $_SESSION['user_id'];
$username = getuser($user, 'username');
?>
<h3><?php echo $username; ?></h3>
<?php
if($user != $my_id){
$check_frnd_query = mysql_query("SELECT id FROM frnds WHERE (user_one='$my_id' AND user_two='$user') OR (user_one='$user' AND user_two='$my_id')");
if(mysql_num_rows($check_frnd_query) == 1){
echo "<a href='#' class='box'>Already Frnds</a> | <a href='actions.php?action=unfrnd&user=$user' class='box'>Unfriend $username</a>";
} else {
$from_query = mysql_query("SELECT `id` FROM `frnd_req` WHERE `from`='$user' AND `to`='$my_id'");
$to_query = mysql_query("SELECT `id` FROM `frnd_req` WHERE `from`='$my_id' AND `to`='$user'");
if(mysql_num_rows($from_query) == 1){
echo "<a href='#' class='box'>Ignore</a> | <a href='actions.php?action=accept&user=$user' class='box'>Accept</a>";
} else if (mysql_num_rows($to_query) == 1){
echo "<a href='actions.php?action=cancel&user=$user' class='box'>Cancel Request</a>";
} else {
echo "<a href='actions.php?action=send&user=$user' class='box'>Send Friend Request</a>";
}
}
}
?>
</div>
</body>
</html>
Cuando te llega la solicitud tampoco puedes aceptarla por que se queda igual
También si tu mandas una solicitud y quieres cancelarlo tampoco lo hace.
-Este es el archivo actions.php que hace las consultas
actions.php
Código PHP:
<?
include 'connect.php';
include 'functions.php';
$action = $_GET['action'];
$user = $_GET['user'];
$my_id = $_SESSION['usuer_id'];
if($action == 'send'){
mysql_query("INSERT INTO frnd_req VALUES('', '$my_id', '$user')");
}
if($action == 'cancel'){
mysql_query("DELETE FROM `frnd_req` WHERE `from`='$my_id' AND `to`='$user'");
}
if($action == 'accept'){
mysql_query("DELETE FROM frnd_req WHERE from='$user' AND to='$my_id'");
mysql_query("INSERT INTO frnds VALUES('', '$user', '$my_id')");
}
if($action == 'unfrnd'){
mysql_query("DELETE FROM frnds WHERE (user_one='$my_id' AND user_two='$user') OR (user_one='$user' AND user_two='$my_id')");
}
header('location: profile.php?user='.$user);
?>
-El archivo functions.php que sirve para el login
functions.php
Código PHP:
<?php
session_start();
function loggedin(){
if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])){
return true;
} else {
return false;
}
}
function getuser($id, $field){
$query = mysql_query("SELECT $field FROM users WHERE id='$id'");
$run = mysql_fetch_array($query);
return $run[$field];
}
?>
Código PHP:
<html>
<head>
<title>Requests - Friend System</title>
<link rel='stylesheet' href='style.css' />
</head>
<body>
<? include 'connect.php'; ?>
<? include 'functions.php'; ?>
<? include 'header.php'; ?>
<div class='container'>
<hr>Requests : </h3>
<?php
$my_id = $_SESSION['user_id'];
$req_query = mysql_query("SELECT `from` FROM `frnd_req` WHERE `to`='my_id'");
while($run_req = mysql_fetch_array($req_query)){
$from = $run_req['from'];
$from_username = getuser($from, 'username');
echo "<a href='profile.php?user=$from' class='box' style='display:block'>$from_username</a>";
}
?>
</div>
</body>
</html>