Answer the question
In order to leave comments, you need to log in
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;
}
<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>
$message = messages_all($connect);
include("view/main.php");
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;
}
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;
}
<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>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question