-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
123 lines (110 loc) · 3.05 KB
/
index.php
File metadata and controls
123 lines (110 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
require 'index.html';
$looking = isset( $_GET['title'] ) || isset( $_GET['author']);
/* Now access the link, http://localhost:8000/?author=HarperLee&title=To Kill a
Mockingbird. You will see that the page prints some of the information that you passed on
to the URL. */
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bookstore</title>
</head>
<body>
<p>You are: <?php echo $_COOKIE['username']?></p>
<p>Are you looking for a book'? <?php echo (int)$lookingForBook; ?></p>
<p>The book you are looking for is</p>
<ul>
<li><b>Title</b>: <?php echo $_GET['title']; ?></li>
<li><b>Author</b>: <?php echo $_GET['author']; ?></li>
</ul>
</body>
</html>
<?php
//mixed conditionals and HTML code in two ways. The first opens a php tag, and add if/else clause that print
//whether we are authenticated or not No HTML is merged within the conditionals, which makes it clear.
if (isset($_COOKIE['username']))
{
echo "you are " . $_COOKIE['username'];
}
else
{
echo "You are not authenticated.";
}
?>
<?php
if (isset( $_GET['title']) && isset( $_GET['author']))
{
//shows an uglier solution, when we have to print a lot of html, echo is not handy and it is better to close the php tag, print all HTML code
?>
<p>The book you are looking for is </p>
<ul>
<li><b>Title</b> : <?php echo $_GET['title']; ?></li>
<li><b>Author</b> : <?php echo $_GET['author']?></li>
</ul>
<?php
}
else
{
?>
<p>You are not looking for a book?</p>
<?php
} //http://www.angel.com/designpatterns/Bookstore/ that is the start;
?>
<?php
/* The last, but not least, type of loop is foreach. This loop is exclusive for arrays, and it
allows you to iterate an array entirely, even if you do not know its keys. There are two
options for the syntax, as you can see in the following examples:
*The foreach loop accepts an array—in this case $names—and it specifies a variable which
will contain the value of the entry of the array. You can see that we do not need to specify
any end condition, as PHP will know when the array has been iterated.
*/
$names = ['Angel', 'Ivan', 'Dragan'];
foreach ( $names as $name )
{
echo $name . " ";
}
foreach ( $names as $key => $name )
{
echo $key . " -> " . $name . " ";
}
?>
<?php
$books = [
[
'title' => 'To Kill A Mockingbird',
'author' => 'Harper Lee',
'available' => true,
'pages' => 336,
'isbn' => 9780061120084
],
[
'title' => '1984',
'author' => 'George Orwell',
'available' => true,
'pages' => 267,
'isbn' => 9780547249643
],
[
'title' => 'One Hundred Years Of Solitude',
'author' => 'Gabriel Garcia Marquez',
'available' => false,
'pages' => 457,
'isbn' => 9785267006323
],
]
?>
<ul>
<?php foreach ( $books as $book): ?>
<li>
<i> <?php echo $book['title']; ?></i>
<?php echo $book['author']; ?>
<?php if ( ! $book['available'] ): ?>
<b>Not available</b>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>