12.11.13 -
0
<?php
$cnx = mysql_connect('localhost', 'NomUtisateur', 'MotDePasse');
mysql_select_db('MaBase', $cnx);
$result = mysql_query('SELECT * FROM article', $cnx);
?>
<!doctype html>
<html>
<head>
<title>PHP MVC</title>
</head>
<body>
<h2>Liste des articles</h2>
<ul>
<?php while($row = mysql_fetch_assoc($result)){ ?>
<li>
<a href="/show.php?id=<?php echo $row['id']; ?>">
<?php echo $row['titre']; ?>
</a>
</li>
<?php } ?>
</ul>
</body>
</html>
<?php mysql_close($cnx); ?>
<?php
// index.php
$cnx = mysql_connect('localhost', 'NomUtilisateur', 'MotDePasse');
mysql_select_db('MaBase', $cnx);
$result = mysql_query('SELECT * FROM article', $cnx);
$articles = array();
while($row = mysql_fetch_assoc($result)){
$articles[] = $row;
}
mysql_close($cnx);
require 'view/list.php';
?>
<!doctype html>
<html>
<head>
<title>PHP MVC</title>
</head>
<body>
<h2>Liste des articles</h2>
<ul>
<?php foreach($articles as $article){ ?>
<li>
<a href="/show.php?id=<?php echo $article['id']; ?>">
<?php echo $article['titre']; ?>
</a>
</li>
<?php } ?>
</ul>
</body>
</html>
<?php
// model/model.php
function ouvrir_database()
{
$cnx = mysql_connect('localhost', 'NomUtilisateur', 'MotDePasse');
mysql_select_db('MaBase', $cnx);
return $cnx;
}
function fermer_database($cnx)
{
mysql_close($cnx);
}
function all_articles()
{
$cnx = ouvrir_database();
$result = mysql_query('SELECT * from article', $cnx);
$articles = array();
while($row = mysql_fetch_assoc($result)){
$articles[] = $row;
}
fermer_database($cnx);
return $articles;
}
?>
<?php
// index.php
require_once 'model/model.php';
$articles = all_articles();
require 'view/list.php';
?>
<!-- view/layout.php -->
<!doctype html>
<html>
<head>
<title><?php echo $titre; ?></title>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
<!-- view/list.php -->
<?php $titre = 'PHP MVC'; ?>
<?php ob_start() ?>
<h2>Liste des articles</h2>
<ul>
<?php foreach($articles as $article){ ?>
<li>
<a href="/show.php?id=<?php echo $article['id']; ?>">
<?php echo $article['titre']; ?>
</a>
</li>
<?php } ?>
</ul>
<?php $content = ob_get_clean(); ?>
<?php include 'layout.php'; ?>
// model.php
function detail_article($id)
{
$cnx = ouvrir_database();
$query = 'SELECT * FROM article WHERE id='.$id;
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
fermer_database($cnx);
return $row;
}
<?php
// show.php
require_once 'model/model.php';
$article = detail_article($_GET['id']);
require 'view/show.php';
?>
<!-- view/show.php -->
<?php $titre = 'Détail'; ?>
<?php ob_start(); ?>
<h2><?php echo $article['titre']; ?></h2>
<div>
<?php echo $article['contenu']; ?>
</div>
<div><?php echo $article['date_creation']; ?></div>
<?php $content = ob_get_clean(); ?>
<?php include 'layout.php'; ?>