使用来自Mysql数据库的Javascript旋转文本

时间:2013-04-22 20:26:19

标签: php javascript mysql

我试图每隔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数组?

e.g。

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

1 个答案:

答案 0 :(得分:0)

在这里,看一下:

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

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

for($count = 0; $count < $num_rows; $count++)
{
    texts.push( ".$row['Text'].");  
}