JavaScript 循环中的输出数据库行
我想制作数据库数据的幻灯片。我使用 php 打印出行,然后将其放入 html 代码中,然后我想使用 javascript 使用 setInterval()
循环来循环代码。
我的问题是:如何输出行并使用 javascript 来显示它?因为我尝试做的事情没有成功。我对 javascript 没有经验,我尝试查找并尝试不同的方法,但由于某种原因它不起作用。
这是我最后尝试做的,但它不会在页面上打印任何内容。
...
$sel_query = "SELECT * FROM `classifieds` WHERE `title` !=''";
$results = mysqli_set_charset($conn,"utf8");
$results = mysqli_query($conn,$sel_query);
while ($row = mysqli_fetch_array($results)) {
?>
...
<body>
<div id="div"> </div>
<script type="text/javascript">
setInterval(displayCode, 1000);
var codeBlock = '<div id="slider-frame" class="slider-frame">' +
'<div class="slide-images">' +
'<div class="img-container">' +
'<h1>' + '<?php echo $row['title']; ?>' + '</h1>' +
'</div>' +
'<p>' + '<?php echo $row['description']; ?>' + '</p>' +
'</div> </div>';
function displayCode() {
document.getElementById("div").innerHTML += codeBlock;
}
</script>
<?php } ?>
</body>
</html>
I want to make a slideshow of data from database. I use php to print out the rows, then I placed that in a html code, then I want to use javascript to loop the code using the setInterval()
loop.
My question is: how can I output the rows and use javascript to display that? because what I tried to do didn't work. I'm not experienced with javascript, and I tried to look up and try out different methods but it doesn't work for some reason.
This is what I tried to do last, but it does not print anything on page.
...
$sel_query = "SELECT * FROM `classifieds` WHERE `title` !=''";
$results = mysqli_set_charset($conn,"utf8");
$results = mysqli_query($conn,$sel_query);
while ($row = mysqli_fetch_array($results)) {
?>
...
<body>
<div id="div"> </div>
<script type="text/javascript">
setInterval(displayCode, 1000);
var codeBlock = '<div id="slider-frame" class="slider-frame">' +
'<div class="slide-images">' +
'<div class="img-container">' +
'<h1>' + '<?php echo $row['title']; ?>' + '</h1>' +
'</div>' +
'<p>' + '<?php echo $row['description']; ?>' + '</p>' +
'</div> </div>';
function displayCode() {
document.getElementById("div").innerHTML += codeBlock;
}
</script>
<?php } ?>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的问题是我的 javascript 代码中的 不起作用。它没有回应我想要的行。我认为发生这种情况是因为 var codeBlock 中的 + 。删除它们并使 var 成为一个字符串后,它就起作用了。
My problem was that the in my javascript code, did not work. It did not echo the row I wanted. I figured that this was happening because of the + in the var codeBlock. After deleting them and making the var one string it worked.