ho preso questo modello per creare un sito dinamico... quindi ho creato le tabelle nel db:
Codice PHP:
CREATE TABLE authors (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
surname VARCHAR(100) NOT NULL,
PRIMARY KEY(id)
);
CREATE TABLE articles (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
author_id INT UNSIGNED NOT NULL,
title VARCHAR(100) NOT NULL,
article TEXT NOT NULL,
PRIMARY KEY(id),
KEY(author_id)
);
e poi ho creato le tre pagine:
index.php
Codice PHP:
<?php
$limit = 5; // articoli per pagina
$mysql = new mysqli('localhost', 'root', '', 'html_it_articles');
if(!$mysql)
{
die("Errore di connessione al database, impossibile procedere");
}
$result = $mysql->query("SELECT COUNT(*) AS tot FROM articles")->fetch_assoc();
$page = isset($_GET['p']) ? $_GET['p'] : 1;
$totals = $result['tot'];
$totals_pages = ceil($totals / $limit);
$articles = $mysql->query("
SELECT
AR.id AS id,
AR.title AS title,
CONCAT(SUBSTR(AR.article, 1, 200), ' ...') AS content,
CONCAT(AU.surname, ' ', AU.name) AS author
FROM
articles AR,
authors AU
WHERE
AR.author_id = AU.id
ORDER BY id DESC
LIMIT ".(($page - 1) * $limit).",".$limit);
?>
<html>
<head>
<title>Articoli</title>
</head>
<body>
<ul>
<li><a href="index.php">Lista articoli</a></li>
<li><a href="insert.php">Inserisci un articolo</a></li>
</ul>
<p>Articoli totali: <?php echo $totals; ?></p>
<table width="500px">
<?php
while($article = $articles->fetch_assoc())
{
printf('<tr>
<td>%d. <a href="show.php?id=%d">%s</a> (%s) </td>
</tr>
<tr>
<td><p>%s</p></td>
</tr>
<tr>
<td><hr /></td>
</tr>',
$article['id'],
$article['id'],
$article['title'],
$article['author'],
$article['content']
);
}
?>
</table>
<p>Pagina <?php echo $page; ?> di <?php echo $totals_pages; ?> <br />
<?php
if($page - 1 > 0)
{
echo '<a href="?p='.($page - 1).'">< prev</a> | ';
}else
{
echo '< prev | ';
}
if($page + 1 <= $totals_pages)
{
echo '<a href="?p='.($page + 1).'">next ></a>';
}else
{
echo 'next >';
}
?>
</p>
</body>
</html>
show.php
Codice PHP:
<?php
$mysql = new mysqli('localhost', 'root', '', 'html_it_articles');
if(!$mysql)
{
die("Errore di connessione al database, impossibile procedere");
}
if(!isset($_GET['id']))
{
header('Location: index.php');
}
$article = $mysql->query("
SELECT
AR.id AS id,
AR.title AS title,
AR.article AS content,
CONCAT(AU.surname, ' ', AU.name) AS author
FROM
articles AR,
authors AU
WHERE
AR.author_id = AU.id AND
AR.id = ".$_GET['id'])->fetch_assoc();
?>
<html>
<head>
<title>Articolo (<?php echo $article['id']; ?>)</title>
</head>
<body>
<ul>
<li><a href="index.php">Lista articoli</a></li>
<li><a href="insert.php">Inserisci un articolo</a></li>
</ul>
<h3><?php echo $article['title']; ?></h3>
<i><?php echo $article['author']; ?></i>
<p>
<?php echo $article['content']; ?>
</p>
</body>
</html>
insert.php
Codice PHP:
<?php
$mysql = new mysqli('localhost', 'root', '', 'html_it_articles');
if(!$mysql)
{
die("Errore di connessione al database, impossibile procedere");
}
if(isset($_POST['action']) and $_POST['action'] == 'insert')
{
$mysql->query("INSERT INTO articles VALUES ('', '".$_POST['author']."', '".addslashes($_POST['title'])."', '".addslashes($_POST['article'])."')");
header('Location: index.php');
}
$authors = $mysql->query("SELECT id, CONCAT(surname, ' ', name) AS fullname FROM authors ORDER BY surname ASC");
?>
<html>
<head>
<title>Inserimento articolo</title>
</head>
<body>
<ul>
<li><a href="index.php">Lista articoli</a></li>
<li><a href="insert.php">Inserisci un articolo</a></li>
</ul>
<h3>Inserisci un articolo</h3>
<form action="" method="post">
<input type="hidden" name="action" value="insert" />
<label>Autore:</label> <select name="author">
<?php
while($author = $authors->fetch_assoc())
{
echo "<option value=".$author['id'].">".$author['fullname']."</option>";
}
?>
</select><br />
<label>Titolo:</label> <input type="text" name="title" size="55"/><br />
<label>Text:</label><br />
<textarea name="article" rows="6" cols="60"></textarea><br />
<input type="submit" value="Salva" />
</form>
</body>
</html>
Quando poi provo ad aprire index.php mi dą errore alla stringa 11 why?