I have been learning programming online for like 3 years. I have developed big project in this time, but I have a problem. I didn't know about MVC pattern and was how to say "Programming from scratch". Right now my code is a big mess, that no one can understand, but only me..
I found out about this MVC pattern and it's a excellent thing but right now I can't understand where and how to make couple of things. How I understand that no php code goes to view? And no html/css into model.
For example in which structure I have to implement my javascript and ajax code? (Is it view?) Where and how to manage displaying if's? Like:
if($user_id == $me){
    //display post with delete option
}else{
    //display post
}
I have a functions with houndreds of lines and if's. For example one of my functions. I want to understand how to reproduce it in MVC pattern.
public function selectUserPosts(){
    try{
        require_once('Class.Users.php');
        $user = new USER();
        $id = $_GET['id'];
        $stmt = $this->conn->prepare("SELECT * FROM fun_posts WHERE addedby=('$id') ORDER BY date DESC");
        $stmt->execute();
        $result = $stmt->fetchAll();
        foreach($result as $post){
            ?>
            <div class="col-sm-4">
                <div class="animated flipInY" id="panel_<?php echo $post['id'];?>">
                    <div class="thumbnail" style="height:300px;">
                        <a href="/Web/Pages/Fun/Fun_Post.php?action=select&image_id=<?php echo $post['id'];?>" target="_blank">
                            <img class="img" style="width: 100%; height:150px;" src="<?php echo $post['image']; ?>" alt="" />
                        </a>
                        <i class="fa fa-clock-o" aria-hidden="true"></i><?php echo $user->time_elapsed($post['date']); ?>
                        <div id="upvote_<?php echo $post['id'];?>" class="panel">
                            <i class="fa fa-arrow-circle-up" style="font-size:22px; margin-top:10px;"></i> <b id="upvote_panel_<?php echo $post['id'];?>"><?php echo $post['upvotes']; ?></b>
                            <button style="float:right; margin-top:5px; width:90px;" class="btn btn-sm btn-success" type="submit"><i class="fa fa-arrow-circle-up"></i> Upvote</button>
                        </div>
                        <div id="downvote_<?php echo $post['id'];?>" class="panel">
                            <i class="fa fa-arrow-circle-down" style="font-size:22px; margin-top:-5px;"></i> <b id="downvote_panel_<?php echo $post['id'];?>"><?php echo $post['downvotes']; ?></b>
                            <button style="float:right; margin-top:-10px; width:90px;" class="btn btn-sm btn-danger" type="submit"><i class="fa fa-arrow-circle-down"></i> Downvote</button>
                        </div>
                        <div id="comment_<?php echo $post['id'];?>" class="panel">
                            <i class="fa fa-comment" style="font-size:22px; margin-top:-10px;"></i> <b id="comment_panel_<?php echo $post['id'];?>"><?php echo $post['comments']; ?></b>
                            <a href="/Web/Pages/Fun/Fun_Post.php?action=select&image_id=<?php echo $post['id'];?>" target="_blank">
                                <button style="float:right; margin-top:-13px; width:90px;" class="btn btn-sm btn-primary" type="submit"><i class="fa fa-comment"></i> Comment</button>
                            </a>
                        </div>
                        </div>
                </div>
            </div>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
            <script>
                $(function(){
                    $("#upvote_<?php echo $post['id'];?>").click(function(){
                        $.ajax(
                            { url: "Home.php?upvote-btn=true?action=select&image_id=<?php echo $post['id'];?>",
                                type: "get",
                                success: function(result){
                                    $('#upvote_panel_<?php echo $post['id'];?>').load(document.URL +  ' #upvote_panel_<?php echo $post['id'];?>');
                                    $('#downvote_panel_<?php echo $post['id'];?>').load(document.URL +  ' #downvote_panel_<?php echo $post['id'];?>');
                                    $('#comment_panel_<?php echo $post['id'];?>').load(document.URL +  ' #comment_panel_<?php echo $post['id'];?>');
                                    document.getElementById('result-box').innerHTML += result;
                                }
                            });
                    });
                    $("#downvote_<?php echo $post['id'];?>").click(function(){
                        $.ajax(
                            { url: "Home.php?downvote-btn=true?action=select&image_id=<?php echo $post['id'];?>",
                                type: "get",
                                success: function(result){
                                    $('#upvote_panel_<?php echo $post['id'];?>').load(document.URL +  ' #upvote_panel_<?php echo $post['id'];?>');
                                    $('#downvote_panel_<?php echo $post['id'];?>').load(document.URL +  ' #downvote_panel_<?php echo $post['id'];?>');
                                    $('#comment_panel_<?php echo $post['id'];?>').load(document.URL +  ' #comment_panel_<?php echo $post['id'];?>');
                                    document.getElementById('result-box').innerHTML += result;
                                }
                            });
                        });
                });
            </script>
            <?php
        }
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
}
 
    