Помощ с редеклариране на клас

C++ JAVA
Post Reply
newfag
Нов
Нов
Posts: 21
Joined: Wed Mar 28, 2012 9:17 am

Помощ с редеклариране на клас

Post by newfag » Fri Nov 30, 2012 3:00 pm

Здравейте, имам една системка за новини обаче идеята ми е да мога да показвам всяка нова новина в различен div, т.е да ги 'разпределям' един вид. Но като се опитам да ги покажа, ми дава грешка относно редеклариране на клас.

Code: Select all

<div class='news'>									
<div class='text'>
<p> 
<?php
include&#40;'news.php'&#41;;

show_news&#40;1&#41;;
?>
<br>
</div>
</div>
и



Toва е кода където го include в сайта ми.

А аз искам да го направя

Code: Select all

<div class='news'>									
<div class='text'>
<p> 
<?php
include&#40;'news.php'&#41;;

show_news&#40;1&#41;;
?>
<br>
</div>
</div>
<div class='news'>									
<div class='text'>
<p> 
<?php
include&#40;'news.php'&#41;;

show_news&#40;1&#41;;
?>
<br>
</div>
</div>&#93;
Но ми дава грешка за редеклариране на клас, както споменах.

Ето това е файла за функцията show_news

Code: Select all

function show_news&#40;$max=-1, $cats=array&#40;&#41;&#41; &#123;
	global $news;
	if &#40;isset&#40;$_GET&#91;'post'&#93;&#41; || isset&#40;$_GET&#91;'archive'&#93;&#41; || isset&#40;$_GET&#91;'category'&#93;&#41; || isset&#40;$_GET&#91;'cat_alias'&#93;&#41; || isset&#40;$_GET&#91;'post_alias'&#93;&#41; || $max==-1&#41; &#123;
		print $news->message;
	&#125; else &#123;
		print $news->getNews&#40;$max, $cats&#41;;
	&#125;
&#125;
както и

Code: Select all

<?php
include&#40;str_replace&#40;'\\', '/', dirname&#40;__FILE__&#41;&#41; . '/functions.php'&#41;;

class NewsDisplay extends WingedNews &#123;
	var $tplpath;
	var $notes;
	var $message;
	var $logfile;
	var $dir;
	var $url;
	
	function NewsDisplay&#40;&#41; &#123;
		$this->WingedNews&#40;&#41;;
		$this->tplpath = $this->path . '/display/' . $this->configs&#91;'display'&#93; . '/';
		$this->logfile = $this->path . '/data/logs.txt';
		$this->notes = parse_ini_file&#40;$this->tplpath . 'note.tpl', true&#41;;
		$this->message = '';
		$this->url = $_SERVER&#91;'PHP_SELF'&#93;;
		$dir = str_replace&#40;'\\', '/', dirname&#40;$this->url&#41;&#41;;
		$this->dir = $dir=='/'? '/' &#58; $dir . '/';
	&#125;
	
	function commentsAllowed&#40;$postid&#41; &#123;
		$posts = $this->getPostDetails&#40;$postid&#41;;
		if &#40;$posts===false&#41; return false;
		return &#40;empty&#40;$posts&#91;'LOCKED'&#93;&#41; && $this->configs&#91;'comment'&#93;&#41;;
	&#125;
	
	function getNews&#40;$max, $cats&#41; &#123;
		$posts = $this->getPostList&#40;$max, $cats&#41;;
		return $this->getOutputPostList&#40;$posts&#41;;
	&#125;
	
	function getArchives&#40;&#41; &#123;
		$details = array&#40;&#41;;
		$sorts_y = array&#40;&#41;;
		$sorts_m = array&#40;&#41;;
		$handle = opendir&#40;$this->postpath&#41;;
		while &#40;&#40;$file=readdir&#40;$handle&#41;&#41;!==false&#41; &#123;
			if &#40;$file&#91;0&#93;=='.'&#41; continue;
			$name = basename&#40;$file, '.txt'&#41;;
			list&#40;$year, $month&#41; = explode&#40;'-', $name&#41;;
			$posts = $this->getPostList&#40;0, array&#40;&#41;, false, $year, $month&#41;;
			if &#40;empty&#40;$posts&#41;&#41; continue;
			$details&#91;&#93; = array &#40;
							'YEAR' => $year,
							'MONTH' => $this->notes&#91;'month'&#93;&#91;$month&#93;,
							'LINK' => $this->configs&#91;'alias'&#93;? $this->dir . $year . '/' . $month . '/' &#58; $this->url . '?archive=' . $name
							&#41;;
			$sorts_y&#91;&#93; = $year;
			$sorts_m&#91;&#93; = $month;
		&#125;
		closedir&#40;$handle&#41;;
		array_multisort&#40;$sorts_y, SORT_DESC, $sorts_m, SORT_DESC, $details&#41;;
		return $this->parseRecTemplate&#40;file_get_contents&#40;$this->tplpath . 'archives.tpl'&#41;, '<!-- BEGIN -->', '<!-- END -->', $details&#41;;
	&#125;

	function getCategories&#40;&#41; &#123;
		$cats = $this->getCats&#40;true&#41;;
		$posts = array&#40;&#41;;
		$num_cat = count&#40;$cats&#41;;
		for &#40;$i=0; $i<$num_cat; $i++&#41; &#123;
			$cats&#91;$i&#93;&#91;'LINK'&#93; = $this->url . '?category=' . $cats&#91;$i&#93;&#91;'CAT_ID'&#93;;
			if &#40;$this->configs&#91;'alias'&#93;&#41; &#123;
				$cats&#91;$i&#93;&#91;'LINK'&#93; = $this->dir . 'category/' . $this->formatAlias&#40;$cats&#91;$i&#93;&#91;'CAT_NAME'&#93;&#41;;
			&#125;
			$posts = array_merge&#40;$posts, $cats&#91;$i&#93;&#91;'POSTS'&#93;&#41;;
			unset&#40;$cats&#91;$i&#93;&#91;'POSTS'&#93;&#41;;
		&#125;
		$diff = count&#40;$this->getPostList&#40;&#41;&#41; - count&#40;array_unique&#40;$posts&#41;&#41;;
		if &#40;$diff&#41; &#123;
			$cats&#91;$num_cat-1&#93;&#91;'NUM_POST'&#93; = $diff;
		&#125; else &#123;
			unset&#40;$cats&#91;$num_cat-1&#93;&#41;;
		&#125;
		return $this->parseRecTemplate&#40;file_get_contents&#40;$this->tplpath . 'categories.tpl'&#41;, '<!-- BEGIN -->', '<!-- END -->', $cats&#41;;
	&#125;
			
	function addComment&#40;$post, $name, $website, $message&#41; &#123;
		if &#40;get_magic_quotes_gpc&#40;&#41;==0&#41; &#123;
			$name = addslashes&#40;$name&#41;;
			$message = addslashes&#40;$message&#41;;
		&#125;
		$id = $this->configs&#91;'daylight'&#93;? time&#40;&#41;+3600 &#58; time&#40;&#41;;
		$content = "\n<comment>\n" . $id . "\n" . $name . "\n" . $website . "\n" . $message . "\n</comment>";
		$handle = fopen&#40;$this->commentpath . $post . '.txt', 'a'&#41; or exit&#40;'error1'&#41;;
		if &#40;fwrite&#40;$handle, $content&#41;===false&#41; &#123;
			fclose&#40;$handle&#41;;
			exit&#40;'error2'&#41;;
		&#125;
		fclose&#40;$handle&#41;;
	&#125;

	function checkFlood&#40;$ip&#41; &#123;
		$current = time&#40;&#41;;
		$ips = parse_ini_file&#40;$this->logfile&#41;;
		if &#40;array_key_exists&#40;$ip, $ips&#41; && $current-$ips&#91;$ip&#93; < $this->configs&#91;'flood'&#93;&#41; return false;
		$mode = filesize&#40;$this->logfile&#41;<10240? 'a' &#58; 'w';
		$handle = fopen&#40;$this->logfile, $mode&#41; or exit&#40;'error1'&#41;;
		$content = $ip . " = " . $current . "\n";
		if &#40;fwrite&#40;$handle, $content&#41;===false&#41; &#123;
			fclose&#40;$handle&#41;;
			exit&#40;'error2'&#41;;
		&#125;
		fclose&#40;$handle&#41;;
		return true;
	&#125;

	function checkBlackList&#40;$ip&#41; &#123;
		$ips = explode&#40;',', $this->configs&#91;'list'&#93;&#41;;
		for &#40;$i=0, $n=count&#40;$ips&#41;; $i<$n; $i++&#41; &#123;
			$text = trim&#40;$ips&#91;$i&#93;&#41;;
			if &#40;!empty&#40;$text&#41;&#41; &#123;
				$pattern = '/' . str_replace&#40;'.', '\.', $text&#41; . '/';
				if &#40;preg_match&#40;$pattern, $ip&#41;>0&#41; return false;
			&#125;
		&#125;
		return true;
	&#125;

	function wordFilter&#40;$message&#41; &#123;
		$patterns = array&#40;&#41;;
		$words = explode&#40;',', $this->configs&#91;'filter'&#93;&#41;;
		for &#40;$i=0, $n=count&#40;$words&#41;; $i<$n; $i++&#41; &#123;
			$text = trim&#40;$words&#91;$i&#93;&#41;;
			if &#40;!empty&#40;$text&#41;&#41; $patterns&#91;&#93; = '/' . $text . '/i';
		&#125;
		return preg_replace&#40;$patterns, '****', $message&#41;;
	&#125;
	
	function getOutputPostList&#40;$posts&#41; &#123;
		for &#40;$i=0, $n=count&#40;$posts&#41;; $i<$n; $i++&#41; &#123;
			$posts&#91;$i&#93;&#91;'LINK'&#93; = $this->url . '?post=' . $posts&#91;$i&#93;&#91;'ID'&#93;;
			if &#40;$this->configs&#91;'alias'&#93;&#41; &#123;
				$dates = $this->getDateArray&#40;$posts&#91;$i&#93;&#91;'ID'&#93;&#41;;
				$posts&#91;$i&#93;&#91;'LINK'&#93; = $this->dir . $dates&#91;'year'&#93; . '/' . $dates&#91;'mon'&#93; . '/' . $dates&#91;'mday'&#93; . '/' .
									$this->formatAlias&#40;$posts&#91;$i&#93;&#91;'SUBJECT'&#93;&#41; . '-' . $dates&#91;'seconds'&#93;;
			&#125;
			$posts&#91;$i&#93;&#91;'MESSAGE'&#93; = $this->str2para&#40;$this->parseBB&#40;$posts&#91;$i&#93;&#91;'MESSAGE'&#93;, true&#41;&#41;;
			$posts&#91;$i&#93;&#91;'CATEGORY'&#93; = '';
			for &#40;$j=0, $m=count&#40;$posts&#91;$i&#93;&#91;'CATS'&#93;&#41;-1; $j<$m; $j++&#41; &#123;
				if &#40;$posts&#91;$i&#93;&#91;'CATS'&#93;&#91;$j&#93;&#91;'CHECK'&#93;&#41; &#123;
					if &#40;$this->configs&#91;'alias'&#93;&#41; &#123;
						$posts&#91;$i&#93;&#91;'CATEGORY'&#93; .= '<a href="' . $this->dir . 'category/' .
												$this->formatAlias&#40;$posts&#91;$i&#93;&#91;'CATS'&#93;&#91;$j&#93;&#91;'CAT_NAME'&#93;&#41; . '">' .
												$posts&#91;$i&#93;&#91;'CATS'&#93;&#91;$j&#93;&#91;'CAT_NAME'&#93; . '</a>, ';
					&#125; else &#123;
						$posts&#91;$i&#93;&#91;'CATEGORY'&#93; .= '<a href="' . $this->url . '?category=' .
												$posts&#91;$i&#93;&#91;'CATS'&#93;&#91;$j&#93;&#91;'CAT_ID'&#93; . '">' .
												$posts&#91;$i&#93;&#91;'CATS'&#93;&#91;$j&#93;&#91;'CAT_NAME'&#93; . '</a>, ';
					&#125;
				&#125;
			&#125;
			if &#40;empty&#40;$posts&#91;$i&#93;&#91;'CATEGORY'&#93;&#41;&#41; &#123;
				$uncats = array_pop&#40;$posts&#91;$i&#93;&#91;'CATS'&#93;&#41;;
				if &#40;$this->configs&#91;'alias'&#93;&#41; &#123;
					$posts&#91;$i&#93;&#91;'CATEGORY'&#93; = '<a href="' . $this->dir . 'category/' .
												$this->formatAlias&#40;$uncats&#91;'CAT_NAME'&#93;&#41; . '">' .
												$uncats&#91;'CAT_NAME'&#93; . '</a>';
				&#125; else &#123;
					$posts&#91;$i&#93;&#91;'CATEGORY'&#93; = '<a href="' . $this->url . '?category=0">' .
												$uncats&#91;'CAT_NAME'&#93; . '</a>';
				&#125;
			&#125; else &#123;
				$posts&#91;$i&#93;&#91;'CATEGORY'&#93; = substr&#40;$posts&#91;$i&#93;&#91;'CATEGORY'&#93;, 0, -2&#41;;
			&#125;
			unset&#40;$posts&#91;$i&#93;&#91;'CATS'&#93;&#41;;
		&#125;
		return $this->parseRecTemplate&#40;file_get_contents&#40;$this->tplpath . 'general.tpl'&#41;, '<!-- BEGIN -->', '<!-- END -->', $posts&#41;;
	&#125;
	
	function getPostByAlias&#40;$year, $month, $day, $alias, $sec&#41; &#123;
		$details = array&#40;&#41;;
		$file = $this->postpath . $year . '-' . $month . '.txt';
		if &#40;!is_file&#40;$file&#41;&#41; return false;
		$lines = file&#40;$file&#41;;
		for &#40;$i=0, $n=count&#40;$lines&#41;; $i<$n; $i++&#41; &#123;
			if &#40;trim&#40;$lines&#91;$i&#93;&#41;=='<post>'&#41; &#123;
				$id = trim&#40;$lines&#91;$i+1&#93;&#41;;
				$dates = $this->getDateArray&#40;$id&#41;;
				$subject = trim&#40;$lines&#91;$i+2&#93;&#41;;
				if &#40;$dates&#91;'mday'&#93;==$day && $dates&#91;'seconds'&#93;==$sec && $this->formatAlias&#40;$subject&#41;==$alias&#41; &#123;
					$details&#91;'ID'&#93; = $id;
					$details&#91;'LOCKED'&#93; = in_array&#40;$id, $this->getLockedPosts&#40;&#41;&#41;? 'checked' &#58; '';
					$details&#91;'SUBJECT'&#93; = htmlspecialchars&#40;stripslashes&#40;$subject&#41;, ENT_QUOTES&#41;;
					$details&#91;'DATE'&#93; = $this->getZoneDate&#40;$this->configs&#91;'date'&#93;, $id&#41;;
					$details&#91;'TIME'&#93; = $this->getZoneDate&#40;$this->configs&#91;'time'&#93;, $id&#41;;
					$message = '';
					for &#40;$i+=3;; $i++&#41; &#123;
						if &#40;trim&#40;$lines&#91;$i&#93;&#41;=='</post>'&#41; break;
						$message .= $lines&#91;$i&#93;;
					&#125;
					$details&#91;'MESSAGE'&#93; = htmlspecialchars&#40;stripslashes&#40;$message&#41;, ENT_QUOTES&#41;;
					$details&#91;'CATS'&#93; = $this->getPostCats&#40;$id&#41;;
					return $details;
				&#125; else $i+=4;
			&#125;
		&#125;
		return false;
	&#125;
	
	function setMessageById&#40;$id&#41; &#123;
		$posts = $this->getPostDetails&#40;$_GET&#91;'post'&#93;&#41;;
		if &#40;$posts!==false&#41; &#123;
			$this->message .= $this->getOutputPost&#40;$posts, $this->tplpath . 'view_post.tpl', $this->tplpath . 'comments.tpl', $this->tplpath . 'comment_form.tpl', $this->dir, $this->url&#41;;
		&#125;
	&#125;
	
	function setMessageByArchive&#40;$archive&#41; &#123;
		$dates = explode&#40;'-', $archive&#41;;
		$year = isset&#40;$dates&#91;0&#93;&#41;? intval&#40;$dates&#91;0&#93;&#41; &#58; 0;
		$month = isset&#40;$dates&#91;1&#93;&#41;? intval&#40;$dates&#91;1&#93;&#41; &#58; 0;
		$day = isset&#40;$dates&#91;2&#93;&#41;? intval&#40;$dates&#91;2&#93;&#41; &#58; 0;
		$posts = $this->getPostList&#40;0, array&#40;&#41;, false, $year, $month, $day&#41;;
		$this->message = $this->getOutputPostList&#40;$posts&#41;;
	&#125;
	
	function setMessageByCatAlias&#40;$alias&#41; &#123;
		$cats = $this->getCats&#40;&#41;;
		for &#40;$i=0, $n=count&#40;$cats&#41;; $i<$n; $i++&#41; &#123;
			if &#40;$this->formatAlias&#40;$cats&#91;$i&#93;&#91;'CAT_NAME'&#93;&#41;==$alias&#41; &#123;
				$posts = $this->getPostList&#40;0, array&#40;$cats&#91;$i&#93;&#91;'CAT_ID'&#93;&#41;&#41;;
				$this->message = $this->getOutputPostList&#40;$posts&#41;;
				return;
			&#125;
		&#125;
	&#125;
	
	function setMessageByPostAlias&#40;$input&#41; &#123;
		$gets = explode&#40;',', $input&#41;;
		$year = isset&#40;$gets&#91;0&#93;&#41;? intval&#40;$gets&#91;0&#93;&#41; &#58; 0;
		$month = isset&#40;$gets&#91;1&#93;&#41;? intval&#40;$gets&#91;1&#93;&#41; &#58; 0;
		$day = isset&#40;$gets&#91;2&#93;&#41;? intval&#40;$gets&#91;2&#93;&#41; &#58; 0;
		$alias = isset&#40;$gets&#91;3&#93;&#41;? $gets&#91;3&#93; &#58; '';
		$sec = isset&#40;$gets&#91;4&#93;&#41;? intval&#40;$gets&#91;4&#93;&#41; &#58; 0;
		$posts = $this->getPostByAlias&#40;$year, $month, $day, $alias, $sec&#41;;
		if &#40;$posts!==false&#41; &#123;
			$this->message .= $this->getOutputPost&#40;$posts, $this->tplpath . 'view_post.tpl', $this->tplpath . 'comments.tpl', $this->tplpath . 'comment_form.tpl', $this->dir&#41;;
		&#125;
	&#125;
	
	function setMessageByPostedComment&#40;$postid, $name, $website, $message&#41; &#123;
		$post_name = trim&#40;$name&#41;;
		$post_website = trim&#40;$website&#41;;
		$post_message = trim&#40;$message&#41;;
		if &#40;empty&#40;$post_name&#41; || empty&#40;$post_message&#41; || strpos&#40;$post_message, '</comment>'&#41;!==false || strpos&#40;$post_message, '<comment>'&#41;!==false&#41; &#123;
			$this->message = $this->notes&#91;'notice'&#93;&#91;'invalid'&#93;;
		&#125; else if &#40;$this->checkBlackList&#40;$_SERVER&#91;'REMOTE_ADDR'&#93;&#41;&#41; &#123;
			if &#40;$this->checkFlood&#40;$_SERVER&#91;'REMOTE_ADDR'&#93;&#41;&#41; &#123;
				$this->addComment&#40;$postid, $post_name, $post_website, $post_message&#41;;
				$this->message = $this->notes&#91;'notice'&#93;&#91;'confirm'&#93;;
			&#125; else &#123;
				$this->message = $this->notes&#91;'notice'&#93;&#91;'flood'&#93;;
			&#125;
		&#125; else &#123;
			$this->message = $this->notes&#91;'notice'&#93;&#91;'block'&#93;;
		&#125;
	&#125;
&#125;	

function show_news&#40;$max=-1, $cats=array&#40;&#41;&#41; &#123;
	global $news;
	if &#40;isset&#40;$_GET&#91;'post'&#93;&#41; || isset&#40;$_GET&#91;'archive'&#93;&#41; || isset&#40;$_GET&#91;'category'&#93;&#41; || isset&#40;$_GET&#91;'cat_alias'&#93;&#41; || isset&#40;$_GET&#91;'post_alias'&#93;&#41; || $max==-1&#41; &#123;
		print $news->message;
	&#125; else &#123;
		print $news->getNews&#40;$max, $cats&#41;;
	&#125;
&#125;



function show_archives&#40;&#41; &#123;
	global $news;
	print $news->getArchives&#40;&#41;;
&#125;

function show_categories&#40;&#41; &#123;
	global $news;
	print $news->getCategories&#40;&#41;;
&#125;

$news = new NewsDisplay&#40;&#41;;

if &#40;isset&#40;$_POST&#91;'do'&#93;&#41;&#41; &#123;
	if &#40;$_POST&#91;'do'&#93;=='comment'&#41; &#123;
		if &#40;!&#40;isset&#40;$_POST&#91;'id'&#93;&#41; && isset&#40;$_POST&#91;'name'&#93;&#41; && isset&#40;$_POST&#91;'website'&#93;&#41; && isset&#40;$_POST&#91;'message'&#93;&#41; && $news->commentsAllowed&#40;$_POST&#91;'id'&#93;&#41;&#41;&#41; &#123;
			exit&#40;'error3'&#41;;
		&#125;
		$news->setMessageByPostedComment&#40;$_POST&#91;'id'&#93;, $_POST&#91;'name'&#93;, $_POST&#91;'website'&#93;, $_POST&#91;'message'&#93;&#41;;
	&#125;
&#125;

if &#40;isset&#40;$_GET&#91;'post'&#93;&#41;&#41; &#123;
	$news->setMessageById&#40;$_GET&#91;'post'&#93;&#41;;
&#125; else if &#40;isset&#40;$_GET&#91;'archive'&#93;&#41;&#41; &#123;
	$news->setMessageByArchive&#40;$_GET&#91;'archive'&#93;&#41;;
&#125; else if &#40;isset&#40;$_GET&#91;'category'&#93;&#41;&#41; &#123;
	$news->message = $news->getOutputPostList&#40;$news->getPostList&#40;0, array&#40;$_GET&#91;'category'&#93;&#41;&#41;&#41;;
&#125; else if &#40;isset&#40;$_GET&#91;'cat_alias'&#93;&#41;&#41; &#123;
	$news->setMessageByCatAlias&#40;$_GET&#91;'cat_alias'&#93;&#41;;
&#125; else if &#40;isset&#40;$_GET&#91;'post_alias'&#93;&#41;&#41; &#123;
	$news->setMessageByPostAlias&#40;$_GET&#91;'post_alias'&#93;&#41;;
&#125;
?>

целия код.

Грешката която ми дава е

Cannot redeclare class NewsDisplay in D:\xampp\htdocs\news.php оn line 259
А на линия 259 има само едно }, което ако махна обаче ми дава 854435843 грешки. Мерси предварително!
Last edited by newfag on Fri Nov 30, 2012 3:05 pm, edited 1 time in total.

FFDP
Нов
Нов
Posts: 19
Joined: Wed Oct 03, 2012 9:40 am

Post by FFDP » Fri Nov 30, 2012 3:05 pm

include-a се пише само 1 път преди show_news(N);

T.e. може да извикаш show_news() 50 пъти, но include "news.php", трябва само веднъж да го има.

newfag
Нов
Нов
Posts: 21
Joined: Wed Mar 28, 2012 9:17 am

Post by newfag » Fri Nov 30, 2012 3:25 pm

FFDP wrote:include-a се пише само 1 път преди show_news(N);

T.e. може да извикаш show_news() 50 пъти, но include "news.php", трябва само веднъж да го има.
Ох да, бях забравил, стана!

Благодаря ти!

Post Reply