22
« en: 03 de Febrero de 2010, 23:55:25 pm »
Hola A Todos Aqui les traigo un sistema de noticia hecho por Saphari y Algunos de aqui del foro phperos
SQL
CREATE TABLE IF NOT EXISTS `noticia` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`titulo` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`noticia` text COLLATE utf8_unicode_ci NOT NULL,
`fecha` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`autor` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Connect.php
<?php
$db_host="localhost"; // Host al que conectar, habitualmente es el ‘localhost’
$db_nombre="Nombre de la Base de Datos"; // Nombre de la Base de Datos que se desea utilizar
$db_user="root"; // Nombre del usuario con permisos para acceder
$db_pass="root"; // Contraseña de dicho usuario
// Ahora estamos realizando una conexión y la llamamos ‘$link’
$link=mysql_connect($db_host, $db_user, $db_pass) or die ("Error conectando a la base de datos.");
// Seleccionamos la base de datos que nos interesa
mysql_select_db($db_nombre ,$link) or die("Error seleccionando la base de datos.");
// Devolvemos $link porque nos hará falta más adelante, cuando queramos hacer consultas.
return $link;
?>
Formulario de la Noticia
<style type="text/css">
<!--
body,td,th {
color: #FFFFFF;
}
body {
background-color: #000000;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
<style type="text/css">
<!--
body,td,th {
color: #FFFFFF;
}
body {
background-color: #000000;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
<blockquote>
<form action="enviar.php" method="post" name="form" id="form">
<table width="314" border="0">
<tr>
<td width="85"><strong>Titulo:</strong></td>
<td width="219"><label>
<input name="titulo" type="text">
</label></td>
</tr>
<tr>
<td><strong>Autor:</strong></td>
<td><label>
<input name="autor" type="text">
</label></td>
</tr>
<tr>
<td><strong>Fecha:</strong></td>
<td><label>
<select name="days" id="days">
<?php
for($i = 1; $i <= @date("t"); $i++) {
$i==@date("d") ? $amount = "selected" : $amount = "";
$i<=9 ? $zero = "0" : $zero = "";
echo "<option value='$zero$i' $amount>$zero$i</option>";
}
?>
</select>
</label>
/
<label><?php
$months = array("", "01", "02", "03", "04","05", "06", "07", "08","09", "10", "11", "12");
$date = @date("m"); // Esta es la Linea del Error
$date <= 9 ? $date = $date[1] : $date = $date;
?>
<select name="months" id="months">
<?php
for($i = 1; $i <= count($months)-1; $i++) {
$i == $date ? $amount = "selected" : $amount = "";
echo "<option value='$months[$i]' $amount>$months[$i]</option>";
}
?>
</select>
</label>
/
<label>
<select name="years" id="years">
<?php
for($i = 1950; $i <= @date("Y"); $i++) {
$i==date("Y") ? $amount = "selected" : $amount = "";
echo "<option value='$i' $amount>$i</option>";
}
?>
</select>
</label></td>
</tr>
<tr>
<td><strong>
<label>Mensaje:</label>
</strong></td>
<td><label></label>
<label></label><label>
<textarea name="noticia" cols="25" rows="10"></textarea>
</label></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<p>
<label>
<input name="ok" type="submit" id="ok" value="Enviar Noticia">
</label>
<label>
<input type="reset" name="Submit2" value="Borrar">
</label>
</p>
</form>
</blockquote>
El Archivo enviar.php
<?php
include("connect.php"); # Incluimos el conectar.php
if($_POST['ok']) { # Miramos si han dado al boton enviar noticia
$fecha = @date("d-m-Y");
$sql = "INSERT INTO `noticia` (titulo, noticia, fecha, autor) VALUES ('".$_POST['titulo']."','".$_POST['noticia']."','".$fecha."','".$_POST['autor']."')";
if(mysql_query($sql) or die(mysql_error())){ # Expones sql
echo "Se ha enviado la noticia correctamente"; # si se ha enviado la noticia correctamente
} else {
echo "Ha habido un error en enviar la noticia"; # Si ha habido un error
}
}
echo '<br><a href="ver.php">Ver Noticias</a>'; # IR a la url de ver las noticias
?>
Por Ultimo Ver la Noticia
ver.php
<style type="text/css">
<!--
body,td,th {
color: #FFFFFF;
}
body {
background-color: #000000;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style><?php
include("connect.php"); // Incluimos el conectar.php para conectarnos a nuestra base de datos
$the = mysql_query("SELECT * from `noticia` ORDER BY `id` DESC"); // Buscamos las noticias almacenadas de la primera a la ultima
if (mysql_num_rows($the)) {
while ($ver = mysql_fetch_array($the)) { // Extramos datos de la tabla sql a la raiz
echo "<br><b>Titulo</b>:" .$ver['titulo']; // Mostramos el titulo de la noticia
echo "<br><b>Mensaje</b>:" .$ver['noticia']; // Mostramos el Mensaje de la noticia
echo "<br><b>Autor</b>:" .$ver['autor']; // Mostramos el Autor de la noticia
echo "<br><b>Fecha</b>: ".$ver['fecha']; // Mostramos la Fecha de la Noticia
}
}
echo '<br><a href="enviar.php">Enviar noticias</a>'; // Ir al enviador de noticias
?>
Espero que les guste xD