我想问一下我应该如何将foreach循环转换为每个回显表内容的php代码?
这是我的.php
<?php
require_once 'init.php';
$articleQuery = $db->query("
SELECT
articles.id,
articles.title,
COUNT(articles_likes.id) AS likes,
GROUP_CONCAT(users.username SEPARATOR '|') AS liked_by
FROM articles
LEFT JOIN articles_likes
ON articles.id = articles_likes.article
LEFT JOIN users
ON articles_likes.user = users.id
GROUP BY articles.id
");
while($row = $articleQuery->fetch_object()){
$row->liked_by = $row->liked_by ? explode('|', $row->liked_by) : [];
$articles[] = $row;
}
$articleQuery = $db->query("
SELECT
articles2.id,
articles2.title,
COUNT(articles_dislikes.id) AS dislikes,
GROUP_CONCAT(users.username SEPARATOR '|') AS disliked_by
FROM articles2
LEFT JOIN articles_dislikes
ON articles2.id = articles_dislikes.article
LEFT JOIN users
ON articles_dislikes.user = users.id
GROUP BY articles2.id
");
while($row = $articleQuery->fetch_object()){
$row->disliked_by = $row->disliked_by ? explode('|', $row->disliked_by)
: [];
$articles2[] = $row;
}
// echo '<pre>', print_r($articles, true), '</pre>';
?>
<?php foreach($articles as $article): ?>
<?php foreach($articles2 as $article1): ?>
<div class="article">
<h3><?php echo $article->title; ?></h3>
<a href="like.php?type=article&id=<?php echo $article->id; ?
>">Like<?php echo $article->likes; ?></a>
<div class="article1">
<a href="dislike.php?type=article&id=<?php echo $article1->id;
?>">disLike<?php echo $article1->dislikes; ?></a>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
我想回应每个html帖子的特定表标题,id等,
这是我的table.sql
-- Table structure for table `articles`
--
CREATE TABLE IF NOT EXISTS `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
-- Dumping data for table `articles`
INSERT INTO `articles` (`id`, `title`) VALUES
(1, 'Test article one'),
(2, 'Test article two'),
(3, 'Test article three');
--------------------------------------------------------
-- Table structure for table `articles_likes`
--
CREATE TABLE IF NOT EXISTS `articles_likes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` int(11) DEFAULT NULL,
`article` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
--
-- Dumping data for table `articles_likes`
--
INSERT INTO `articles_likes` (`id`, `user`, `article`) VALUES
(1, 1, 1),
(2, 1, 2),
(4, 2, 2);
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `username`) VALUES
(1, 'ifah'),
(2, 'alex');
这意味着当我在html中发表文章时,我想回应让我们在h3 html中说标题1谢谢..
答案 0 :(得分:0)
是的,我当然可以,我想先谢谢你的回答。 sample
所以基本上,我想计算一个帖子中的点击次数作为一个类似的按钮,但是当我想要回显它们时,我只知道要使用循环显示。
我想在需要时逐个回显它们,比如使用按钮标签或div标签来回显它们。我是一名具有科学背景的大学生,我努力学习编码但不熟悉PHP。那么告诉我你的想法。谢谢!! 我的table.sql
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `likebutton`
-- --------------------------------------------------------
--
-- Table structure for table `articles`
--
CREATE TABLE IF NOT EXISTS `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Dumping data for table `articles`
--
INSERT INTO `articles` (`id`, `title`) VALUES
(1, 'Test article one'),
(2, 'Test article two'),
(3, 'Test article three');
-- --------------------------------------------------------
-- Table structure for table `articles_likes`
--
CREATE TABLE IF NOT EXISTS `articles_likes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` int(11) DEFAULT NULL,
`article` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
--
-- Dumping data for table `articles_likes`
--
INSERT INTO `articles_likes` (`id`, `user`, `article`) VALUES
(1, 1, 1),
(2, 1, 2),
(4, 2, 2);
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `username`) VALUES
(1, 'Anns'),
(2, 'Bob');
此外,我希望每个id只能点击一次,并且可以返回到不同于按钮。感谢