My quick Facebook Application :)

I thought to prepare an application where I hold all users’ blood group. Dont afraid to see how scratched the code is :P

Application url:  http://apps.facebook.com/mybloodgroup/

DB SQL:

CREATE TABLE `bloodgroup` (
 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
 `fb_uid` VARCHAR( 255 ) NOT NULL DEFAULT '0',
 `fb_uname` VARCHAR( 255 ) NULL ,
 `group` VARCHAR( 255 ) NULL
 ) ENGINE = MYISAM

PHP Code:

<?php
// Application: bloodgroup

// u have to put the fb php client to a specific directory
require_once '../client/facebook.php'; 

$appapikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$appsecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$facebook  = new Facebook($appapikey, $appsecret);
$user_id   = $facebook->require_login();

$db_ip   = "localhost";
$db_user = "xxxxxx";
$db_pass = "xxxxxx";
$db_name = "xxxxxx";

mysql_connect($db_ip, $db_user, $db_pass) or die("Could not connect: " . mysql_error());
mysql_select_db($db_name);

if(isset($_POST['action']) && $_POST['action']=='insert'){

 $fb_uid = $user_id;
 $bg_group = $_POST['bg_group'];
 $bg_fb_uname = $_POST['bg_fb_uname'];

 $sql= "SELECT * FROM `bloodgroup` WHERE fb_uid = ".$user_id." ";
 $result = mysql_query($sql) or die('Error, select failed');
 if (mysql_num_rows($result) != 0) { $needupdate = 1; }

 if ($needupdate == 1){
 $sql= "UPDATE `bloodgroup` SET `group` = '".$bg_group."',`fb_uname` = '".$bg_fb_uname."' WHERE fb_uid = ".$user_id." ";
 mysql_query($sql) or die('Error, update failed');
 }
 else{
 $sql= "INSERT INTO `bloodgroup` (`fb_uid`,`fb_uname`,`group`) VALUES ('".$user_id."','".$bg_fb_uname."','".$bg_group."')";
 mysql_query($sql) or die('Error, insert failed');
 }

}

?>
<fb:dashboard></fb:dashboard>
<h1> BLOOD GROUP </h1>

<div>
<form id="bloodgroup_insert" name="bloodgroup_insert" method="post" action="<? $_SERVER['PHP_SELF'] ?>">
 My Blood Group :
 <select name="bg_group">
 <option>A Positive</option>
 <option>A Negative</option>
 <option>B Positive</option>
 <option>B Negative</option>
 <option>AB Positive</option>
 <option>AB Negative</option>
 <option>O Positive</option>
 <option>O Negative</option>
 </select>
 <input type="hidden" id="bg_fb_uname" name="bg_fb_uname" value="<?php echo "<fb:name useyou='FALSE' uid='$user_id'/>"; ?>" />
 <input type="hidden" id="action" name="action" value="insert"/>
 <input type="submit" name="add" value="ADD" />
</form>
</div>

<?
 //SQL for own BG
 $sql= "SELECT * FROM `bloodgroup` WHERE fb_uid = ".$user_id."";
 $result = mysql_query($sql) or die('Error, select failed');

 if (mysql_num_rows($result) == 0) {
  $notset_bg = 1;
  $bloodgroup = "Not set yet";
 }
 else{
  $notset_bg = 0;
  while ($row = mysql_fetch_assoc($result)) {
   $bloodgroup = $row["group"];
   $bloodgroup_own = $bloodgroup;
   $bg[$user_id]["group"] = $row["group"];
   $bg[$user_id]["entrytime"] = $row["entrytime"];
  }
 }

 if($no_bloodgroup == 0){
  echo "<br> HELLO <fb:name useyou=\"FALSE\" uid=\"$user_id\"/>";
  echo "<fb:profile-pic uid=\"$user_id\" size=\"square\" linked=\"true\" /> Your Blood group is ".$bloodgroup.", Added on :".$bg[$user_id]["entrytime"].".";
  echo "<br>";
 }
?>

<? // People Who Have same Blood Group like my one.
echo "<p>People Who Have ".$bloodgroup_own." Blood Group : </p>";
echo "<br>"; $sql= "SELECT * FROM `bloodgroup` WHERE `group` LIKE '".$bloodgroup_own."'  ORDER BY RAND() LIMIT 5";
 $result = mysql_query($sql) or die('Error, select failed');
 if (mysql_num_rows($result) == 0) {
 $nodata_bg = 1;
 $bloodgroup = "Not set yet";
 }
 else{
 $nodata_bg = 0;
 while ($row = mysql_fetch_assoc($result)) {
 $bloodgroup = $row["group"];
 echo "<fb:profile-pic uid= ".$row["fb_uid"]." size=\"square\" linked=\"true\" /> <!--fb:name uid=\"$friend\"/--> : ".$bloodgroup." ";
 }
 }
?>

<div style=" height:5px; width: auto; background-color:#993366;" > </div>

<? // my friends blood group
$friends = $facebook->api_client->friends_get();
echo "<p>My Friends Blood Group : </p>";
echo "<br>";
$i=1;
foreach ($friends as $friend) {

 //SQL to find the BG
 $sql= "SELECT * FROM `bloodgroup` WHERE fb_uid = ".$friend."";
 $result = mysql_query($sql) or die('Error, select failed');
 if (mysql_num_rows($result) == 0) {
 $no_bloodgroup = 1;
 $bloodgroup = "Not Available";
 }
 else{
 $no_bloodgroup = 0;
 while ($row = mysql_fetch_assoc($result)) {
 $bloodgroup = $row["group"];
 $bg[$friend]["group"] = $row["group"];
 $bg[$friend]["pos-neg"] = $row["pos-neg"];
 $bg[$friend]["entrytime"] = $row["entrytime"];
 }
 }
 if($no_bloodgroup == 0){
 echo "<fb:name uid=\"$friend\"/> : ".$bloodgroup;
 echo "<br>";
 $i++;
 }
}

?>
<div style=" height:15px; width: auto;><? echo "Developed by, <fb:name uid=619926881 />"; ?> </div>

2 thoughts on “My quick Facebook Application :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s