-2

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();
    }
}
Car Paradise
  • 33
  • 1
  • 5

1 Answers1

-1

MVC - Model View Controller

1) In the Model you have to place all logic and work with databases and other services

2) In the View you have to place code, that will see user (you can't call logic methods or functions those use services like database in there)

3) And Controller have to connect Model and View together. And, of course, it have to be as route manager

KusokBanana
  • 345
  • 1
  • 12
  • Thanks for answer. But I still don't get it. Like I would think that ajax/javascript code need to be in view, but it's function, so it goes to model? I don't have a clue how to manage that work. And with if's too. If is logic, so it needs to be in model?! and I have to return different values regarding to if? – Car Paradise Sep 23 '18 at 00:44
  • You select posts from your database - it is in Model. Then you have to pass variable with your posts to View. You can use PHP code in the view, but it must be just values, not calling the services – KusokBanana Sep 23 '18 at 00:52
  • @CarParadise So it will be like: **Model** `public function getPosts() { .. }` **Controller** `$posts = $user->getPosts();` **View** `foreach ($posts as $post) { ... }` – KusokBanana Sep 23 '18 at 00:56
  • That's actually wrong. "Model" is not a "database abstraction". And controllers are not "traffic cops". Their responsibility is to just alter the model layer's state (nothing to do with views - at least in web context). – tereško Sep 23 '18 at 00:58
  • Okay. That I have understood. But I still got question about javascript? It goes into View right? Because, like I have button and onClick javascript starts work. If javascript wil be in different place, it won't work right? – Car Paradise Sep 23 '18 at 01:00
  • I didn't say this. I just told that the logic is in there and you can manipulate by database's data in there @tereško – KusokBanana Sep 23 '18 at 01:02
  • @CarParadise you should not be using `onClick` at all (not related to even MVC). Instead you should be using [`addEventListener()`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener). And javascript includes would be done in the templates (think: Twig). – tereško Sep 23 '18 at 01:03
  • @tereško Okay. So maybe you know a good MVC tutorial or some information? I have been googling a lot, and every where explained understandable and made different. It's hard to understand if you have been learning with google for many years and understood that everything in your code is wrong. No matter how you do something always someone will say it's wrong. So I started to learn at codeacademy but they don't offer mvc course. – Car Paradise Sep 23 '18 at 01:10
  • @CarParadise I have [this collections](https://stackoverflow.com/a/16356866/727208) of materials that might help. But most of those will not be "teaching MVC". Instead they will be building up your theoretical basis for understanding "Separation of Concerns" architectural principle, which is what MVC is actually a form of. – tereško Sep 23 '18 at 01:45