-
Notifications
You must be signed in to change notification settings - Fork 2
/
news.php
141 lines (127 loc) · 6.33 KB
/
news.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php include("config.php");
if (!isset($_SESSION['connected_id'])) {
header("Location: login.php");
exit();
}
// Get the user ID from the session
$userId = intval($_SESSION['connected_id']);?>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>ReSoC - Actualités</title>
<meta name="author" content="Julien Falconnet">
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<header>
<a href='login.php'><img src="resoc.jpg" alt="Logo de notre réseau social"/></a>
<nav id="menu">
<a href="news.php">Actualités</a>
<a href="wall.php?user_id=<?php echo $userId; ?>">Mur</a>
<a href="feed.php?user_id=<?php echo $userId; ?>">Flux</a>
<a href="tags.php?tag_id=<?php echo $userId; ?>">Mots-clés</a>
</nav>
<nav id="user">
<a href="#">▾ Profil</a>
<ul>
<li><a href="login.php?user_id=<?php echo $userId; ?>">Login</a></li>
<li><a href="settings.php?user_id=5">Paramètres</a></li>
<li><a href="followers.php?user_id=5">Mes suiveurs</a></li>
<li><a href="subscriptions.php?user_id=5">Mes abonnements</a></li>
</ul>
</nav>
</header>
<div id="wrapper">
<aside>
<img src="user.jpg" alt="Portrait de l'utilisatrice"/>
<section>
<h3>Présentation</h3>
<p>Sur cette page vous trouverez les derniers messages de
tous les utilisatrices du site.</p>
</section>
</aside>
<main>
<?php
/*
// C'est ici que le travail PHP commence
// Votre mission si vous l'acceptez est de chercher dans la base
// de données la liste des 5 derniers messsages (posts) et
// de l'afficher
// Documentation : les exemples https://www.php.net/manual/fr/mysqli.query.php
// plus généralement : https://www.php.net/manual/fr/mysqli.query.php
*/
// Etape 1: Ouvrir une connexion avec la base de donnée.
//verification
if ($mysqli->connect_errno)
{
echo "<article>";
echo("Échec de la connexion : " . $mysqli->connect_error);
echo("<p>Indice: Vérifiez les parametres de <code>new mysqli(...</code></p>");
echo "</article>";
exit();
}
// Etape 2: Poser une question à la base de donnée et récupérer ses informations
// cette requete vous est donnée, elle est complexe mais correcte,
// si vous ne la comprenez pas c'est normal, passez, on y reviendra
$laQuestionEnSql = "
SELECT posts.content,
posts.created,
users.id as author_id,
users.alias as author_name,
count(likes.id) as like_number,
GROUP_CONCAT(DISTINCT tags.label) AS taglist
FROM posts
JOIN users ON users.id=posts.user_id
LEFT JOIN posts_tags ON posts.id = posts_tags.post_id
LEFT JOIN tags ON posts_tags.tag_id = tags.id
LEFT JOIN likes ON likes.post_id = posts.id
GROUP BY posts.id
ORDER BY posts.created DESC
LIMIT 15
";
$lesInformations = $mysqli->query($laQuestionEnSql);
// Vérification
if ( ! $lesInformations)
{
echo "<article>";
echo("Échec de la requete : " . $mysqli->error);
echo("<p>Indice: Vérifiez la requete SQL suivante dans phpmyadmin<code>$laQuestionEnSql</code></p>");
exit();
}
// Etape 3: Parcourir ces données et les ranger bien comme il faut dans du html
// NB: à chaque tour du while, la variable post ci dessous reçois les informations du post suivant.
while ($post = $lesInformations->fetch_assoc())
{
//la ligne ci-dessous doit etre supprimée mais regardez ce
//qu'elle affiche avant pour comprendre comment sont organisées les information dans votre
// echo "<pre>" . print_r($post, 1) . "</pre>";
// @todo : Votre mission c'est de remplacer les AREMPLACER par les bonnes valeurs
// ci-dessous par les bonnes valeurs cachées dans la variable $post
// on vous met le pied à l'étrier avec created
//
// avec le ? > ci-dessous on sort du mode php et on écrit du html comme on veut... mais en restant dans la boucle
?>
<article>
<h3>
<time><?php echo $post['created'] ?></time>
</h3>
<address><a href="wall.php?user_id=<?php echo $post['author_id']; ?>"><?php echo htmlspecialchars($post['author_name']); ?></a></address>
<div>
<!-- ci-dessous on enleve le caractere # du text( content) -->
<?php $cleaned_content = str_replace('#', '', $post['content']);
echo "<p>" . htmlspecialchars($cleaned_content) . "</p>";?>
</div>
<footer>
<small>♥<?php echo $post['like_number'] ?> </small>
<a href=""><?php echo "#",$post['taglist'] ?></a>,
</footer>
</article>
<?php
// avec le <?php ci-dessus on retourne en mode php
}// cette accolade ferme et termine la boucle while ouverte avant.
?>
</main>
</div>
</body>
</html>