Limiting the # of articles shown on a PHP CMS?

Discussion in 'PHP' started by 5thround, May 21, 2007.

  1. #1
    Anyone have an idea on how I would go about limiting the number of headlines show up on a page that I use the PHP Include code on? My current code is below, thanks for the tips!

    <html>
    <body bgcolor=#f4efce>
    
    <basefont size=2 face=arial>
    
    <b>Add Article</b>
    <?
            include ("template.inc");
            include ("config.php");
    	
    	$subject = $_POST[subject];
    	$summary = $_POST[summary];
    	$passwd = $_POST[passwd];
    	$date = $_POST[date];
    	$author = $_POST[author];
    	$body = $_POST[body];
    	$article_id = $_POST[article_id];
    
    	#foreach($GLOBALS as $a => $b){	print "<li>$a => $b";}
    
    	$summary_template = "t_summary.php";
    	$article_template = "t_article.php";
    	$headline_template = "t_headline.php";
                    $max_summary = 1;
                    $max_headlines = 15;
    
    	function summary_page ($subject, $date, $author, $summary, $article_id)
    	{
    		global $summary_template;
            	$t = new Template();
            	$t->set_file("SummaryPage", $summary_template);
    		$article_url = "".$article_id.".php";
    		$date = nl2br($date);
    		$author = nl2br($author);
    		$summary =  nl2br($summary);	 
    		$t->set_var( array(
    				"subject" => $subject,
    				"date"    => $date,
    				"author"    => $author,
    				"summary" => $summary,
    				"article_url" => $article_url
    				));
    		$t->parse("Summary", "SummaryPage");
    		return $t->get_var("Summary");
    	}
    
    	function headline_page ($subject, $date, $author, $article_id)
    	{
    		global $headline_template;
            	$t = new Template();
            	$t->set_file("HeadlinePage", $headline_template);
    		$article_url = "".$article_id.".php";
    		$date = nl2br($date); 
    		$author = nl2br($author);
    		$t->set_var( array(
    				"subject" => $subject,
    				"date"    => $date,
    				"author"    => $author,
    				"article_url" => $article_url
    				));
    		$t->parse("Headline", "HeadlinePage");
    		return $t->get_var("Headline");
    	}
    
    	function main_page ($subject, $date, $author, $summary, $article_id, $body)
    	{
    		global $article_template;
    
                    $t = new Template();
                    $t->set_file("ArticlePage", $article_template);
                    $article_url = "".$article_id.".php";
                    $date = nl2br($date);
                    $author = nl2br($author);
                    $summary =  nl2br($summary);
                    $body =  nl2br($body);
                    $t->set_var( array(
                                    "subject" => $subject,
                                    "date"    => $date,
                                    "author"    => $author,
                                    "summary" => $summary,
                                    "body" => $body,
                                    "article_url" => $article_url
                                    ));
                    $t->parse("Article", "ArticlePage");
                    return $t->get_var("Article"); 
    	}
    
    	function add_article($filename, $news)
    	{
    
    		if(file_exists($filename)){
    			$fh = fopen($filename, "r");
    			$old_news = fread($fh, filesize($filename));
    			fclose($fh); 
    		}
    
    		/* TODO: Multipage articles
    			preg_match_all("<!--ARTICLE PAGE=(\d*)-->", $old_news, $matches;
    		
    			if( count($matches[0]) >= $max_summary){
    				$oldfilename = $filename.($matches[0][0]+1);
    			} 
    		*/
    
    		$fh = fopen($filename, "w");
    		$news = stripslashes($news);
    		fwrite($fh, "\n<!--ARTICLE-->\n$news $old_news");
    		fclose($fh);
    	}
    
    	if(strcmp($subject, "")){	
    		if(!(strcmp($passwd, $password))){	
    			add_article("article_summary.php", summary_page($subject, $date, $author, $summary, $article_id));
    			add_article("headlines.php", headline_page($subject, $date, $author, $article_id));
    			add_article("$article_id.php", main_page($subject, $date, $author, $summary, $article_id, $body));
    			echo "<p> <a href=$article_id.php>Article</a> has been added! <p>";
    		}else{
    			echo "<p><b> Password is wrong! </b>";
    		}
    	}
    ?>
    
    Code (markup):
     
    5thround, May 21, 2007 IP
  2. manilodisan

    manilodisan Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You tried changing $max_headlines = 15; from 15 to another value?
     
    manilodisan, May 21, 2007 IP
  3. 5thround

    5thround Active Member

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    $max_headlines was something that I edited into the config.php file. $max_summary was already in the file so I just kinda played around with it. Neither of the max_ codes work.
     
    5thround, May 21, 2007 IP
  4. manilodisan

    manilodisan Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In this case I don't think that the solution is in the code you shared. It might be where the functions are called or in the included files (config?)
     
    manilodisan, May 21, 2007 IP
  5. 5thround

    5thround Active Member

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    Alright, I'll take a look at it in the morning. It's an awkward script because it's PHPLib which is something I haven't really looked at yet, so the PHP in the other files looks almost 100% HTML with the exception of a couple things.
     
    5thround, May 22, 2007 IP
  6. 5thround

    5thround Active Member

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    Do you think that the solution is in the code below?

    
    
    <?
    
            require('config.php'); 
    
    	$filename = "article_summary.php";
    
    	#- open article summaries
    	if(file_exists($filename)){
    		$fh = fopen($filename, "r");
    		$old_news = fread($fh, filesize($filename));
    		fclose($fh);
    	}
    
     
    	#- get first five article
    	$articles = explode("<!--ARTICLE-->", $old_news);
    
    	$i=0;
    	foreach ( $articles as $article ){
    		if(count($articles)>$i){
    			if($max_latest >= $i++){
    				print $article;
    			}
    		}
    	}
    
    ?>
    
    
    
    
    Code (markup):
     
    5thround, May 22, 2007 IP