1

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

Vkitor
  • 57
  • 7
  • 4
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Dec 16 '14 at 16:47
  • How is this not working? Other than `$mbblog` being completely undefined – Marc B Dec 16 '14 at 16:48
  • You have to fetch the array into your $mbblog variable after executing the query, see http://php.net/manual/function.mysql-fetch-array.php (but don't use deprecated functions). – hellcode Dec 16 '14 at 16:55
  • 1
    Please don't use `` either! – fire Dec 16 '14 at 17:01

3 Answers3

1

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 echos 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.

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.

Hope this helps!

MattFiler
  • 175
  • 2
  • 15
0

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. Once the timer is fired then make the ajax call to your database and pull the data. 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.

Eric Anderson
  • 358
  • 2
  • 4
  • 15
0

You can use jQuery for this

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

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

setInterval(ajax_call, interval);
jay.jivani
  • 1,560
  • 1
  • 16
  • 33