I have a small system of making posts on my site that I am creating. I print each post with "echo" from PHP.
Everything works great at first(print it). But the problem arises when I try to comment on that post.
What is the problem?
When I write the first comment it stays with its post, but when I write the second comment in that same post it happens that instead of leaving the second comment below the first, it clones me or prints another post with that comment (the second.) I don't know if they understand.
People table
------- ------- ---------- --------- -------- --------
ID nombre apellido correo password Fotos
------ ------ --------- -------- -------- ------
Post table
------- ------- ---------- ---------
Id_post User_Id Titulo Contenido
------ ------ --------- --------
Comments table
------- --------- ---------- --------- --------
id_com contenido fecha id_usuario id_post
------ -------- --------- -------- --------
posts_votes table
------- --------- ---------- --------- --------
Id Id_post id_usuario puntos fecha
------ -------- --------- -------- --------
I will put the code:
As you can see I do a query of 3 tables using Join.
$query_buscar_posts = mysqli_query($conn, " SELECT
posts.Id_post,
posts.Titulo,
posts.Contenido,
personas.Id,
personas.Fotos,
personas.nombre,
personas.apellido,
posts_votos.id_usuario,
posts_votos.puntos,
posts_votos.id_post,
comentarios.contenido
FROM posts
LEFT JOIN comentarios
ON comentarios.id_post = posts.Id_post AND posts.User_Id =
comentarios.id_usuario
LEFT JOIN posts_votos
ON posts_votos.id_post = posts.Id_post AND posts.User_Id =
posts_votos.id_usuario
LEFT JOIN personas
ON posts.User_Id = personas.Id
WHERE posts.User_Id = $my_id ORDER BY id DESC") or die('Error: ' .
mysqli_error($conn));
while ($posts_filas = mysqli_fetch_array($query_buscar_posts)) {
$id_user = $posts_filas['Id'];
$code = $posts_filas['Id_post'];
$title = $posts_filas['Titulo'];
$contenido = $posts_filas['Contenido'];
# code...
$contenido2 = $posts_filas['contenido'];
$points = $posts_filas['puntos'];
From here each post is printed:
echo '<div style ="margin-bottom:20px;" class="POSTED">
<div class="post_p1">
<div class="Header_post">
<div class="orden_post">
<div class="orden_post_content">
<a href="#"><p class="aaaaa">'. $posts_filas['nombre'].'
'.$posts_filas['apellido'].'</p></a>
</div>
<div class="orden_post_content_pic">
<a href="perfil.php?=id"><img width="50px" height="50px" src="'.
$posts_filas['Fotos'].'"> </img></a>
</div>
</div>
<div class="boton_eliminar_post">
<a href="javascript:void(0)"><i class="fa fa-minus" aria-
hidden="true"></i></a>
</div>
</div>
<div class="post_titulo">' . htmlentities($title) . '</div>'
.'<div class="post_contenido"><div class="post_contenido_content">' .
htmlentities($contenido) .'</div>' . '</div>'
.'<div class="points_post">
<div class="content_points">
<div class="orden_post">
<div class="orden_post_content"><a href="javascript:void(0)"
><span><i style="font-size:24px;" class="fa fa-sun-o" aria-
hidden="true"></i></span></a></div>
<div id="share_link" class="orden_post_content"><a
href="javascript:void(0)" ><i class="fa fa-share-square" aria-
hidden="true"></i>Compartir</a></div>
<div class="orden_post_content"><a
href="javascript:void(0)"><span>'.$posts_filas['puntos']. '
</span>globos</a></div>
</div>
</div></div>'
.'<form id="form_comentarios" method="POST">
<ul style="padding:0;">
<li class="alinear_elementos_post">
<input type="hidden" name="eid"
value="'.$posts_filas['Id_post'].'">
<img width="35px" height="35px" src="'.
$posts_filas['Fotos'].'"> </img>
</li>
<li id="textbox-align" class="alinear_elementos_post">
<input type="text" class="campo_comentario"
name="comment_content" rows="2" cols="44" style="text-align:center;"
placeholder="Escribe un comentario." required></input>
</li>
<li class="alinear_elementos_post">
<input class="boton_campo_comentario" type="submit"
name="comentar"></input>
</li>
</ul>
</form>'.'
<p class="aaaaa"> '.$posts_filas['contenido'].'</p>'
.'</div>'
.'<div class="post_p2">
<a onclick="('.$posts_filas['Id_post'].')" data-
id='.$posts_filas['Id_post'].' href="javascript:void(0)"
class="globes_post_giving"><div class="alinear_boton_balloons"><img
class="img_style" src="img/globo-s.png"></img></div></a>
<a href="#" class="globes_post_res">#</a>
</div>'
.'</div>';
}
$contar = mysqli_num_rows($query_buscar_posts);
if($contar == 0) {
echo "<div class='No_post'>
<div class='message_empty_post'>Aun no has puesto nada aqui!</div><br>
<div class='contenido_no_post'><i class='fa fa-frown-o' aria-
hidden='true'></i></div>
<div class='footer_empty_post'>
<a href=''>Editar mi perfil</a>
<a href=''>Ir a mi perfil</a>
</div>
</div>";
}
?>
Every post I print looks something like this:
But the problem is in the comments. The first one comes out according to the post that I put it on and the second one prints another same post for me, which shouldn't be the case.
To split the query into two parts, I would do it like this:
First I get all the posts and their related votes:
Then I loop through one by one with a while. For each iteration of the while, I make another sql query to get the comments and the users related to the post that the loop is going through:
So far you already get the information, now you just have to print it. I see that you have a lot of html and there you insert php. At this point, where I put the "Here you print" comment, you would put your html.
The truth is that it is a spaghetti and the code is not aesthetic, maybe you consider making a second version of it where you try to separate the html and php as much as possible.
Something you could implement is to separate the actions by functions. That is, create a function to get all the posts, another one to get the comments:
You include the functions here if you want to separate them into a separate file.
And as I say, you only add the html you want to print. If you have doubts, comment. Greetings.
Note: So that each comment is shown to the corresponding post, check that when inserting it you are doing it with the id of the post in question, otherwise it will print it where it is not, if it prints it.
I edited the first query ($query_posts) to get the votes related to the post.
Good, let's see, as you ask I have set it up in two queries, one for the posts and their info, and another for the comments. I enclose the complete code, it is possible that there is some syntax error because I am not from php and it cannot be tested without the tables either, so try and tell me to see what comes out.
If you don't want to separate your SQL query into two different SQL queries you should implement some method to detect "new messages":
In your case it is easy to implement it because there is no "parent" that contains both the message and the comments, otherwise you would have to create additional code to deal with this inconvenience.
Even so, there is an alternative method that would be to make a first pass to the data returned by your query to save it in an associative array, where the first level is a message and an element of it contains the comments:
This way you will have an element in
$datos
for each message and an element in the indexcontenido
for each comment. With twoforeach
(one for the messages and one for the comments of each message) you could generate the HTML of your forum.