Ciao a tutti ho creato unp script php che permette di scrivere articoli e salvarli in mysql allora per prima cosa ho creato un file.sql con scritto :
Codice:
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)
);
poi ho creato un file insert.php per inserire gli articoli:
Codice PHP:
<?php
$limit = 5; // articoli per pagina
$mysql = new mysqli('localhost', 'root', '', 'provacast');
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>
poi ho creato un file show.php e ho inserito per leggere gli articoli:
Codice PHP:
<?php
$mysql = new mysqli('localhost', 'root', '', 'provacast');
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>
infine ho creato l'index.php per linkare gli articoli in modo che tutti possano vederli:
Codice PHP:
<?php
$limit = 5; // articoli per pagina
$mysql = new mysqli('localhost', 'root', '', 'provacast');
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>
Gli articoli li inserisce perà non me li linka .l'errore pesno che stia sell'index ma non lo trovo...Gli articoli li insrisce perchè nell'index che ho testato in locale 'era scritto articoli totali 2 ma poi non c'era il linka che mi rimandava al file show.php (per leggere gli articoli in dettagli) può essere perchè ho tralasciato il campo autore?Cioè l'errore è dovuto a me che quando ho scritto l'articolo ho tralasciao il capmo autore?
EDIT:
Codice PHP:
?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']
);
}
?>
secondo me l'errore sta qua o no?