dsjpik057730 2014-04-04 12:02
浏览 28
已采纳

HTML选取框更改行为

If I put marquee in while loop it shows all the data at the same time, sliding right to left. I want this to behave one by one, when one finishes the second starts... and lastly the first comes again, how can I do this please help me out I would be very grateful to you, thanks in advance :)

$data=mysql_query("SELECT heading from moto_static_pages");
while($res = mysql_fetch_assoc($data)){ 
  echo '<marquee  behavior="scroll" direction="left" style="padding:0 0 10px 0;">';
  echo $res['heading'];
  echo '</marquee>';
}
  • 写回答

1条回答 默认 最新

  • dongyan5641 2014-04-04 12:12
    关注

    PHP is server-side. Everything is computed on the server, and then all at the same time displayed on the browser in-front of you. So your loop is merely looping around what eventually will be displayed in the browser, and printing it all out at once, which is what it's supposed to do.

    You will need to use JavaScript to accomplish this, as this is client-side. You cannot do this with PHP.

    You will need to retrieve all the data from your database, convert it to json (json_encode()), and send this to your browser as one string. Then, in JavaScript, loop through the json and print out each one as a marquee with a timed event.

    To reiterate, unless you are going to be doing something with asynchronous I/O with PHP, which you aren't, you will need to do this with JavaScript.


    This is untested code and straight from the top of my head, but hopefully this will point you in the right direction.

    <?php
    
    // Server-side code, get your data, json encode it too!
    
    $data = json_encode(mysql_query("SELECT heading from moto_static_pages"));
    
    ?>
    
    <script type = "text/javascript">
    
        // Client side code that happens in real time in the browser
    
        // Get your data from your php variable
        var data = JSON.parse(<?php echo $data; ?>);
    
        // Loop your data
        for (var i = 0; i < data.length; i++)
        {
            // Output your data in marquees
            setTimeout(function() {
                document.write("<marquee behaviour='scroll' direction='left'>");
                document.write(data[i].heading);
                document.write("</marquee>");
            }, 4000); // Every 4 seconds
        }
    
    </script>
    

    Don't expect this code to just work for you, so get debugging it and using var_dump() to see what you get from PHP, and console.log() to see what you have in JavaScript.

    Final Note

    mysql_query has been deprecated and should be used with caution. You should check out PDO / mysqli instead; they're not too hard to understand and they're interesting too!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥15 C#不用正则表达式如何全字匹配
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题