0

Terribly sorry because it will be a long post. I am a beginner in MYSQL and I just at the beginning of learning. I'm trying to make the user comment-reply system . I'm completely stuck.

I have a database with a table name 'post' for comment with the following structure

 TABLE `post` (
  `id_posta` int(4) NOT NULL,
  `tekst_posta` text NOT NULL,
  `name` text NOT NULL,
  `slika_posta` blob NOT NULL,
  `type` text NOT NULL,
  `vreme_posta` datetime NOT NULL,
  `id_autora` int(4) NOT NULL,
  `ime` varchar(50) NOT NULL,
  `prezime` varchar(100) NOT NULL
   ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

and table name 'komentar', where written reply to comments

TABLE `komentar` (
  `id_komentara` int(11) NOT NULL,
  `tekst_komentara` text NOT NULL,
  `id_aut_komentara` int(4) NOT NULL,
  `id_autora` int(4) NOT NULL,
  `vreme_komentara` datetime NOT NULL,
  `id_posta` int(4) NOT NULL,
  `imek` varchar(50) NOT NULL,
  `prezimek` varchar(100) NOT NULL
   ) ENGINE=InnoDB DEFAULT CHARSET=latin

When I write all the comments, everything works perfect.

$upit = "SELECT id_posta, slika_posta, vreme_posta, tekst_posta, id_autora, ime, prezime FROM post ORDER BY `post`.`id_posta` DESC;";
  $postovi = $con->query($upit);
 
 if($postovi->num_rows > 0)
 {
  
  while($red = $postovi->fetch_assoc())
  {
   $idp = $red["id_posta"];
   echo "<table border-collapse: separate; empty-cells: hide; class='my-special-table'>";
   echo "<tr><td>";
   echo $red["ime"]. " " . $red["prezime"]. ':' ;
   echo "</td></tr>";
   echo "<tr><td class='prvatd'>";
   echo $red["tekst_posta"];
   echo "</td></tr>";
   if( $red["slika_posta"] != '' ){
   echo '<tr><td class="w_imgg"><center><img src="data:image/png;base64,'.base64_encode($red["slika_posta"]).'" class="w_img"></center></td></tr>';
   }
   echo "<br>";
   echo "<tr><td style= 'color:#FF0000; font-size:12px;'>";
   echo 'Objavljeno' . ' ' . $red["vreme_posta"];
   echo "</td></tr>";
   echo "<tr><td>";
   echo "<form method='post' action = '' >
           <textarea id = 'styled' name='tekst_komentara' placeholder='Prokomentarisite'></textarea><br>
     <input type='hidden' name='id_pos' value= $idp/>
     <input type='submit' name='unesi_komentar' value='Objavi komentar'/>
         </form>";
   echo "</td></tr>";
   echo "<tr><td>";
   echo REPLY ?????
   echo "<td></td>";
   echo "<tr><td>Lajk<td></td>";
   echo "</table>";
   echo "<br><br>";
   
  }
 }
  
   $con->close();

The problem arises when i try to echo reply in the reply td to appropriate comment.

i try this....

$con = new mysqli ("localhost", "root", "", "introduce");

$upit = "SELECT id_posta, slika_posta, vreme_posta, tekst_posta, id_autora, ime, prezime FROM post ORDER BY `post`.`id_posta` DESC;";
$postovi = mysqli_query($con,$upit);
if($postovi->num_rows > 0)
{
  while($red = $postovi->fetch_assoc()) {
    
               
   echo "<table>";
            echo "<tr><td>";
   echo $red["tekst_posta"];
   echo "</td></tr>";
   echo "</table>"; 
     

    $upit1 = "SELECT * FROM komentar INNER JOIN post ON post.id_posta=komentar.id_posta ;"; 
    $komentari = mysqli_query($con,$upit1);
 while($red = $komentari->fetch_assoc()) {
     
   echo "<table>";
            echo "<tr><td>";
   echo $red["tekst_komentara"];
   echo "</td></tr>";
   echo "</table>";
    }
  }
  
}

and result is

2                     2  (second post)
1-1                  2-1 (first reply)
1-2                  2-2 (second reply)
2-1
1    and should be    1   (first post)
1-1                  1-1  etc,etc....
1-2                  1-2   
2-1    

then i try this

 $con = new mysqli("localhost", "root", "", "introduce");
 
   $sql = "SELECT * FROM komentar INNER JOIN post ON post.id_posta=komentar.id_posta ; ";
  
   $komentari = $con->query($sql);
   if($komentari->num_rows > 0)
   {
    while($red = $komentari->fetch_assoc())
    {
    
     echo $red["tekst_posta"];
     echo "<br>";
     echo $red["tekst_komentara"];
    
    }
   }

result is

1
  1-1
   1
  1-2
   2
  2-1 duplicate post on every replay....
   2
  2-2
   2
  2-3

next i try...

 $sql = "SELECT id_posta, slika_posta, vreme_posta, tekst_posta, id_autora, ime, prezime FROM post ORDER BY `post`.`id_posta` DESC;";
   $sql .= "SELECT * FROM post JOIN SELECT * FROM komentar WHERE komentar.id_posta = 'post.id_posta'";

i try GROUP BY and try, and try.....

I was thinking about array.... so, I ask for your advice because i this will never be able to do on this way.

0 Answers0