<?PHP
class NewsEdit
{
var $news_array = array();
var $parsed_array=array();
var $filename;
function NewsEdit($file)
{
$ini_array=array();
$file_text=file_get_contents($file);
$ini_array=explode('[start_post]',$file_text);
if($ini_array[0]=='[start_post]')
{
array_shift($ini_array);
}
$this->news_array = $ini_array;
$this->filename=$file;
}
function add_post($add_arr='00/00/00| | | ')
{
array_unshift($this->news_array,$add_arr);
$this->write_news($this->filename);
}
function show_post($num_posts=1)
{
$tmp_array=array();
$num_posts=(($num_posts > 0) && $num_posts <= ((count($this->news_array) -1))) ? $num_posts : 1;
$i=0;
foreach ($this->news_array as $value)
{
if($i == $num_posts)
{
break;
}
if($value)
{
array_push($tmp_array,$value);
$i++;
}
}
return $tmp_array;
}
function get_array()
{
return $this->news_array;
}
function delete_post($post_id=1)
{
$value=array();
$tmp_array=array();
$post_id=((($post_id <= count($this->news_array)-1) ) && ($post_id > 0)) ? $post_id : 1;
$this->news_array[$post_id]='DELETE';
foreach ($this->news_array as $value)
{
if($value != 'DELETE')
{
array_push($tmp_array,$value);
}
}
$this->news_array=$tmp_array;
$this->parsed_array=$this->news_array;
$this->write_news($this->filename);
}
function array_list_num()
{
return count($this->news_array);
}
function write_news($file)
{
$filehandle = fopen($file, "w");
foreach($this->news_array as $val)
{
if ($val != '' && $val!='[start_post]')
{
fwrite($filehandle, '[start_post]' . $val);
}
}
fclose($filehandle);
}
function edit_post($edit_arr='00/00/00| | | ',$post_id=1)
{
$value=array();
$tmp_array=array();
$post_id=((($post_id <= count($this->news_array)-1) ) && ($post_id > 0)) ? $post_id : 1;
$this->news_array[$post_id]=$edit_arr;
foreach ($this->news_array as $value)
{
if($value != 'DELETE')
{
array_push($tmp_array,$value);
}
}
$this->news_array=$tmp_array;
$this->parsed_array=$this->news_array;
$this->write_news($this->filename);
}
function sel_entry($search=1)
{
$value=array();
$this->parsed_array=$this->news_array;
$search=((($search <= count($this->parsed_array)-1) ) && ($search > 0)) ? $search : 1;
$value=$this->parsed_array[$search];
$retun_arr=explode('|',$value);
return $retun_arr;
}
}
?>