php - PHP MySQL HTML 新贴纸
问题描述
我有以下问题,我现在不知道如何解决这个问题。
这是我当前的代码
<?php
$connect = mysqli_connect("localhost", "root", "", "mkeey");
$sql = "SELECT * FROM tbl_news ORDER BY id LIMIT 5";
$result = mysqli_query($connect, $sql);
?>
<html>
<head>
<meta charset="utf-8">
<title>Newsticker Test</title>
<link href="content/css/test.css" rel="stylesheet" />
</head>
<body>
<div class="marquee">
<div>
<?php
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$title = $row['news_title'];
$message = $row['news_text'];
echo '<span><b>['.$title.']</b> : '.$message.'</span>';
}
}
?>
</div>
</div>
</body>
</html>
CSS
body { margin: 20px; }
.marquee {
height: 55px;
width: 50%;
overflow: hidden;
position: relative;
}
.marquee div {
display: block;
width: 200%;
height: 30px;
position: absolute;
overflow: hidden;
animation: marquee 5s linear infinite;
}
.marquee span {
float: left;
width: 50%;
}
@keyframes marquee {
0% { left: 0; }
100% { left: -100%; }
}
下面的问题,当我做整个事情的时候,变量在边界的末端不断被覆盖。这意味着 News1 仅可见,然后 News2 在最左侧短暂可见。我该如何解决这个问题?
因此,任何可以提供帮助的人都会很好,谢谢。
解决方案
你echo
在循环之外。这就是为什么只打印最后一个条目的原因。将其echo
放入循环中,它将解决问题。
<?php
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$title = $row['news_title'];
$message = $row['news_text'];
echo '<marquee behavior="scroll" direction="left" onmouseover="this.stop();" onmouseout="this.start();"><b>['.$title.']</b> : '.$message.'</marquee>';
}
}
?>
注意:我不想<marquee>
因为不标准而大发雷霆。请注意这一点。请考虑使用标准 HTML 元素或它们的组合:a
, p
, div
, ... 请注意,您可以使用纯标准 HTML 和 CSS 创建选取框效果。只需搜索“css only marquee”。
推荐阅读
- python - 如何在训练/验证/测试中对齐 pandas get_dummies?
- ios - 使用 webrtc iOS swift 在视频通话期间纠正 iPhoneX 上的远程视频大小
- javascript - 如何使用 redux-saga 缓冲操作
- hana - 如何根据“时间维度”在计算视图中为每一天创建一个“历史”表
- angularjs - 是否可以将 ng-class 与 ng-style 结合使用
- unit-testing - 为发布管道创建任务组时搜索文件夹值
- excel - 如何使用条件语句(隐藏/取消隐藏列和锁定/保护单元格)调试受保护的工作表,使其工作得更快
- html - 即使显示最大宽度,分词也不起作用
小于最大值
- android - Colors.xml 正在生成一些不完整的与 gradle 相关的代码,即使替换为正确的代码似乎也不起作用
- sql - 查询与多个实体类型相关的表