0

我正在尝试每 5 秒旋转一次表格单元格中的文本。但我需要从 mysql 数据库中获取数据。这是我拥有的使用 html 字幕的当前代码。(代码在 php 中)。

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 秒旋转一次文本,代码如下:

<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 数组?

例如

var rotatingText = Arrayfuction('{php tag} echo $phparray; {php tag}');
4

1 回答 1

0

在这里,检查一下:

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

我认为您的 php 代码将与此类似:

for($count = 0; $count < $num_rows; $count++)
{
    texts.push( ".$row['Text'].");  
}
于 2013-04-22T21:36:22.883 回答