首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Javascript从Mysql数据库中旋转文本

问使用Javascript从Mysql数据库中旋转文本
EN

Stack Overflow用户
提问于 2013-04-23 04:26:19
回答 1查看 745关注 0票数 0

我正在尝试每5秒轮换一次表格单元格中的文本。但我需要从mysql数据库中获取数据。这是我目前使用的代码,它使用一个html字幕。(代码在php中)。

代码语言:javascript
复制
echo "<marquee behavior=\Slide\" scrollamount=\"3\" direction=\"left\"height=\"100px\">";  
echo "<a href=\"".$root."Index.php?page=diary\"><table><tr><td ><h2>--Upcoming Events--</h2></td></tr></table></a>";
echo "<table width=\"50px\"><tr><td></td></tr></table>";
for($count = 0; $count < $num_rows; $count++)
{
    $row = mysql_fetch_array($result);
    echo "<a href=\"".$root."Index.php?page=diary\"><table class=\"footer-inner\"><tr><td><p2>Event: ".$row['Title']."</p2></td><td width=\"10px\"></td>";
    echo "<td><p2>Date: ".$row['Date']."</p2></td></tr>";
    echo "<tr> <td><p2>Start Time: ".$row['Start Time']."</p2></td><td width=\"10px\"></td>";
    echo "<td rowspan=\"3\"><p2>Details: ".$row['Text']."</p2></td></tr>";
    echo "<tr><td><p2>End Time: ".$row['End Time']."</p2></td></tr>";
    echo "<tr><td><p2>Location: ".$row['Location']."</p2></td></tr></table></a>";
    echo "<table width=\"50px\"><tr><td></td></tr></table>";
}
echo "</marquee>";

我已经对此做了一些研究,我发现了如何每隔x秒轮换文本,代码如下:

代码语言:javascript
复制
<html>
<head>
<title>Rotating Text</title>
<script type="text/javascript">
var rotatingTextElement;
var rotatingText = new Array();
var ctr = 0;

function initRotateText() {
rotatingTextElement = document.getElementById("textToChange");
rotatingText[0] = rotatingTextElement.innerHTML; // store the content that's already on the page
rotatingText[1] = "Ten Reason to Buy Gumballs";
rotatingText[2] = "White House bla bla";
setInterval(rotateText, 5000);
}
function rotateText() {
ctr++;
if(ctr >= rotatingText.length) {
ctr = 0;
}
rotatingTextElement.innerHTML = rotatingText[ctr];
}
window.onload = initRotateText;
</script>
</head>
<body>
<span id="textToChange">New Release... Leopard</span>
</body>
</html> 

但正如你所看到的,这只做了一行,是的,我可以对表中的每个值都这样做,我对此很满意。但是如何从mysql数据库中获得我想要的内容并将其插入到数组中呢?

我使用php创建页面,我可以使用php获取所有需要的信息,只需将其更改为java脚本格式即可。

--编辑

是否有可能在同一段代码中将php数组转换为javascript数组?

例如:

代码语言:javascript
复制
var rotatingText = Arrayfuction('{php tag} echo $phparray; {php tag}');
EN

回答 1

Stack Overflow用户

发布于 2013-04-23 05:36:23

在这里,看看这个:

http://jsfiddle.net/WJk8Q/3/

我认为你的php代码应该类似于:

代码语言:javascript
复制
for($count = 0; $count < $num_rows; $count++)
{
    texts.push( ".$row['Text'].");  
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16156092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档