简体   繁体   English

来自数据库的实时新闻行情

[英]Live news ticker from database

I tried to find on internet some simple solution for making news ticker that will update by it self from data from database using. 我试图在互联网上找到一些简单的解决方案,以使新闻自动收录器可以自动使用数据库中的数据进行更新。 this is what I have for now. 这就是我现在所拥有的。 can you please help me? 你能帮我么?

<html>
<body>
 <?php
  $dbhost='localhost';
  $dbuser='root';
  $dbpass='password';
  $db='base';
  $conn = @mysql_connect($dbhost,$dbuser,$dbpass);
  mysql_select_db($db);
  $query= "SELECT * FROM mbblog ORDER BY id DESC";
  $result= mysql_query ($query);
  $news = $mbblog['short_news'];
 ?>

  <marquee behavior="scroll" direction="left" 
      onmouseover="this.stop();" 
      onmouseout="this.start();">
        <h1><?php echo $news; ?></h1>
  </marquee>

</body>
</html>

Thank you 谢谢

I'm guessing that your question is that you aren't getting the data from the database? 我猜您的问题是您不是从数据库中获取数据吗? If so, it looks like your missing a line of code that reads the information your getting from the query. 如果是这样,则好像您缺少一行代码,该代码读取了从查询中获取的信息。

I believe that this is what it should look like... 我相信这是应该的样子...

<?php
 $dbhost = 'localhost';
 $dbuser = 'root';
 $dbpass = 'password';
 $db = 'base';
 $conn = mysql_pconnect($dbhost,$dbuser,$dbpass);
 mysql_select_db($db, $conn);

 $result = mysql_query("SELECT * FROM mbblog ORDER BY id DESC");
 $mbblog = mysql_fetch_assoc($result);
 $news = $mbblog['short_news'];

 mysql_close($conn);
?>

And if you wanted it to automatically update (say every 5 seconds) you could use AJAX, simply place the code above on an external page making sure it echo s your $news variable, and using a JavaScript loop, update the content of a <div> using AJAX to call the content of the external page every time the loop runs. 而且,如果您希望它自动更新(例如每5秒更新一次),则可以使用AJAX,只需将上面的代码放在外部页面上,以确保它echo您的$news变量,然后使用JavaScript循环,更新<div>在每次循环运行时使用AJAX调用外部页面的内容。

That would work something like this... 这样会起作用...

window.setInterval(function get()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET","LOCATION_OF_CODE.php?session=" + Math.random(),true);
    xmlhttp.send();

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("ELEMENT_TO_DISPLAY_CONTENT").innerHTML=xmlhttp.responseText;
        }
    }
}, 5000);

So for your marquee code, you would place a <div> tag inside the <marquee> and give it an ID to link to the AJAX. 因此,对于字幕代码,您可以在<marquee>内放置一个<div>标记,并为其指定一个ID以链接到AJAX。

Hope this helps! 希望这可以帮助!

Other than the issues that the people have commented about... 除了人们评论的问题之外...

The best way to accomplish this is to set an ajax call to run on a timer. 实现此目的的最佳方法是将ajax调用设置为在计时器上运行。 Once the timer is fired then make the ajax call to your database and pull the data. 计时器启动后,请对数据库进行ajax调用并提取数据。 You can even add functionality to find the Max ID every time there is an update and check that ID versus the Max ID in the database to cut down on some executions. 您甚至可以添加功能,以在每次更新时查找最大ID,并检查ID与数据库中的最大ID的比较,以减少某些执行。

You can use jQuery for this 您可以为此使用jQuery

var ajax_call = function() {
  //your jQuery ajax code
};

var interval = 1000 * 60 * X; // where X is your every X minutes

setInterval(ajax_call, interval);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM