【问题标题】:PHP what should I do if I want to echo table instead of foreach loopsPHP如果我想回显表而不是foreach循环我该怎么办
【发布时间】:2017-04-26 16:17:43
【问题描述】:

我想问我应该如何将我的 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 和 like,

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

【问题讨论】:

  • 表现出一些努力,尝试一些然后来寻求帮助。
  • 你能添加视觉效果吗,你希望你的输出是什么样的。
  • 当然可以,谢谢julekgwa。我只学习了 HTML 和 CSS,从来没有尝试过 PHP,我会更多地练习,以便有一天我可以帮助别人。 :)

标签: php html mysql foreach


【解决方案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只能点击一次,并且可以返回不同的按钮。谢谢

【讨论】:

    猜你喜欢
    • 2022-07-25
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 2022-01-05
    • 2016-01-20
    相关资源
    最近更新 更多