Ver Mensaje Individual
  #41 (permalink)  
Antiguo 12/09/2011, 07:07
lokor712
 
Fecha de Ingreso: septiembre-2011
Mensajes: 38
Antigüedad: 12 años, 8 meses
Puntos: 5
Respuesta: [APORTE] Sistema de noticias y comentarios

Buenas a todos!! Ya tengo el código para mostrar imágenes en el index y el ver y que se hagan pequeñas, bueno, hay 3 pasos, os los explico:

1ero:
Crear una carpeta en el principio de todo, osea donde están todas las carpetas, admin, uploads, etc, bueno, la llamaremos miniimages.

2ndo:
Crear un arhivo tambien en el inicio de todo que se llame hacethumb.php, este hará las imagenes pequeñas y las trasladará a miniimages
hacethumb.php
Código PHP:
<?php
$ancho_nuevo 
80;
$carpeta "uploads/";
$ext = array("gif","jpg","png");
$carpetanueva "miniimages/";
if(
is_dir($carpeta) && $dir opendir($carpeta)){
while ((
$nombre_archivo readdir($dir)) !== false){
$archivo pathinfo($carpeta.$nombre_archivo);
if (
in_array(strtolower($archivo['extension']),$ext))
{
if(
strtolower($archivo['extension'])=="gif"){
$img imagecreatefromgif($carpeta.$nombre_archivo);
}else if(
strtolower($archivo['extension'])=="jpg"){
$img imagecreatefromjpeg($carpeta.$nombre_archivo);
}else if(
strtolower($archivo['extension'])=="png"){
$img imagecreatefrompng($carpeta.$nombre_archivo);
}
$ancho imagesx($img);
$altura imagesy($img);
$ancho_nuevo $ancho_nuevo;
$altura_nueva floor($altura*($ancho_nuevo/$ancho));
$tmp_img imagecreatetruecolor($ancho_nuevo,$altura_nueva);
imagecopyresized($tmp_img,$img,0,0,0,0,$ancho_nuevo,$altura_nueva,$ancho,$altura);
if(
strtolower($archivo['extension'])=="gif"){
imagegif$tmp_img,$carpetanueva.$nombre_archivo);
}else if(
strtolower($archivo['extension'])=="jpg"){
imagejpeg$tmp_img,$carpetanueva.$nombre_archivo);
}else if(
strtolower($archivo['extension'])=="png"){
imagepng$tmp_img,$carpetanueva.$nombre_archivo);
}
}
}
}
closedir($dir);
?>
3er paso:
Modificaremos los archivos index.php y ver.php para que muestren las imagenes
index.php
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
<title>Sistema de Noticias de KarlicOs :::.</title>
</head>

<body>
<?php
include ('includes/connect.php');
include (
'hacethumb.php');
//hacemos las consultas 
$result=mysql_query("select * from noticias order by fecha Desc"$connect); 
$totalregistros=mysql_num_rows($result); 
$result2=mysql_query("select * from comentarios where id_comentario",$connect);
$totalcomentarios=mysql_num_rows($result2);
?> 
</head> 
<body>
<div align="center">
<table width="70%" height="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="80%" align="center" valign="top">
    <?php
    
//Recogemos las consultas en un array y las mostramos 
    
while($row=mysql_fetch_array($result))
            {
    
$result2=mysql_query("select * from comentarios where id_comentario='".$row[id_noticia]."'",$connect);
    
$totalcomentarios=mysql_num_rows($result2);
    
    
$carpeta= ('miniimages/');
    echo 
'<h3>'.$row[titulo].'</h3>';
    
//echo '<br><br>';
    //echo ' '.$row[noticia].' <br>';
    
echo substr($row['noticia'], 0550);    echo '&nbsp;<strong>...</strong><br><br>';        echo '<div align="right"><a href="uploads/'.$row[foto].'"><img src="miniimages/'.$row[foto].'"/></a></div>';
    echo 
'<strong>Autor:&nbsp;'.$row[autor].'&nbsp; |&nbsp;Publicado en:&nbsp; '.$row[categoria].'&nbsp; |&nbsp;El:&nbsp; '.$row[fecha].'&nbsp;|&nbsp;Comentarios:&nbsp;('.$totalcomentarios.                                                                                                                            ')&nbsp;|&nbsp;<a href="ver.php?id='.$row[id_noticia].'">Leer mas ...</a></strong>';
    }
    
mysql_free_result($result)
    
?></td>
  </tr>
</table>
</div>
</body>
</html>
ver.php
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
<title>Sistema de Noticias de KarlicOs - Ver Noticia :::.</title>
<script type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
 
function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+': Es necesario una dirección de e-mail.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+': Es necesario que contenga un numero.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+': Es necesario que contenga un numero entre '+min+' y '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+': Es necesario que escriba un nick/comentario.\n'; }
  } if (errors) alert('Aparecierón lo siguientes errores:\n'+errors);
  document.MM_returnValue = (errors == '');
}
//-->
</script>
</head>
 
<body>
<div align="center">
<table width="70%" height="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="80%" align="center" valign="top">
<?php
require ('includes/connect.php');
$id=mysql_real_escape_string($_GET["id"]);
$result mysql_query("select * from noticias where id_noticia='$id'",$connect);
while(
$row mysql_fetch_array($result))
{
$result2=mysql_query("select * from comentarios where noticia_id='$id'",$connect);
$totalcomentarios=mysql_num_rows($result2);
echo 
'<h3>'.$row[titulo].'</h3>';
echo 
''.$row[noticia].'<br><br><br>';        echo '<div align="right"><a href="uploads/'.$row[foto].'"><img src="miniimages/'.$row[foto].'"/></a></div>';
echo 
'Autor:&nbsp;'.$row[autor].' | '.$row[categoria].' |&nbsp;Publicado en:&nbsp; '.$row[fecha].'';
//echo '<a href="editarcomentario.php?id='.$row[id].'">Editar</a> | <a href="borrar.php?id='.$row[id].'">Borrar</a>';
}
mysql_free_result($result)
?> 
<br><br><br><br>
<?php
echo '<h2>Comentarios:&nbsp;('.$totalcomentarios.')</h2><br>';
//hago el llamado a la base
$result=mysql_query("select * from comentarios where noticia_id='$id'",$connect);
//Bucle while para visualizarlos
while($row=mysql_fetch_array($result))
{
echo 
'<h4>Nick: '.$row[nick].':</h4>Email: '.$row[email].'<br>Comentario: '.$row[comentario].'<br><br>';
}
mysql_free_result($result)
?> 
 
<form action="publicacomentario.php" method="post" onsubmit="MM_validateForm('nick','','R','email','','RisEmail','comentario','','R');return document.MM_returnValue">
<input type="hidden" name="id" value="<?php echo $id;  ?>"><br>
Nick:<br>
<input type="text" name="nick"><br>
E-Mail:<br>
<input type="text" name="email"><br>
Escriba el comentario<br>
<textarea name="comentario" cols="70" rows="15"></textarea>
<br>
<input type="submit" value="Enviar comentario"><br>
</form>
</td>
  </tr>
</table>
</div>
</body>
</html>
Vale os lo explico asi por encima, el hacethumb.php lo que hace es leer las imagenes de la carpeta uploads, modificarle el ancho a 80px (se puede editar) y moverlas a la carpeta miniimage, entonces tenemos las grandes en uploads y las pequeñas en miniimages. El archivo está incluido en el index.pp y el ver.php, osea que no he modificado nada del publicando.php, osea que cuando alguien entre en vuestra página, automáticamente se ejecutará el archivo hacethumb.php.

Bueno, probarlo un poco y me decís que tal todo...

Un Saludo!!

Ahh!! Y he añadido una tabla en el centro de index.php y ver.php para que las noticias estén mas centradas, bueno, un saludo!!