SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Non-Member
    Join Date
    Apr 2010
    Posts
    298
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with PHP news system

    I am using MaxNews PHP script for a project.
    Basically the script allows me to enter content via an admin panel and I just PHP include the content in my home page.

    The problem I am having is that the I ONLY WANT 1 (ONE) news article showing at once only. The script by default has it showing them ALL!
    How can I fix this?

    The files:


    maxNews.class.php
    PHP Code:
    <?php
    /**
     * Max News
     * 
     * This is the Max News business logic class. 
     * For more details please read the readme.txt
     */
    ?>
    <?php
    class maxNews{
       var 
    $newsDir 'news';
       var 
    $newsList;
       var 
    $newsCount 1;
       
    function 
    getNewsList(){
        
       
    $this->newsList = array();
       
        
    // Open the actual directory
        
    if ($handle = @opendir($this->newsDir)) {
            
    // Read all file from the actual directory
            
    while ($file readdir($handle))  {
                if (!
    is_dir($file)) {
                   
    $this->newsList[] = $file;
              }
            }
        }    
        
        
    rsort($this->newsList);
        
        return 
    $this->newsList;
    }   

    function 
    getNewsCount(){
       if (
    $this->newsCount == 1$this->getNewsList();
       
    $this->newsCount sizeof($this->newsList);
       return 
    $this->newsCount;
    }

    function 
    displayNews(){
          
    $list $this->getNewsList();
          
          echo 
    "<table class='newsList'>";
          foreach (
    $list as $value) {
              
    $newsData file($this->newsDir.DIRECTORY_SEPARATOR.$value);
              
    $newsTitle  $newsData[0];
             
    $submitDate $newsData[1];    
             unset (
    $newsData['0']);
             unset (
    $newsData['1']);
              
             
    $newsContent "";
             foreach (
    $newsData as $value) {
                   
    $newsContent .= $value;
             }
              
              echo 
    "<tr><th align='left'>$newsTitle</th>
                        <th class='right'>
    $submitDate</th></tr>";
              echo 
    "<tr><td colspan='2'>".$newsContent."<br/></td></tr>";
          }
          echo 
    "</table>";
          if (
    sizeof($list) == 0){
             echo 
    "<center><p>No news at the moment!</p><p>&nbsp;</p></center>";
          }
    }

    function 
    displayAddForm(){
    ?>  
       <script language="javascript" type="text/javascript" src="js/tiny_mce.js"></script>
    <script language="javascript" type="text/javascript">
    tinyMCE.init({
        mode : "textareas",
        theme : "advanced",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_align : "center",
        theme_advanced_toolbar_location : "top",

    });
    </script>  
      <form class="iform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        News title:<br/>
        <input type="text" name="title" size="40"/><br/><br/>
        Content:<br/>
        <textarea name="newstext" rows="15" cols="67"></textarea><br/>
        <center><input type="submit" name="submit" value="Save" /></center>
      </form> 
       
    <?php   
    }

    function 
    insertNews(){
       
    $newsTitel   = isset($_POST['title']) ? $_POST['title'] : 'Untitled';
       
    $submitDate  date('Y-m-d g:i:s A');
       
    $newsContent = isset($_POST['newstext']) ? $_POST['newstext'] : 'No content';
       
       
    $filename date('YmdHis');
       if (!
    file_exists($this->newsDir)){
          
    mkdir($this->newsDir);
       }
       
    $f fopen($this->newsDir.DIRECTORY_SEPARATOR.$filename.".txt","w+");         
       
    fwrite($f,$newsTitel."\n");
       
    fwrite($f,$submitDate."\n");
       
    fwrite($f,$newsContent."\n");
       
    fclose($f);

       
    header('Location:index.php');   
       
    }
    }
    ?>
    admin.php
    PHP Code:


    <?php
    /**
     * Max News
     * 
     * This is the Max News administration panel. 
     * For more details please read the readme.txt
     */
    ?>
    <?php
    require_once("maxNews.class.php"); 
    $newsHandler = new maxNews();  
    if (!isset(
    $_POST['submit'])) {
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       <title>Admin panel</title>
       <link href="style/style.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <div id="container">
        <div id="header"><div id="header_left"></div>
        <div id="header_main">Admin Panel</div><div id="header_right"></div></div>
        <div id="content">
          <?php $newsHandler->displayAddForm(); ?>     
        </div>
        
    </div>
    </body>
    </html> 

    <?php 
    } else {
       
    $newsHandler->insertNews();
    }
    ?>

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Before the loop add the following which will force only one article to be shown:
    Code PHP:
    $list = $list[0];

    See the below for a much cleaner output of the function which will avoid errors in the loop if the $list array is empty:
    Code PHP:
    function displayNews() {
        $list = $this->getNewsList();
     
        if (sizeof($list) >= 1) {
            $list = $list[0];
     
            echo "<table class='newsList'>";
     
            foreach ($list as $value) {
                $newsData   = file($this->newsDir . DIRECTORY_SEPARATOR . $value);
                $newsTitle  = $newsData[0];
                $submitDate = $newsData[1];    
                unset($newsData[0]);
                unset($newsData[1]);
     
                $newsContent = '';
     
                foreach ($newsData as $value) {
                    $newsContent .= $value;
                }
     
                echo "<tr>";
                echo "    <th align='left'>" . $newsTitle . "</th>";
                echo "    <th class='right'>" . $submitDate . "</th>";
                echo "</tr>";
                echo "<tr>";
                echo "    <td colspan='2'>" . $newsContent . "<br/></td>";
                echo "</tr>";
            }
     
            echo "</table>";
        } else {
            echo "<center><p>No news at the moment!</p><p>&nbsp;</p></center>";
        }
    }
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •