Skip to content

Commit bc956da

Browse files
committed
php add pizza
0 parents  commit bc956da

File tree

8 files changed

+255
-0
lines changed

8 files changed

+255
-0
lines changed

add.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
include('../tuto/config/db_connect.php');
3+
4+
$title = $email = $ingredients = '';
5+
$errors = array('email'=> '', 'title' => '', 'ingredients' => '');
6+
7+
if(isset($_POST['submit'])){
8+
// echo htmlspecialchars( $_POST['email']);
9+
// echo htmlspecialchars( $_POST['title']);
10+
// echo htmlspecialchars( $_POST['ingredients']);
11+
12+
// email check
13+
if(empty($_POST['email'])){
14+
$errors['email'] = '<div class="btn red">an email as required</div><br />';
15+
}else {
16+
$email = $_POST['email'];
17+
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
18+
$errors['email'] = '<div class="btn red">an email valid must valid</div><br />';
19+
20+
}
21+
}
22+
23+
// title check
24+
if(empty($_POST['title'])){
25+
$errors['title'] = '<div class="btn red">an title as required</div><br />';
26+
}else {
27+
$title = $_POST['title'];
28+
if(!preg_match('/^[a-zA-Z\s]+$/', $title)) {
29+
$errors['title'] = '<div class="btn red">Title must be letters</div><br />';
30+
}
31+
}
32+
33+
// ingredients check
34+
if(empty($_POST['ingredients'])){
35+
$errors['ingredients'] = '<div class="btn red">ingredient for pizza!</div><br />';
36+
}else {
37+
$ingredients = $_POST['ingredients'];
38+
if(!preg_match('/^([a-zA-Z\s]+)(,\s*[a-zA-Z\s]*)*$/', $ingredients)) {
39+
$errors['ingredients'] = '<div class="btn red">Ingredients must separated by coma</div><br />';
40+
}
41+
if(array_filter($errors)) {
42+
$errors['forminvalid'] = '<div class="btn red">form is valid</div><br />';
43+
44+
} else {
45+
$email = mysqli_real_escape_string($conn, $_POST['email']);
46+
$title = mysqli_real_escape_string($conn, $_POST['title']);
47+
$ingredients = mysqli_real_escape_string($conn, $_POST['ingredients']);
48+
$sql = "INSERT INTO pizzas(email,title,ingredients) VALUES('$email', '$title', '$ingredients')";
49+
if(mysqli_query($conn, $sql)) {
50+
//success
51+
}else {
52+
// error
53+
echo 'query error' . mysqli_error($conn);
54+
}
55+
mysqli_close($conn);
56+
$errors['formvalid'] = '<div class="btn green">form is send, redirect to index after 5 secondes</div><br />';
57+
header( "refresh:5;url=index.php" );
58+
}
59+
}
60+
}
61+
?>
62+
63+
<!DOCTYPE html>
64+
<html lang="en">
65+
<?php include('templates/header.php')?>
66+
<section class="container grey-text">
67+
<h4 class="center">add a pizza
68+
69+
</h4>
70+
<form action="" class="white" action="add.php" method="POST">
71+
<label for="">your mail:</label>
72+
73+
<input type="text" name="email" value="<?php echo htmlspecialchars( $email) ?>" >
74+
<div class="red-text"><?php echo $errors['email'] ?></div>
75+
<label for="">Pizza title:</label>
76+
<input type="text" name="title" value="<?php echo htmlspecialchars( $title) ?>">
77+
<div class="red-text"><?php echo $errors['title'] ?></div>
78+
<label for="">ingredients (comma separated):</label>
79+
<input type="text" name="ingredients" value="<?php echo htmlspecialchars( $ingredients) ?>">
80+
<div class="red-text"><?php echo $errors['ingredients'] ?></div>
81+
<div class="center">
82+
<div class="red-text"><?php echo $errors['forminvalid'] ?></div>
83+
<div class="red-text"><?php echo $errors['formvalid'] ?></div>
84+
85+
<input type="submit" name="submit" value="btn" class="btn brand z-depth-0">
86+
</div>
87+
</form>
88+
</section>
89+
<?php include('templates/footer.php')?>
90+
91+
</html>

config/db_connect.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php $conn = mysqli_connect('localhost', 'admin', 'admin', 'ninja_pizza');
2+
3+
if(!$conn){
4+
echo 'connection errors' . mysqli_connect_error();
5+
}else{
6+
7+
} ?>

details.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
include('../tuto/config/db_connect.php');
3+
4+
if(isset($_POST['delete'])) {
5+
$id_to_delete = mysqli_real_escape_string($conn, $_POST['id_to_delete']);
6+
$sql = "DELETE FROM pizzas WHERE id = $id_to_delete";
7+
8+
if(mysqli_query($conn, $sql)){
9+
header('Location:index.php');
10+
}else {
11+
//error
12+
echo 'query error' . mysqli_error($conn);
13+
}
14+
}
15+
16+
if(isset($_GET['id'])) {
17+
$id = mysqli_real_escape_string($conn, $_GET['id']);
18+
19+
$sql = "SELECT * FROM pizzas WHERE id = $id";
20+
21+
$result = mysqli_query($conn, $sql);
22+
23+
$pizza = mysqli_fetch_assoc($result);
24+
25+
mysqli_free_result($result);
26+
mysqli_close($conn);
27+
28+
}
29+
?>
30+
<!DOCTYPE html>
31+
<html lang="en">
32+
<?php include('templates/header.php')?>
33+
<h2 class="center">Details</h2>
34+
<div class="container center">
35+
<?php if($pizza): ?>
36+
<h4><?php echo htmlspecialchars( $pizza['title']); ?></h4>
37+
<p>created by:<?php echo htmlspecialchars( $pizza['email']); ?></p>
38+
<p>created at:<?php date( $pizza['created_at']); ?></p>
39+
<div><?php echo htmlspecialchars( $pizza['ingredients']); ?></div>
40+
<form action="details.php" method="POST">
41+
<input type="hidden" value="<?php echo $pizza['id'] ?>" name="id_to_delete">
42+
<input type="submit" name="delete" value="Delete" class="btn red z-depth-0">
43+
</form>
44+
<?php else: ?>
45+
<h5 class='red-text'>no pizza here!</h5>
46+
<?php endif; ?>
47+
</div>
48+
<?php include('templates/footer.php')?>
49+
50+
</html>

img/pizza.jpg

43.3 KB
Loading

index.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
include('../tuto/config/db_connect.php');
4+
5+
// query from database pizza
6+
$sql = 'SELECT title, ingredients, id FROM pizzas ORDER BY created_at';
7+
$result = mysqli_query($conn, $sql);
8+
$pizzas = mysqli_fetch_all($result, MYSQLI_ASSOC);
9+
$countPizza = count($pizzas) >= 2 ? '<p>there are 2 pizzas or more</p>' : '<p>There few or less pizza than 2</p>';
10+
mysqli_free_result($result);
11+
// close database connection
12+
mysqli_close($conn);
13+
14+
15+
16+
?>
17+
18+
<!DOCTYPE html>
19+
<html lang="en">
20+
<?php include('templates/header.php')?>
21+
<h4 class="center grey-text">pizza!</h4>
22+
<div class="container">
23+
<div class="row">
24+
<?php foreach($pizzas as $pizza): ?>
25+
<div class="col s6 md3">
26+
<div class="card z-depth-0">
27+
<img src="./img/pizza.jpg" class="pizza">
28+
<div class="card-content center">
29+
<h6><?php echo $pizza['title'] ?></h6>
30+
<ul>
31+
<?php foreach(explode(',', $pizzas[0]['ingredients']) as $ing): ?>
32+
<li class='btn green m4'><?php echo htmlspecialchars( $ing) ?></li><br />
33+
<?php endforeach ?>
34+
</ul>
35+
</div>
36+
<div class="card-action right-align">
37+
<a href="details.php?id=<?php echo $pizza['id'] ?>" class="brand-text">more info</a>
38+
</div>
39+
</div>
40+
</div>
41+
42+
<?php endforeach ?>
43+
<?php echo $countPizza; ?>
44+
</div>
45+
</div>
46+
<?php include('templates/footer.php')?>
47+
48+
</html>

sandbox.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
$score = 50;
3+
4+
$score > 40 ? 'high score' : 'low score';
5+
?>
6+
7+
<!DOCTYPE html>
8+
<html lang="en">
9+
<head>
10+
<meta charset="UTF-8">
11+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
12+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
13+
<title>Document</title>
14+
</head>
15+
<body>
16+
17+
</body>
18+
</html>

templates/footer.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<footer class="section">
2+
<div class="center grey-text">copyright 2022 codingbdx </div>
3+
</footer>
4+
</body>

templates/header.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<head>
2+
<meta charset="UTF-8">
3+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5+
<title>Document</title>
6+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
7+
<style>
8+
.brand {
9+
background-color: red!important;
10+
}
11+
.brand-text {
12+
color: gray!important
13+
;
14+
}
15+
form {
16+
max-width: 70vw;
17+
margin: auto;
18+
padding: 0.7em;
19+
}
20+
.pizza {
21+
width: 100px;
22+
margin: 40px auto -30px;
23+
display: block;
24+
position: relative;
25+
top:-40px;
26+
}
27+
</style>
28+
</head>
29+
<body class="grey lighten-4">
30+
<nav class="white z-depth-0">
31+
<div class="container">
32+
<a href="index.php" class="brand-logo brand-text">Ninja pizza</a>
33+
<ul id="nav-mobile" class="right hide-on-small-and-down">
34+
<li><a href="add.php" class="btn brand z-depth-0">add a pizza</a></li>
35+
</ul>
36+
</div>
37+
</nav>

0 commit comments

Comments
 (0)