PHP MySQL HTML Newsticker

796 Views Asked by At

i've the following problem and I don't know how I can fix this right now.

This is my current code

<?php   
    $connect = mysqli_connect("localhost", "root", "", "mkeey");  
    $sql = "SELECT * FROM tbl_news ORDER BY id LIMIT 5";  
    $result = mysqli_query($connect, $sql);  
?>  
<html>  
<head>    
    <meta charset="utf-8">  
    <title>Newsticker Test</title>   
    <link href="content/css/test.css" rel="stylesheet" />  
</head>  
    <body>   
        <div class="marquee">
            <div>
                <?php  
                    if(mysqli_num_rows($result) > 0)  
                    {  
                        while($row = mysqli_fetch_array($result))  
                        {  
                            $title = $row['news_title'];
                            $message = $row['news_text'];
                            echo '<span><b>['.$title.']</b> : '.$message.'</span>';
                        }  

                    }  
                ?>
            </div> 
        </div>
    </body>  
</html>  

CSS


body { margin: 20px; }

.marquee {
  height: 55px;
  width: 50%;

  overflow: hidden;
  position: relative;
}

.marquee div {
  display: block;
  width: 200%;
  height: 30px;

  position: absolute;
  overflow: hidden;

  animation: marquee 5s linear infinite;
}

.marquee span {
  float: left;
  width: 50%;
}

@keyframes marquee {
  0% { left: 0; }
  100% { left: -100%; }
}

The following problem, when I do the whole thing, the variables are constantly overwritten at the end of the border. This means that News1 is only visible and then News2 is briefly visible on the far left. How do I fix this problem?

So anyone who could help would be nice, thank you.

1

There are 1 best solutions below

0
On

Your echo is outside the loop. That's why only the last entry is printed. Put the echo into the loop and it'll fix the issue.

<?php  
if(mysqli_num_rows($result) > 0)  
{  
    while($row = mysqli_fetch_array($result))  
    {  
    $title = $row['news_title'];
    $message = $row['news_text'];
    echo '<marquee behavior="scroll" direction="left" onmouseover="this.stop();" onmouseout="this.start();"><b>['.$title.']</b> : '.$message.'</marquee>';
    }  
}  
?>  

Note: I don't want to start a flame about <marquee> being non-standard. Just be aware of that. Please consider using standard HTML elements or a combination of them: a, p, div, ... Note that you can create marquee effect using pure standard HTML and CSS. Just search for "css only marquee".