G
G
green1762015-12-06 21:05:56
PHP
green176, 2015-12-06 21:05:56

How to properly organize output in php template?

I can't figure out how to organize output in a template. There is a table with messages, and a table with likes.
+-------------+ +------------+
|message.......| |likes............|
+-------------+ +------------+
|id.............. .| | lid............|
|name............| | mid...........|
|message......| | name.........|
|date......................| +------------+
---------------
In the model, search for all messages

function messages_all($connect){
    $query = "SELECT * FROM `message` ORDER BY `message`.`id` DESC";
    $result = mysqli_query($connect, $query);
    
    if (!$result)
        die(mysqli_error($connect));
    
    $count = mysqli_num_rows($result);
    $messages = array();
    
    for ($i=0; $i<$count; $i++){
        $row = mysqli_fetch_assoc($result);
        $messages[] = $row;
    }
    return $messages;
}

In the output template
<div class="main">
               
                <?php foreach($messages as $p): ?>
                    <div class="row message">
                       <div class="col-md-6 ">
                            <p><?=$p['name']?> say: </p>
                            <p><?=$p['message']?> At <?= date ('H:i ', strtotime($p['date']))?></p>
                       </div>
                        <div class="col-md-6">
                            <a class="btn btn-info pull-right" href="index.php?action=like&id=<?=$p['id']?>">like </a>
                            <a class="btn btn-info  <?php ($name != $p['name'] ? print "disabled" : "") ?> pull-right" href="index.php?action=delete&id=<?=$p['id']?>">delete</a>
                        </div>
                        
                    </div>
                <?php endforeach ?>
            </div>

Well, in the controller I just call the function
$message = messages_all($connect);
        include("view/main.php");

further likes
when the like button is pressed, I execute the function like_add($connect, $id, $name); to which I pass the id of the liked message and the name of the person who liked the like_add
function
function like_add ($connect, $id, $name){
    $name = trim($name);
    $id = (int)$id;
    if ($id == 0)
        return false;
    $sql = "INSERT INTO `likes` (mid, name) VALUES ('%d','%s')";
    $query = sprintf ($sql, $id, mysqli_real_escape_string($connect, $name)); 
    $result = mysqli_query($connect, $query);
    
    if (!$result)
        die(mysqli_error($connect));
    return true;
}

Those. each time the button is clicked, a record is created with the message id and the name of the person who clicked the button.
Next, to find out the number of likes for a message, I simply count the number of entries in the database with the message id
function like_count ($connect, $mid){
    $query = sprintf ("SELECT * FROM `likes` WHERE mid=%d", (int)$mid);
    $result = mysqli_query($connect, $query);
    
    if(!$result)
        die(mysqli_error($connect));
    
    $likes = mysqli_num_rows($result);
    
    return $likes;    
}

Now the actual question is: how to display the number of likes for a post? At this stage, I just execute the like_count function in the template, it looks something like this
<div class="main">
               
                <?php foreach($messages as $p): ?>
                    <div class="row message">
                       <div class="col-md-6 ">
                            <p><?=$p['name']?> say: </p>
                            <p><?=$p['message']?> At <?= date ('H:i ', strtotime($p['date']))?></p>
                       </div>
                        <div class="col-md-6">
 <!-- ВОТ ЗДЕСЬ -->                           <a class="btn btn-info pull-right" href="index.php?action=like&id=<?=$p['id']?>">like <?=like_count ($connect, $p['id']) ?></a>
                            <a class="btn btn-info  <?php ($name != $p['name'] ? print "disabled" : "") ?> pull-right" href="index.php?action=delete&id=<?=$p['id']?>">delete</a>
                        </div>
                        
                    </div>
                <?php endforeach ?>
            </div>

but I don't think it's right. How to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2015-12-06
@green176

On a good note, the logic should not be called from the template. Is that some widgets to connect.
It is better to count likes in the first request.
Something like this

SELECT m.message_id. m.user_id, m.text, count(like.id) as likes_cnt
FROM message as m
LEFT JOIN like ON m.message_id = like.message_id
GROUP BY m.message_id

message_id | user_id  | text  | likes_cnt
1          | 1        | test  | 0 
2          | 1        | hello | 12

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question