gpt4 book ai didi

PHP如果我想回显表而不是foreach循环我该怎么办

转载 作者:行者123 更新时间:2023-11-29 19:04:11 26 4
gpt4 key购买 nike

我想问一下如何将 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 和点赞,

这是我的表.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,谢谢..

最佳答案

是的,我当然可以,我首先要感谢您的回答。 sample

sample likes count

所以基本上,我想计算帖子中的点击次数以充当“点赞”按钮,但是当我想回显它们时,我只知道使用循环来显示。

我想在需要的时候一一回显它们,比如使用按钮标签或div标签来回显它们。我是一名理科背景的大学生,我正在努力学习编码,但不熟悉PHP。那么请告诉我你的想法。谢谢!! 我的表.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只能点击一次并且可以返回,不像按钮。谢谢

关于PHP如果我想回显表而不是foreach循环我该怎么办,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43639600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com