php et mise en forme d'une requête mysql
Bonjour à tous,
voilà, j'ai suivi un tuto pour un système de news, et le résultat est plutot satisfaisant voici mes codes :
new.php (affiche automatiquement de façon aléatoire une news ) :
Code:
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
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
<title>Bienvenue sur mon site</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
h1, h3
{
text-align:center;
}
h3
{
background-color:black;
color:white;
font-size:0.9em;
margin-bottom:0px;
}
.news p
{
background-color:#CCCCCC;
margin-top:0px;
}
.news
{
width:70%;
margin:auto;
}
</style>
</head>
<body>
<h1> Bienvenue sur mon site !</h1>
<p> Voici les dernières news </p>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("news");
//ON récupère les 5 dernières news
$retour = mysql_query('SELECT * FROM news ORDER BY RAND() LIMIT 1');
while ($donnees = mysql_fetch_array($retour))
{
?>
<div class="news">
<h3>
<?php echo $donnees['titre']; ?>
<em><?php echo date('d/m/Y à h\hi'); ?></em>
</h3>
<p>
<?php
$contenu = nl2br(stripslashes($donnees['contenu']));
echo $contenu
?>
</p>
</div>
<?php
}
?>
</body>
</html> |
ensuite on a liste_news.php qui liste les news présentent dans la base de donnée :
Code:
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
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
<title>Liste des news</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
h2, th, td
{
text-align:center;
}
table
{
border-collapse:collapse;
border:2px solid black;
margin:auto;
}
th, td
{
border:1px solid black;
}
</style>
</head>
<body>
<h2> <a href="rediger_news.php">Ajouter une news</a></h2>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("news");
//verif 1 : veut-on poster une news?
if(isset($_POST['titre']) AND isset($_POST['contenu']))
{
$titre = mysql_real_escape_string($_POST['titre']);
$contenu = mysql_real_escape_string($_POST['contenu']);
//verif si c'est une modif de news ou pas
if($_POST['id_news'] == 0)
{
mysql_query("INSERT INTO news VALUES('', '" . $titre . "', '" . $contenu . "', '" . time() . "')");
}
else
{
//Protection faille sql
$_POST['$id_news'] = mysql_real_escape_string($_POST['id_news']);
mysql_query("UPDATE news SET titre='" . $titre . "', contenu = '". $contenu . "', where id = '". $_POST['id_news'] . "'");
}
}
//Verif 2 : veut-on supprimer une news?
if(isset($_GET['supprimer_news']))
{
$_GET['supprimer_news'] = mysql_real_escape_string($_GET['supprimer_news']);
mysql_query("DELETE FROM news WHERE id='". $_GET['supprimer_news'] . "'");
}
?>
<table>
<tr>
<th>Modifier</th>
<th>Supprimer</th>
<th>Titre</th>
<th>Date</th>
</tr>
<?php
$retour = mysql_query("SELECT * FROM news ORDER BY id DESC");
while($donnees = mysql_fetch_array($retour))
{
?>
<tr>
<td><?php echo '<a href="rediger_news.php?modifier_news= ' . $donnees['id'] .'">'; ?>Modifier</a></td>
<td><?php echo '<a href="liste_news.php?supprimer_news= ' . $donnees['id'] .'">'; ?>Supprimer</a></td>
<td><?php echo stripslashes($donnees['titre']); ?></td>
<td><?php echo date('d/m/Y'); ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html> |
et enfin rediger_news.php permettant de rédiger une news :
Code:
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
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
<title>Rédiger une news</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
h3, form
{
text-align:center;
}
</style>
<h3><a href="liste_news.php"> Retour vers la liste des news</a></h3>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("news");
//si on veut modifier une news
if(isset($_GET['modifier_news']))
{
$_GET['modifier_news'] = mysql_real_escape_string(htmlspecialchars($_GET['modifier_news']));
//Récupération info news correpondante
$retour = mysql_query("SELECT * FROM news WHERE id='". $_GET['modifier_news'] . "'");
$donnees = mysql_fetch_array($retour);
//on place titre et contenu dans une variable simple, $id_news sert à se souvenir que c'est une modif
$titre = stripslashes($donnees['titre']);
$contenu = stripslashes($donnees['contenu']);
$id_news = $donnees['id'];
}
else // Alors c'est une création de news
{
$titre = "";
$contenu = "";
$id_news = 0;
}
?>
<form action="liste_news.php" method="post">
<p> Titre : <input type="text" name="titre" value="<?php echo $titre; ?>" /></p>
<p>
Contenu :<br />
<textarea name="contenu" rows="30" cols="60">
<?php echo $contenu; ?>
</textarea>
<br />
<input type="hidden" name="id_news" value= "<?php echo $id_news; ?>" />
<input type="submit" value= "ok" />
</p>
</form>
</body>
</html> |
Bon ça fonctionne mais mon problème est le suivant:
les news à afficher sont en faite des poèmes, qui ont parfois une mise en forme spéciale selon les artistes. les retours à la ligne sont respectés mais pas les mots en gras ou en italique, y-a-t-il une possibilité de garder la mise en forme d'origine ou de la modifier après coût?