Skip to content

Instantly share code, notes, and snippets.

@sabaelhilo
Created April 11, 2013 14:47
Show Gist options
  • Save sabaelhilo/5363981 to your computer and use it in GitHub Desktop.
Save sabaelhilo/5363981 to your computer and use it in GitHub Desktop.
<?php
include('DataLogin.php');
$data_login = new DataLogin();
$connection = $data_login->getConnection();
// stop the application if we can't connect
if (!$connection) {
echo '<div class="error-message">Could not connect to Database</div>';
die();
}
// Get the post data from the submitted form
if (!empty($_POST)) {
$contact_name = $_POST['full_name'];
$contact_phone = $_POST['phone_number'];
$contact_twitter_name = $_POST['twitter_handle'];
// Get the contact's twitter follower count
// Twitter's API is RESTful YES! - using json to get the data as an array
$twitter_api_url= "https://api.twitter.com/1/followers/ids.json";
$url = $twitter_api_url . '?cursor=-1&screen_name=' . $contact_twitter_name;
$json = file_get_contents($url, true);
$decode = json_decode($json, true);
// if twitter returns an error for some reason
// there is a limit on the number of requests per hour
if ($decode["error"]=="Rate limit exceeded. Clients may not make more than 150 requests per hour.") {
echo '<div class="error-message">More than a 150 requests have been made. Try again in a bit!</div>';
} elseif ($decode["errors"]) {
foreach($decode["errors"] as $error) {
if ($error["code"] == 34) {
echo '<div class="error-message">Twitter user with screen name ' . $contact_twitter_name . ' doesnt exist</div>';
} else {
// any other error code
echo '<div class="error-message">Unable to Retrieve Twitter Follower Count</div>';
}
}
} else {
// add the contact to the database
$contact_twitter_followers = sizeof($decode["ids"]);
$sql_query_insert = mysqli_query($connection,"INSERT INTO contacts VALUES ('$contact_twitter_name', '$contact_phone', '$contact_name', '$contact_twitter_followers')");
if (!$sql_query_insert) {
// not the most user friendly message - fix if have time
echo '<div class="error-message">Unable to Add Contact to Address Book: '. mysqli_error($connection) .'</div>';
}
}
}
// if I had more time I would've used a template (i.e. smarty) to seperate the html from the php
// display the address book table
echo '<div class="address-book-page">';
echo '<div class="address-book">';
echo '<h1>Your Address Book</h1>';
$select_result = mysqli_query($connection, "SELECT * FROM contacts ORDER BY full_name");
if (mysqli_num_rows($select_result)) {
echo '<table class="contacts-table" cellpadding=0 cellspacing=0>';
echo '<tr class="table-header"><th>Name</th><th>Phone Number</th><th>Twitter Handle</th><th>Twitter Follower Count</th></tr>';
while($row = mysqli_fetch_array($select_result)) {
$name = $row["full_name"];
$phone_number = $row["phone_number"];
$twitter_handle = $row["twitter_handle"];
$twitter_count = $row["twitter_followers"];
echo '<tr><td>'.$name.'</td><td>'.$phone_number.'</td><td>'.$twitter_handle.'</td><td>'.$twitter_count.'</td></tr>';
}
echo '</table>';
}
echo '<div class="add-contact-link"><a href="http://www.sabaelhilo.com/addContact.html">Add Contact</a></div>';
echo '</div>';
echo '</div>';
mysqli_close($connection);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="addressBookStyles.css">
<title> Address Book </title>
</head>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment