|
Home -> Manuali e Tutorials -> Guida PHP -> TagBoard (1/3)
Scarica il tutorial | Stampa il tutorial | Cerca nel sito
REALIZZARE UNA TAG BOARD IN PHP
Di seguito verrā spiegato come realizzare una tag sia con supporto di un database MySQL (Anteprima, ) e sia con l'uso di file di testo .txt (senza database quindi; Anteprima, ).
Creare una Tag con supporto MySQL
Vediamo innanzitutto il database necessario per l'uso della Tag; chiamiamo il database "tag" e sarā costituito da una sola tabella "dati":
CREATE TABLE dati (
id int(11) NOT NULL auto_increment,
nome varchar(100) NOT NULL default '',
email varchar(150) NOT NULL default '',
message longtext NOT NULL default '',
data varchar(100) NOT NULL default '',
www varchar(150) NOT NULL default '',
PRIMARY KEY (id)
)
|
La Tag verrā realizzata su quattro files: smilies.php , tag.php , fmysql.php , tagboard.js . Il primo file contiene semplicemente l'elenco dei percorsi delle smiles, mentre il file javascript serve semplicemente a costruire la pop-up per postare le smiles. Gli altri due files php servono per il funzionamento della tag stessa.
Il file js lo trovate nel file zip disponibile per il download, mentre di seguito č riportato il contenuto del file smilies.php, contenente un'array $smile_array, le cui chiavi sono le stringhe corrispondenti ai codici delle smiles, mentre i valori sono i tag IMG corrispondenti.
<?php
$smile_array[':blink'] = "<IMG SRC=smilies/blink.gif width=20 height=20 border=0>";
$smile_array[':byesigh'] = "<IMG SRC=smilies/bye2.gif width=26 height=18 border=0>";
$smile_array[':clap'] = "<IMG SRC=smilies/clap.gif width=31 height=25 border=0>";
$smile_array[':cool'] = "<IMG SRC=smilies/cool.gif width=20 height=20 border=0>";
$smile_array[':crybaby'] = "<IMG SRC=smilies/crybaby.gif width=40 height=18 border=0>";
$smile_array[':dry'] = "<IMG SRC=smilies/dry.gif width=20 height=20 border=0>";
$smile_array[':blushing'] = "<IMG SRC=smilies/blushing.gif width=20 height=20 border=0>";
$smile_array[':gratta?'] = "<IMG SRC=smilies/g.gif width=25 height=23 border=0>";
$smile_array[':hands'] = "<IMG SRC=smilies/hands.gif width=46 height=22 border=0>";
$smile_array[':Happy'] = "<IMG SRC=smilies/happy.gif width=20 height=20 border=0>";
$smile_array[':Huh2'] = "<IMG SRC=smilies/huh2.gif width=20 height=20 border=0>";
$smile_array[':icon3'] = "<IMG SRC=smilies/icon3.gif width=19 height=19 border=0>";
$smile_array[':icon9'] = "<IMG SRC=smilies/icon9.gif width=19 height=19 border=0>";
$smile_array[':laughing1'] = "<IMG SRC=smilies/laughing1.gif width=35 height=30 border=0>";
$smile_array[':kicking'] = "<IMG SRC=smilies/kicking.gif width=28 height=23 border=0>";
$smile_array[':Mad'] = "<IMG SRC=smilies/mad.gif width=20 height=20 border=0>";
$smile_array[':No'] = "<IMG SRC=smilies/no.gif width=20 height=20 border=0>";
$smile_array[':ohmy'] = "<IMG SRC=smilies/ohmy.gif width=20 height=20 border=0>";
$smile_array[':Sad'] = "<IMG SRC=smilies/sad.gif width=20 height=20 border=0>";
$smile_array[':scratch'] = "<IMG SRC=smilies/scratch.gif width=30 height=26 border=0>";
$smile_array[':shiny'] = "<IMG SRC=smilies/shiny.gif width=18 height=18 border=0>";
$smile_array[':))'] = "<IMG SRC=smilies/icon6.gif width=19 height=19 border=0>";
$smile_array[':)'] = "<IMG SRC=smilies/smile.gif width=20 height=20 border=0>";
$smile_array[':P'] = "<IMG SRC=smilies/tongue.gif width=20 height=20 border=0>";
$smile_array[':unsure'] = "<IMG SRC=smilies/unsure.gif width=20 height=20 border=0>";
$smile_array[':wacko'] = "<IMG SRC=smilies/wacko.gif width=20 height=20 border=0>";
$smile_array[':weeping'] = "<IMG SRC=smilies/weeping.gif width=18 height=18 border=0>";
$smile_array[':yawn'] = "<IMG SRC=smilies/yawn.gif width=18 height=18 border=0>";
$smile_array[':Yeah2'] = "<IMG SRC=smilies/yeah2.gif width=28 height=28 border=0>";
$smile_array[':bye'] = "<IMG SRC=smilies/bye1.gif width=26 height=18 border=0>";
?>
|
Di seguito č invece riportato il codice del file tag.php, file principale dell'applicazione:
<?php
define("TAG_DIR", realpath("."));
// sezione PHP in cui viene eseguito il controllo dei campi inseriti, il controllo di mess giā presente nel db e l'inserimento
if ($_POST["nome"]!="NOME" && $_POST["message"]!="" && $_POST["nome"]!="nome")
{
include_once(TAG_DIR."/fmysql.php");
include_once(TAG_DIR."/smilies.php");
$conn = connessione("tag");
foreach($smile_array as $smile_face=>$smile_image)
{
$_POST["message"] = str_replace($smile_face, $smile_image, $_POST["message"]);
}
if(!esiste($_POST,"dati",$conn))
{
$today=date("j-m-Y H:i");
$query = mysql_insert($_POST,$today,"dati",$conn);
}
closeConnect($conn);
}
?>
<html>
<head>
// Codice JS per l'indicazione dei caratteri rimanenti
<LINK HREF="stile.css" type="text/css" rel="stylesheet" >
<SCRIPT language=JavaScript>
<!--
function initcharsleft()
{
charsleft(document.forms["tagform"].text);
}
function charsleft(feld)
{
var anz = feld.value.length;
if (anz> 150 )
{
feld.value = feld.value.substring(0,150);
frei = 0;
}
else
{
frei = 150 -anz;
}
document.forms["tagform"].num.value = frei;
}
//-->
</SCRIPT>
</head>
<body>
// sezione HTML e PHP per la rappresentazione della Tag e messaggi
<TABLE border=0 cellpadding=0 cellspacing=0 width="20%" class=tableborder height="70%">
<TR>
<TD class=row4>
<TABLE border=0 cellpadding=0 cellspacing=4 width="100%">
<TR>
<TD>
<TABLE border=0 cellpadding=0 cellspacing=0 width="100%" class=tableborder>
<TR>
<TD valign=top class=row2><div class=div_tag>
<?php
include_once(TAG_DIR."/fmysql.php");
$conn = connessione("tag");
$query = mysql_list_all("dati",$conn);
if($query!=null)
{
lista($query);
}
closeConnect($conn);
?>
</div></TD>
</TR>
</TABLE>
</TD>
</TR>
<TR height=10>
<TD> </TD>
</TR>
<TR>
<TD valign=top><FORM METHOD=POST name=tagform ACTION="tag.php">
<SCRIPT language="Javascript" src="tagboard.js" type="text/javascript"></SCRIPT>
<CENTER>
<TABLE border=0 cellpadding=0 cellspacing=1 width="100%">
<TR height=25>
<TD class=row3 colspan=2 align=middle><INPUT TYPE="text" NAME="nome" MAXLENGTH="20" size=20 value="NOME"></TD>
</TR>
<TR height=25>
<TD class=row3 colspan=2 align=middle><INPUT TYPE="text" NAME="www" MAXLENGTH="150" size=20 value="http://" ></TD>
</TR>
<TR height=25>
<TD class=row3 colspan=2 align=middle><INPUT TYPE="text" NAME="email" MAXLENGTH="30" size=20 value="EMAIL"></TD>
</TR>
<TR>
<TD class=row3 valign=middle align=middle><input size=3 value=150 name="num"><BR><A onclick="return pop_up_smilies();" href="smilies.htm" target=_blank class=genmed> <I>smilies</I></A></TD>
<TD class=row3 valign=middle align=middle><TEXTAREA name="message" ROWS="3" COLS="10" maxlength="150" onKeyPress=charsleft(this); onKeyDown=charsleft(this); onBlur=charsleft(this); onKeyUp=charsleft(this); onFocus=charsleft(this); wrap=VIRTUAL onChange=charsleft(this);></TEXTAREA></TD>
</TR>
<TR height=30>
<TD colspan=2 class=row3 valign=middle><CENTER><INPUT TYPE="submit" value=" Invia " name="ok" class=mainoption></CENTER></TD>
</TR>
</TABLE></CENTER></FORM></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE><BR>
</body>
</html>
|
Torna su | Indice Guida | Pagina << 26 >>
|