Scripts

newsflash

Dit script is ervoor gemaakt om met behulp van XML een newsflash te maken waarbij je de look zelf kunt aanpassen in de css file. Volgens mij kan het script nog wat hier en daar geoptimaliseerd worden. Voor tips graag reageren. Voorbeeld:

newsflash
clsnewsflash.php: 

[code]
<?php 
error_reporting(E_ALL);   //just to be sure

define("READ",1);         //shows the newsflash
define("EDIT",2);         //removes flashes
define("ADD",3);          //adds a newsflash

class newsflash
{

  private $obj_xml;       //the object for editing the XML-code
  private $obj_xml_read;  //the object for reading the XML-code
  private $wtd;           //=What to do 
  private $news_title;    //an array with the titles
  private $news_text;     //an array with the text
  private $news_link;     //an array with the links
  private $xml_file;      //the file containing the XML
  
  function show($wtd,$file) 
  //prints the newsflash (wtd = READ/EDIT/ADD,filename=xml-file)
  {
    if ($wtd==READ) $this->wtd=READ;      //set the 'what to do'
    else if ($wtd==EDIT) $this->wtd=EDIT;
    else if ($wtd==ADD) $this->wtd=ADD;
    else trigger_error("Wrong parameter command.");

     $this->xml_file = $file;
     if($this->wtd==EDIT || $this->wtd==ADD){             //if removing or adding
     $this->obj_xml = new DOMDocument();
     $this->obj_xml->load($this->xml_file);               //open the file
     }
     elseif($this->wtd==READ) {
     $this->obj_xml_read=simplexml_load_file($this->xml_file);      //for reading

   if ($this->wtd==READ){                          //if reading
    foreach($this->obj_xml_read->article as $num)  //for each article this loop
    {                                              //print the elements
      echo "
      <span>
      <a href= \"". $num->link ."\">
      <b>". $num->title ."</b><br />";
      echo  $num->text ."</a><br /><br />
      </span>";
    }
  } //end if READ
   elseif ($this->wtd==ADD){                      //if adding
     
     
     if (!(isset($_POST['Add']))){                //if the button isn't clicked
       $form="<form method=\"POST\"><table><table><tr><td>Title:</td>
<td><input type=\"text\" name=\"title\" size=\"40\"</td></tr><tr><td>Text:</td>
<td><input type=\"text\" name=\"text\" size=\"40\"></td></tr><tr><td>link:</td>
<td><input type=\"text\" name=\"link\" size=\"40\"></td></tr><tr><td>
<input type=\"submit\" value=\"Add\" name=\"Add\"></td></tr></table></form>"; 
       echo $form ;
     }else{
     
       $title=trim(addslashes(($_POST['title'])));  //Get the superglobals
       $text=trim(addslashes(($_POST['text'])));
       $link=trim(addslashes(($_POST['link'])));
       
       $root=$this->obj_xml->documentElement;       //the <articles>

       foreach($root->getElementsByTagName("article") as $now)
       {                     //check if the title already exists 
         foreach($now->getElementsByTagName("title") as $node)
         {
           if ($title == $node->textContent)
           die("Title already exsists.<br />".
           "<a href=\"". $_SERVER["SCRIPT_NAME"] ."\">Go back</a>"); //yes? die!!!
         }
       }
       
       $root2nd=$this->obj_xml->createElement("article");  //creates the article tag
       
       $xml_title=$this->obj_xml->createElement("title");
       $xml_title->appendChild($this->obj_xml->createTextNode($title));
       $root2nd->appendChild($xml_title);  //create and add the title tag
      
       $xml_text=$this->obj_xml->createElement("text");
       $xml_text->appendChild($this->obj_xml->createTextNode($text));
       $root2nd->appendChild($xml_text);  //create and add the text tag
       
       $xml_link=$this->obj_xml->createElement("link");
       $xml_link->appendChild($this->obj_xml->createTextNode($link));
       $root2nd->appendChild($xml_link);  //create and add the link tag
       
       $root->appendChild($root2nd);
       $this->obj_xml->save($this->xml_file);//save it to the file
       
       echo"Add new?<br /><a href=\"" . $_SERVER["SCRIPT_NAME"] ."\">Click here!</a>";
     } 
     } 
   elseif ($this->wtd==EDIT){     //if editing
   if (!(isset($_POST['Edit']))){ //if the button was pressed
   
     echo"Remove:<br />
     <form method=\"POST\" action=\"". $_SERVER["SCRIPT_NAME"] ."\">";//start the form
     $root=$this->obj_xml->documentElement;
     foreach($root->getElementsByTagName("article") as $num1) //get all articles
     {
       foreach ($num1->getElementsByTagName("title") as $num2) //get the title of the article 
       { 
         echo "
         <input type=\"radio\" value=\"". $num2->textContent ."\" name=\"R\">
         ". $num2->textContent ."<br />";                   //and echo a button+text 
       }
     }
     echo"<input type=\"submit\" value=\"Remove\" name=\"Edit\">";//end the form
   }else{
     $root=$this->obj_xml->documentElement;
     
     foreach($root->getElementsByTagName("article") as $now) //get the articles
     {
       foreach($now->getElementsByTagName("title") as $title) //get the title
       {
         if ($title->textContent == $_POST['R'])              //if the title matches
         {
           $root->removeChild($now);                          //and remove the title
         }
       }
     }
     $this->obj_xml->save($this->xml_file); //save the xml to a file
     echo"Remove another one?<br /><a href=\"" . $_SERVER["SCRIPT_NAME"]."\">Click here!</a>";
   }
 }
}
}}

?>
[/code]

newsflash.css: 

[code]
#newsflash { 
  position: relative; 
  overflow: hidden; 
  width:200; 
  height:100; 
  border-color:#000000; 
  border-width:2px; 
} 
span{ 
  position:absolute; 
  color:#000000; 
  top:100px; 
  height:100; 
  width:200; 
  background-color:#FFFFFF; 
} 
[/code]

newsflash.js: 

[code]
var x=100; 
var x2=5; 
var numb=0; 
var now=0; 
var nodes= new Array(); 
var firsttime=false; 
var handeld; 
var handling; 

function start() 
{ 
  nodes=document.getElementsByTagName("span"); 
  nex = window.setTimeout("nextstep();",10); 
} 
function nextstep() 
{ 
    handling=nodes[now]; 
    x-=0.5; 
    x2-=0.5; 
    handling.style.top=x; 
    if (firsttime) 
    handeld.style.top=x2; 
    if (x<5)  nex = window.setTimeout("nextnode();",3000); 
    else if (x<30)  nex = window.setTimeout("nextstep();",25); 
    else nex = window.setTimeout("nextstep();",10); 
} 
function nextnode() 
{ 
    handeld=handling; 
    if (firsttime) 
    handeld.style.top=100; 
    x2=5; 
    x=100; 
    now++; 
    firsttime=true; 
    if (now==nodes.length) now=0; 
    nextstep(); 
} 
window.onload=start; 
[/code]

newsflash.xml: 

[code]
<?xml version="1.0" encoding="iso-8859-1"?> 
<articles> 
  <article> 
    <title>Leejoo</title> 
    <text>Leejoo.nl is een site met scripts...</text> 
    <link>http://leejoo.nl</link> 
  </article> 
  <article> 
    <title>PhpFreakz.nl</title> 
    <text>PhpFreakz: Voor je scripts</text> 
    <link>http://www.phpfreakz.nl</link> 
  </article> 
  <article> 
    <title>PhpHulp.nl</title> 
    <text>Handig om php the leren. Op deze website heb ik het namelijk ook geleerd!</text> 
    <link>www.phphulp.nl</link> 
  </article> 
</articles> 
[/code]


newsflash.php: 

[code]
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<title>newsflash</title> 
<script language="javascript" type="text/javascript" src="newsflash.js"> 
</script> 
<link rel="stylesheet" href="newsflash.css" type="text/css" /> 
</head> 
<div id="newsflash"> 
<?php  
include_once("clsnewsflash.php"); 
$newsflash=new newsflash();
$newsflash->show(READ,"newsflash.xml"); 
?> 
</div> 
<body> 
</body> 
</html> 
[/code]


add.php: 

[code]
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<title>Add a newsflash</title> 
<script language="javascript" type="text/javascript" src="newsflash.js"> 
</script> 
<link rel="stylesheet" href="newsflash.css" type="text/css" /> 
</head> 
<div id="newsflash"> 
<?php 
include_once("clsnewsflash.php"); 
$newsflash=new newsflash();
$newsflash->show(ADD,"newsflash.xml"); 
?> 
</div> 
<?php 
$newsflash->show(READ,"newsflash.xml"); 
?> 
<body> 
</body> 
</html> 
[/code]

remove.php: 

[code]
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<title>Remove a newsflash</title> 
<script language="javascript" type="text/javascript" src="newsflash.js"> 
</script> 
<link rel="stylesheet" href="newsflash.css" type="text/css" /> 
</head> 
<div id="newsflash"> 
<?php 
include_once("clsnewsflash.php"); 
$newsflash=new newsflash();
$newsflash->show(EDIT,"newsflash.xml"); 
?> 
</div> 
<?php 
$newsflash->show(READ,"newsflash.xml"); 
?> 
<body> 
</body> 
</html> 
[/code]

Reacties

0
Nog geen reacties.