Precedente :: Successivo |
Autore |
Messaggio |
mdweb Dio maturo


Registrato: 18/12/07 16:59 Messaggi: 4412
|
Inviato: 25 Gen 2008 17:40 Oggetto: php e mysql per articoli |
|
|
Ciao a tutti vorrei costruire un sito di quelli dinamici...Di solito mi occupo della creazione di parte statica dei siti ma stavolta il mio cliente mi ha chiesto di creargli un sito dinamico...So le basi di php ecco cosa ho sviluppato:
Codice: | <?php
$limit = 5; // articoli per pagina
$mysql = new mysqli('localhost', 'root', '', 'casticrew');
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>
|
Questo è il codice che si usa per estrarre gli articoli da mysql ma io come ce li metto in mysql gli articoli?Avrei bisogno di una cosa semplice niente di che per favore aiutatemi... |
|
Top |
|
 |
freemind Supervisor sezione Programmazione


Registrato: 04/04/07 21:28 Messaggi: 4643 Residenza: Internet
|
Inviato: 26 Gen 2008 03:04 Oggetto: |
|
|
Dovrai avere una paginetta da qualche parte con una form che cicli e scriva nel db.
Da quel che vedo hai due tabelle: articles e authors. Dalla prima tiri fuori l'id, il titolo e una descrizione, mentre dalla seconda cognome e nome dell'autore.
Potresti provare con una pagina così:
Codice: |
<?
$mysql = new mysqli('localhost', 'root', '', 'casticrew');
if(!$mysql)
{
die("Errore di connessione al database, impossibile procedere");
}
// qui scriviamo nel db se è settato il comando Salva
if (isset($_REQUEST['Command']) && $_REQUEST['Command']=='Salva')
{
// Inseriamo nella tabella articles
$Insert
= "INSERT INTO articles (id,title,article) "
. "VALUES("
. "'" . $mysql->real_escape_string($_REQUEST['id']) . "',"
. "'" . $mysql->real_escape_string($_REQUEST['titolo']) . "',"
. "'" . $mysql->real_escape_string($_REQUEST['descr']) . "'"
. ") ";
$mysql->query($Insert);
// Inseriamo nella tabella authors
$Insert
= "INSERT INTO authors (surname,name) "
. "VALUES("
. "'" . $mysql->real_escape_string($_REQUEST['cognome']) . "',"
. "'" . $mysql->real_escape_string($_REQUEST['nome']) . "'"
. ") ";
$mysql->query($Insert);
}
?>
<!-- Al posto di questo commento ci metti l'header html della pagina -->
<body>
<!-- qui mettiamo la form -->
<form name="frm" method="post" action="<? print $_SERVER['PHP_SELF'];?>">
Id <input type="text" name="id"><br>
Titolo <input type="text" name="titolo"><br>
Descr <input type="text" name="descr"><br>
Cognome <input type="text" name="cognome"><br>
nome <input type="text" name="nome"><br>
<input type="submit" name="Command" value="Salva">
</form>
</body>
|
Ho ipotizzato che articles abbia l'id auto_increment e non ho fatto nessun tipo di controllo sulla chiave di authors.
NBon ho provato ma mi pare che le query siano corrette.
Secondo me la select che usi per tirar fuori i prodotti dovrebbe diventare questa:
Codice: |
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 INNER JOIN authors AU ON AR.author_id = AU.id
ORDER BY id DESC
LIMIT ".(($page - 1) * $limit).",".$limit);
|
Ovviamente è uno schema molto brutale ma l'idea è questa. |
|
Top |
|
 |
mdweb Dio maturo


Registrato: 18/12/07 16:59 Messaggi: 4412
|
Inviato: 26 Gen 2008 21:51 Oggetto: |
|
|
Con questa pagina che devo fare?
tipo ne devo fa 2 una con il cdice mio una con il codice tuo come faccio?
La tua la salvo come insert.php e poi devo creare un tabella artcles nel databse o no? |
|
Top |
|
 |
mdweb Dio maturo


Registrato: 18/12/07 16:59 Messaggi: 4412
|
Inviato: 27 Gen 2008 10:41 Oggetto: |
|
|
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:
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:
Codice: |
<?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:
Codice: |
<?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:
Codice: |
<?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? |
|
Top |
|
 |
mdweb Dio maturo


Registrato: 18/12/07 16:59 Messaggi: 4412
|
Inviato: 27 Gen 2008 13:57 Oggetto: |
|
|
http://www.casticrew.altervista.org/
su locale mi fa quello che ho detto in precendeza su altervista mi da questo:Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /membri/casticrew/index.php on line 11 |
|
Top |
|
 |
mdweb Dio maturo


Registrato: 18/12/07 16:59 Messaggi: 4412
|
Inviato: 28 Gen 2008 22:01 Oggetto: |
|
|
Penso che l'errore stia nelòla select oppure in netratto in cui richami il titolo :
<?php
printf.....<!---Questa parte qua-->?> |
|
Top |
|
 |
mdweb Dio maturo


Registrato: 18/12/07 16:59 Messaggi: 4412
|
Inviato: 01 Feb 2008 20:54 Oggetto: |
|
|
Ho provato parecchie modifiche ad esempio :visto che delimito la stringa da stampare con i single quote (') invece che con i double quote ("), ho provato a sostituirli (i single quote stampano la stringa così com'è, senza sostituire le variabili)
Codice: | 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']
); |
Però il risultato è sempre lo stesso non viene mostrato l'elenco degli articoli.Che posso fare? |
|
Top |
|
 |
mdweb Dio maturo


Registrato: 18/12/07 16:59 Messaggi: 4412
|
Inviato: 04 Feb 2008 20:10 Oggetto: |
|
|
lascio anche la query SQL
Codice: | -- phpMyAdmin SQL Dump
-- version 2.9.1.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generato il: 04 Feb, 2008 at 05:10 PM
-- Versione MySQL: 5.0.27
-- Versione PHP: 5.2.0
--
-- Database: `provacast`
--
-- --------------------------------------------------------
--
-- Struttura della tabella `articles`
--
CREATE TABLE `articles` (
`id` int(10) unsigned NOT NULL auto_increment,
`author_id` int(10) unsigned NOT NULL,
`title` varchar(100) NOT NULL,
`article` text NOT NULL,
PRIMARY KEY (`id`),
KEY `author_id` (`author_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
--
-- Dump dei dati per la tabella `articles`
--
INSERT INTO `articles` (`id`, `author_id`, `title`, `article`) VALUES
(1, 0, 'Daniele', 'sdsadsadf'),
(2, 0, 'dfgfdg', 'dfgdgdfg'),
(3, 0, 'dfgdg', 'dfgdfgdfg'),
(4, 0, 'nbmbm', 'nbmnbmnbmbmnbm'),
(5, 0, 'hgjhjhg', 'hgjhgjgh'),
(6, 0, 'dsfds', 'dsfsdfsdf'),
(7, 0, 'prova', 'dsfsdafsda'),
(8, 0, 'uyiyiyijhkjhk', 'uyfivhgkjhnmjhvkjnmnmnbmbm'),
(9, 0, 'vbgnvb', 'vbnvnvb');
-- --------------------------------------------------------
--
-- Struttura della tabella `authors`
--
CREATE TABLE `authors` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
`surname` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Dump dei dati per la tabella `authors`
--
-- --------------------------------------------------------
--
-- Struttura della tabella `news`
--
CREATE TABLE `news` (
`id` int(5) unsigned NOT NULL auto_increment,
`titolo` varchar(255) NOT NULL,
`testo` text NOT NULL,
`data` int(11) default NULL,
`autore` varchar(50) default NULL,
`mail` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Dump dei dati per la tabella `news`
-- |
|
|
Top |
|
 |
mdweb Dio maturo


Registrato: 18/12/07 16:59 Messaggi: 4412
|
Inviato: 05 Feb 2008 20:30 Oggetto: |
|
|
Inserisco il file insertp.php :
Codice: | <?php
$mysql = new mysqli('localhost', 'root', '', 'provacast');
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> |
Forse quello che sbaglio è a non inserire L'autore quando scrivo l'articolo....Il problema è che non posso inserirlo perchè con il seguente codice viene fuori una tendina dove si può solamente selezionare l'autore però neanche me lo fa selezionare perchè la tendina è vuota.Ecco il codice:
Codice: |
<label>Autore:</label> <select name="author">
<?php
while($author = $authors->fetch_assoc())
{
echo "<option value=".$author['id'].">".$author['fullname']."</option>";
}
?>
</select>
|
Credo di avere sbagliato li perchè Non essendoci l'autore il risultato della query è di 0 righe. |
|
Top |
|
 |
SverX Supervisor Macchinisti


Registrato: 25/03/02 12:16 Messaggi: 11810 Residenza: Tokelau
|
Inviato: 06 Feb 2008 13:19 Oggetto: |
|
|
'la tendina' ( il campo SELECT ) è vuoto probabilmente perchè la tabella 'authors' è vuota... |
|
Top |
|
 |
mdweb Dio maturo


Registrato: 18/12/07 16:59 Messaggi: 4412
|
Inviato: 06 Feb 2008 14:29 Oggetto: |
|
|
il problema è che non so come riempirla!Come devo fare ??? Tramite phpmyadmin???? |
|
Top |
|
 |
SverX Supervisor Macchinisti


Registrato: 25/03/02 12:16 Messaggi: 11810 Residenza: Tokelau
|
Inviato: 06 Feb 2008 17:01 Oggetto: |
|
|
mdweb ha scritto: | il problema è che non so come riempirla!Come devo fare ??? Tramite phpmyadmin???? |
per cominciare direi di sì  |
|
Top |
|
 |
mdweb Dio maturo


Registrato: 18/12/07 16:59 Messaggi: 4412
|
Inviato: 06 Feb 2008 17:41 Oggetto: |
|
|
visto che sto inizioando come devo fare?
Mille grazie  |
|
Top |
|
 |
SverX Supervisor Macchinisti


Registrato: 25/03/02 12:16 Messaggi: 11810 Residenza: Tokelau
|
Inviato: 06 Feb 2008 17:49 Oggetto: |
|
|
mdweb ha scritto: | visto che sto inizioando come devo fare? |
Dovresti aver notato -in phpMyAdmin- che quando selezioni una tabella è presente il tasto apposito per inserire dei record...
la documentazione ufficiale in Italiano è qui: http://www.phpmyadmin.net/pma_localized_docs/Documentation-it.html |
|
Top |
|
 |
|
|
Non puoi inserire nuovi argomenti Non puoi rispondere a nessun argomento Non puoi modificare i tuoi messaggi Non puoi cancellare i tuoi messaggi Non puoi votare nei sondaggi
|
|