-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab777bf
commit 317c9b2
Showing
13 changed files
with
469 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
function showFormAddContact($error = 0) { | ||
echo '<strong>Add Contact</strong><br/><br/>'; | ||
echo '<form method="POST">'; | ||
echo 'Name'; | ||
echo '<input class="form-control" type="text" name="name" autofocus/>'; | ||
echo 'Email'; | ||
echo '<input class="form-control" type="email" name="email"/>'; | ||
echo 'Address'; | ||
echo '<input class="form-control" type="textarea" name="address"/>'; | ||
echo 'Phone'; | ||
echo '<input class="form-control" type"tel" name="phone"/><br/>'; | ||
echo '<input class="btn btn-dark" type="submit" value="Save"/>'; | ||
echo '</form>'; | ||
if($error == 1) { | ||
echo '<p>Error inserting in database</p>'; | ||
} elseif ($error == 2) { | ||
echo '<p>Field "name" cannot be blank</p>'; | ||
} | ||
} | ||
|
||
function addContact($name, $email, $address, $phone) { | ||
require 'config.php'; | ||
$data = array(); | ||
|
||
$sql = "INSERT INTO contact (name, email, address, phone) VALUES "; | ||
$sql.="(:name, :email, :address, :phone)"; | ||
|
||
$sql = $pdo->prepare($sql); | ||
$sql->bindValue(':name', $name); | ||
$sql->bindValue(':email', $email); | ||
$sql->bindValue(':address', $address); | ||
$sql->bindValue(':phone', $phone); | ||
|
||
$sql->execute(); | ||
|
||
if($pdo->lastInsertId() > 0) { | ||
return 0; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
try { | ||
$pdo = new PDO("mysql:dbname=agenda_telefonica;host=localhost", "root", "123"); | ||
} catch(PDOException $e) { | ||
echo "Error: ".$e->getMessage(); | ||
exit; | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
<?php | ||
require 'config.php'; | ||
require 'search_contact.php'; | ||
require 'add_contact.php'; | ||
require 'edit_contact.php'; | ||
require 'remove_contact.php'; | ||
|
||
function showMenu() { | ||
//menu add contact/edit contact | ||
if(isset($_GET['add_contact']) || isset($_GET['edit'])) { | ||
$result = 0; | ||
|
||
if(isset($_POST['name'])) { | ||
if(!empty($_POST['name'])) { | ||
$name = addslashes($_POST['name']); | ||
$email = addslashes($_POST['email']); | ||
$address = addslashes($_POST['address']); | ||
$phone = addslashes($_POST['phone']); | ||
|
||
//edit contact in db | ||
if(isset($_GET['id'])) { | ||
$id = addslashes($_GET['id']); | ||
$result = editContact($id, $name, $email, $address, $phone); | ||
//add contact to db | ||
} else { | ||
$result = addContact($name, $email, $address, $phone); | ||
} | ||
|
||
if($result == 0) { | ||
header('Location: contacts.php'); | ||
} | ||
|
||
} else { | ||
$result = 2; | ||
} | ||
} | ||
|
||
//edit form | ||
if(isset($_GET['id'])) { | ||
$id = addslashes($_GET['id']); | ||
showFormEditContact(getContactById($id), $result); | ||
//add form | ||
} else { | ||
showFormAddContact($result); | ||
} | ||
|
||
//menu search contact | ||
} elseif(isset($_GET['search_contact'])) { | ||
if(!isset($_GET['search']) || empty($_GET['search'])) { | ||
showFormSearch(); | ||
} | ||
//remove contact | ||
} elseif(isset($_GET['remove'])) { | ||
$id = addslashes($_GET['id']); | ||
if(getContactById($id)) { | ||
removeContact($id); | ||
showHomeMenu(); | ||
} | ||
///menu home | ||
} else { | ||
showHomeMenu(); | ||
} | ||
|
||
} | ||
|
||
//page navigation | ||
//number of contacts per page | ||
$contact_per_page = 2; | ||
|
||
//get the actual page | ||
$pg = 1; | ||
if(isset($_GET['p']) && !empty($_GET['p'])) { | ||
$pg = addslashes($_GET['p']); | ||
} | ||
$p = ($pg - 1) * $contact_per_page; | ||
|
||
//number of pages | ||
function getMaxPage() { | ||
global $pdo; | ||
global $contact_per_page; | ||
|
||
$sql = "SELECT COUNT(*) AS num FROM contact"; | ||
//show search results | ||
if(isset($_GET['search']) && !empty($_GET['search'])) { | ||
$search = addslashes($_GET['search']); | ||
$search = '%'.$search.'%'; | ||
$sql.= " WHERE name LIKE ?"; | ||
$sql = $pdo->prepare($sql); | ||
$sql->execute(array($search)); | ||
} else { | ||
$sql = $pdo->query($sql); | ||
} | ||
$sql = $sql->fetch(); | ||
$num = intval($sql['num']); | ||
return ceil($num/$contact_per_page); | ||
} | ||
|
||
//current page content | ||
function getContactsPerPage($page = 0) { | ||
global $pdo; | ||
global $contact_per_page; | ||
|
||
$sql = "SELECT * FROM contact"; | ||
if(isset($_GET['search']) && !empty($_GET['search'])) { | ||
$search = addslashes($_GET['search']); | ||
$search = '%'.$search.'%'; | ||
$sql.= " WHERE name LIKE ?"; | ||
$sql.= " LIMIT $page, $contact_per_page"; | ||
$sql = $pdo->prepare($sql); | ||
$sql->execute(array($search)); | ||
} else { | ||
$sql.= " LIMIT $page, $contact_per_page"; | ||
$sql = $pdo->query($sql); | ||
} | ||
|
||
$contacts = $sql->fetchAll(); | ||
|
||
return $contacts; | ||
} | ||
|
||
function getContactById($id) { | ||
$contact = array(); | ||
global $pdo; | ||
$sql = "SELECT * FROM contact WHERE id = :id"; | ||
$sql = $pdo->prepare($sql); | ||
$sql->bindValue(':id', $id); | ||
$sql->execute(); | ||
|
||
if($sql->rowCount() > 0) { | ||
$contact = $sql->fetch(); | ||
} | ||
|
||
return $contact; | ||
} | ||
|
||
function showHomeMenu() { | ||
echo '<strong>Menu</strong><br/><br/>'; | ||
echo '<a href="?add_contact" class="btn btn-dark">Add Contact</a><br/><br/>'; | ||
echo '<a href="?search_contact=true" class="btn btn-dark">Find Contact</a><br/><br/>'; | ||
} | ||
|
||
function showContacts($contacts) { | ||
if(count($contacts) > 0) { | ||
echo '<strong>Contacts</strong><br/><br/>'; | ||
echo '<div id="contatos">'; | ||
echo '<table class="table borderless" width="100%">'; | ||
foreach($contacts as $contact) { | ||
echo '<tr class="controls"><td colspan="2"><a href="contacts.php?remove=true&id='.$contact['id'].'"><img src="images/delete.png"></a><a href="contacts.php?edit=true&id='.$contact['id'].'"><img src="images/edit.png"/></a></td></tr>'; | ||
echo '<tr><th>Name</th><td>'.$contact['name'].'</td></tr>'; | ||
echo '<tr><th>Email</th><td>'.$contact['email'].'</td></tr>'; | ||
echo '<tr><th>Phone</th><td>'.$contact['phone'].'</td></tr>'; | ||
echo '<tr><th>Address</th> <td>'.$contact['address'].'</td></tr>'; | ||
echo '<tr class="space"><td colspan="2"></td></tr>'; | ||
} | ||
echo '</table>'; | ||
echo '</div>'; | ||
} | ||
} | ||
?> | ||
|
||
<!DOCTYPE html/> | ||
<html> | ||
<head> | ||
<title>Contact Schedule</title> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous"> | ||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script> | ||
<link rel="stylesheet" type="text/css" href="css/style.css"/> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="row main-area"> | ||
<div class="col-md-1"> | ||
<?php if(!$p==0 && !isset($_GET['search'])):?> | ||
<a href="?p=<?php if($pg>1){echo $pg-$contact_per_page;}?>" class="btn btn-default btn-back"><img width="50px" src="images/back.png"/></a> | ||
<?php elseif(isset($_GET['search']) && $pg>1):?> | ||
<a href="<?php echo "?search=".$_GET['search'].'&p='.($pg-$contact_per_page);?>" class="btn btn-default btn-back"><img width="50px" src="images/back.png"/></a> | ||
<?php elseif(!$_GET || (isset($_GET['p']) && $_GET['p'] == 0)): ?> | ||
<a href="index.php" class="btn btn-default btn-back"><img width="50px" src="images/back.png"/></a> | ||
<?php else: ?> | ||
<a href="contacts.php" class="btn btn-default btn-home"><img width="50px" src="images/home.png"/></a> | ||
<?php endif?> | ||
</div> | ||
<div class="col-md-5 page1"> | ||
<div class="agenda"> | ||
<?php | ||
if(!empty($_GET['search'])) { | ||
showContacts(getContactsPerPage($p)); | ||
$pg+=1; | ||
} elseif($pg == 1 && empty($_GET['search'])) { | ||
showMenu(); | ||
} | ||
else { | ||
showContacts(getContactsPerPage($p)); | ||
} | ||
?> | ||
</div> | ||
</div> | ||
<div class="controles"> | ||
</div> | ||
<div class="col-md-5 page2"> | ||
<div class="agenda"> | ||
<?php | ||
if($pg <= getMaxPage() && !isset($_GET['edit'])){ | ||
if(!empty($_GET['search'])) { | ||
showContacts(getContactsPerPage($p+$contact_per_page)); | ||
$pg+=1; | ||
} | ||
elseif($p == 0) { | ||
showContacts(getContactsPerPage($p)); | ||
} | ||
else { | ||
showContacts(getContactsPerPage($p+$contact_per_page)); | ||
$pg+=1; | ||
} | ||
} | ||
?> | ||
</div> | ||
</div> | ||
<div class="col-md-1"> | ||
<?php | ||
echo '<a href="contacts.php'; | ||
if(isset($_GET['search'])){ | ||
echo "?search=".$_GET['search'].'&'; | ||
if($pg<=getMaxPage()){ | ||
echo 'p='.$pg; | ||
} else { | ||
echo 'p='.'0'; | ||
} | ||
|
||
}else { | ||
echo '?'; | ||
if($pg<getMaxPage()){ | ||
echo 'p='.($pg+1); | ||
} else { | ||
echo 'p='.'0'; | ||
} | ||
} | ||
echo '" class="btn btn-default btn-foward">'; | ||
?> | ||
<img width="50px" src="images/back.png"/></a> | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
-- -------------------------------------------------------- | ||
-- Host: 127.0.0.1 | ||
-- Server version: 10.1.26-MariaDB-0+deb9u1 - Debian 9.1 | ||
-- Server OS: debian-linux-gnu | ||
-- HeidiSQL Version: 9.4.0.5125 | ||
-- -------------------------------------------------------- | ||
|
||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | ||
/*!40101 SET NAMES utf8 */; | ||
/*!50503 SET NAMES utf8mb4 */; | ||
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; | ||
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; | ||
|
||
-- Dumping structure for table agenda_telefonica.contact | ||
CREATE TABLE IF NOT EXISTS `contact` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT, | ||
`name` varchar(30) NOT NULL, | ||
`email` varchar(30) DEFAULT NULL, | ||
`address` varchar(200) DEFAULT NULL, | ||
`phone` varchar(30) DEFAULT NULL, | ||
PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8mb4; | ||
|
||
-- Dumping data for table agenda_telefonica.contact: ~11 rows (approximately) | ||
/*!40000 ALTER TABLE `contact` DISABLE KEYS */; | ||
INSERT INTO `contact` (`id`, `name`, `email`, `address`, `phone`) VALUES | ||
(11, 'luan', '[email protected]', 'dsadasds', '2233'), | ||
(12, 'carlos', '[email protected]', 'dsadadas', '3332'), | ||
(13, 'testador3', 'dasdasdas@dsadas', 'dasdasds', '123'), | ||
(14, 'testador4', 'dasdasds@dsadasds', 'dasdads', '232132'), | ||
(15, 'testador5', 'dasdas@dsadsa', 'dasdasda', '12323213'), | ||
(16, 'testador10', 'dsadaas@dsada', 'dsdas', '123'), | ||
(17, 'filipe', '[email protected]', 'sao paulo', '33442233'), | ||
(29, 'testador99', 'dsadas@dsda', 'dsadasdas', '1231223'), | ||
(30, 'antonio', '[email protected]', 'dsadsadsadsadas', '23123213'), | ||
(31, 'antonio', '[email protected]', 'dsadsadsadsadas', '23123213'), | ||
(32, 'joao3', 'dsadasd@dsadasd', 'dasdasdas', '1231231'); | ||
/*!40000 ALTER TABLE `contact` ENABLE KEYS */; | ||
|
||
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; | ||
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */; | ||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
body { | ||
color: #FFF; | ||
} | ||
.main-area { | ||
margin-top: 50px; | ||
} | ||
.agenda { | ||
background-color: #1a5276; | ||
border-radius: 2px; | ||
border: 1px solid black; | ||
padding: 50px; | ||
text-align: center; | ||
height: 600px; | ||
} | ||
.page1, .page2 { | ||
padding:0; | ||
} | ||
.contatos { | ||
/*color: #FFF;*/ | ||
} | ||
|
||
.borderless .controls td { | ||
border: none !important; | ||
padding: 0px; | ||
padding-bottom: .75rem; | ||
} | ||
.controls img { | ||
width: 25px; | ||
float: right; | ||
margin-left: 10px; | ||
} | ||
.space { | ||
height: 20px; | ||
} | ||
.btn-foward img{ | ||
transform: rotateY(180deg); | ||
} |
Oops, something went wrong.