简体   繁体   English

动态新闻文章页面

[英]Dynamic News Article Page

I am currently updating a site for the company SMI.我目前正在更新 SMI 公司的网站。 Their website has a news page http://www.smitmc.com/news that I wish to include in their new site.他们的网站有一个新闻页面http://www.smitmc.com/news ,我希望将其包含在他们的新网站中。

I have attempted to do this through PHP by loading text files in a folder and display them in a div.我试图通过在文件夹中加载文本文件并在 div 中显示它们来通过 PHP 执行此操作。

This is my PHP这是我的 PHP

    <?php
    $time = array();
    $text = array();
    $title = array();
    foreach (glob("news/*.txt") as $filename)
    {
        $fileText = file_get_contents($filename);

        $title[] = basename($filename, ".txt");;
        $text[] = addslashes($fileText);
        $time[] = "Date Uploaded: ".date("F d Y",filemtime($filename));
    }
?>

And this is my javascript这是我的 javascript

<script type="text/javascript">     
        window.onload = function () 
        {
            var arr = new Array("<?php echo implode('","',$time)?>");
            var arrText = new Array("<?php echo implode('","',$text)?>");
            var arrTitle = new Array("<?php echo implode('","',$title)?>");
            for (var i = 0; i < arrText.length; i ++) 
            {
                var div = document.createElement ("div");
                var titleText = document.createElement("h1");
                var dateStamp = document.createElement("p");
                var articleText = document.createElement("p");

                div.style.border = "1px solid #c2c2c2";
                div.style.background = "#f2f2f2";
                div.style.borderRadius = "3px";
                div.style.margin = "20px";
                div.style.padding = "10px";
                div.style.fontFamily = "Amble";
                div.style.color = "#555";
                div.style.fontSize = "12px";
                div.id = arrTitle[i];

                dateStamp.style.position = "relative";
                dateStamp.style.left = "-10px";
                dateStamp.style.top = "-10px";
                dateStamp.style.fontSize = "16px";
                dateStamp.style.color = "#be1111";

                articleText.style.position = "relative";
                articleText.style.left = "-10px";
                articleText.style.top = "-10px";
                articleText.style.fontSize = "14px";

                titleText.innerHTML = arrTitle[i] ;
                dateStamp.innerHTML = arr[i];
                articleText.innerHTML = arrText[i];

                 document.getElementById("newsBlock").appendChild (div);
                 document.getElementById(arrTitle[i]).appendChild(titleText);
                 document.getElementById(arrTitle[i]).appendChild(dateStamp);
                 document.getElementById(arrTitle[i]).appendChild(articleText);
            }
         }
    </script>

The problem arises when I try to read a multi-line text file.当我尝试读取多行文本文件时出现问题。 If the text file is a single line of text I have no issues.如果文本文件是单行文本,我没有问题。 Of course this is unacceptable.当然,这是不可接受的。

I am wondering how I can parse a multi-line text file and load it up and display it using this code.我想知道如何解析多行文本文件并使用此代码加载并显示它。 I am not interested in a CMS so please do not suggest this as an option.我对 CMS 不感兴趣,所以请不要建议将其作为选项。

Thanks谢谢

I think the best solution for you if you don't mind to use HTML5 is to use FileReader.如果您不介意使用 HTML5,我认为对您来说最好的解决方案是使用 FileReader。

http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html#fbid=wUtRDwEASPK http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html#fbid=wUtRDwEASPK

You will have here all the needed instruction on how to use it.您将在此处获得有关如何使用它的所有必要说明。 I use this currently for my file uploads.我目前使用它来上传文件。

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

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