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

Ya está solucionado el problema de los comentarios, os explico los pasos:

Ir a vuestro PhpMyAdmin, a SQL y poner esto:

Código PHP:
ALTER TABLE `comentariosCHANGE `id` `id_comentarioINT11 NOT NULL AUTO_INCREMENT 
Cuendo lo haiais puesto, volver a SQL y poner esto:
Código PHP:
ALTER TABLE `comentariosADD `noticia_idINT11 NOT NULL 
Vale, ya tedréis editada la fila id y añadida la fila noticia_id, bueno, pues ahora vienen los códigos, editaremos los códigos publicacomentario.php y ver.php

publicacomentario.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 - Publica Comentarios :::.</title>
</head>

<body>
<?php
require ('includes/connect.php');
$id=$_POST['id'];
$nick=$_POST['nick']; 
$email=$_POST['email']; 
$comentario=$_POST['comentario']; 
$query "INSERT INTO comentarios (nick,email,comentario,noticia_id) VALUES('$nick','$email','$comentario','$id')";
mysql_query($query) or die(mysql_error());
header("location: ver.php?id=$_POST[id]")
?> 
</body>
</html>
Y 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('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
//-->
</script>
</head>

<body>
<?php
require ('includes/connect.php');
$id=$_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 
'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>
</body>
</html>
Bueno, pues ahora, ya podéis comentar y se añadiran todos los comentarios en la noticia que se haian comentado, ahh!! y también están validados los campos, están validados con la validación de dreamweaver, si la quereis modificar, pues modificar lo que querais.

Un Saludo!!