SHARE
TWEET

pictures from national geographic

a guest Jan 18th, 2012 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //dab??jam no dropdown boxa xml faila linku. @ vajadz??gs, lai k????das pazi??ojumu nemet lauk??, jo pirmo reizi atverot, protams, ka
  3. //'name'neb??s defin??ts
  4. $xml_file=@$_POST['name'];
  5.  
  6. //skatamies, vai pirmo reizi lapa atv??rta.
  7. if (!isset($xml_file)) {$xml_file="/your-shot/dailydozen/feed";}
  8. //nor??dam lapu, no kuras tiks ieg??ta bil??u liste priek?? dropdown boxa
  9. $xmlLapa="http://ngm.nationalgeographic.com/your-shot/daily-dozen/";
  10. //nor??dam xml faila pilno atra??an??s vietu
  11. $xml_file = "http://ngm.nationalgeographic.com".$xml_file;
  12. //varb??t k??dam noder...
  13. echo "<br>".$xml_file."<br><br>";
  14.  
  15. //sagrie??am un dab??jam no lapas ??r?? tie??i to, ko mums vajaga - options sada??u.
  16. $xmlLapa = file_get_contents($xmlLapa) or die("Could not read file");
  17. $garums = strlen($xmlLapa);
  18. $saakums = strpos($xmlLapa, "<option");
  19. $beigas = strpos($xmlLapa, "</select");
  20. $options = substr($xmlLapa, $saakums, $beigas-$garums);
  21.  
  22. //nos??cam failu
  23.  
  24. //ats??cam vajadz??gos apskat??mos tegus - image, caption un yscredit
  25. $xml_photo_key = "*PHOTOGALLERY*PHOTO*IMAGE";
  26. $xml_caption_key = "*PHOTOGALLERY*PHOTO*CAPTION";
  27. $xml_author_key = "*PHOTOGALLERY*PHOTO*YSCREDIT";
  28. //defin??jam ieraksta mas??vu
  29. $story_array = array();
  30. //defin??jam skait??kli, kas skait??s ierakstus
  31. $counter = 0;
  32.  
  33. //izveidojam klasi par ierakstu, kur?? main??gie ir att??ls(image), apraksts(caption) un autors
  34. class xml_story{
  35.     var $image, $caption, $author;
  36. }
  37. //klas?? ir tr??s funkcijas.
  38. //pirm?? funkcija - starta tega atra??ana
  39. function startTag($parser, $data){
  40.     global $current_tag;
  41.     $current_tag .= "*$data";
  42. }
  43. //otr?? funkcija - beigu tega atra??ana
  44. function endTag($parser, $data){
  45.     global $current_tag;
  46.     $tag_key = strrpos($current_tag, '*');
  47.     $current_tag = substr($current_tag, 0, $tag_key);
  48. }
  49. //tre???? funkcija - satura atra??ana.
  50. function contents($parser, $data){
  51.     global $current_tag, $xml_photo_key, $xml_caption_key, $xml_author_key, $counter, $story_array;
  52.     switch($current_tag){
  53.  
  54.         case $xml_photo_key:
  55.             $story_array[$counter] = new xml_story();
  56.             $story_array[$counter]->image = $data;
  57.             break;
  58.  
  59.                 case $xml_author_key:
  60.                         $story_array[$counter]->author=$data;
  61.                         break; 
  62.  
  63.                 case $xml_caption_key:
  64.             $story_array[$counter]->caption = $data;
  65.                         $counter++;
  66.            break;
  67.  
  68.                 }
  69. }
  70.  
  71.  
  72. //uztaisam xml parseri
  73. $xml_parser = xml_parser_create();
  74. //palai??am parseri ???
  75. xml_set_element_handler($xml_parser, "startTag", "endTag");
  76.  
  77. xml_set_character_data_handler($xml_parser, "contents");
  78.  
  79.  
  80. $fp = fopen($xml_file, "r") or die("Could not open file");
  81. //it k?? var??tu iztikt bez file_get_contents un izmantot "fread", bet ??oreiz es pa????mu file_get_contents. $data k?? viens gar?? strings tagad.
  82. $data = file_get_contents($xml_file) or die("Could not read file");
  83.  
  84.  
  85. if(!(xml_parse($xml_parser, $data, feof($fp)))){
  86.     die("Error on line " . xml_get_current_line_number($xml_parser));
  87. }
  88.  
  89.  
  90. xml_parser_free($xml_parser);
  91.  
  92. //fclose($fp);
  93.  
  94. ?>
  95.  
  96. <html>
  97. <head>
  98. <title>National Geographic NGN Daily Dozen pictures</title>
  99.  
  100. <style type="text/css">
  101.  
  102.  
  103.   #menu {
  104.     position: fixed;
  105.         _position:absolute;
  106.         _top:expression(eval(document.body.scrollTop));
  107.         left:800px;
  108.         top:0px;
  109.     background-color: #d8da3d }
  110.        
  111.   #content {
  112.   font-family:verdana;
  113.  
  114.   font-size:11px;
  115.   background-color: #fcfcfc
  116.   top:0;
  117.   right:0;
  118.   width:60%;
  119.   }
  120.  </style>
  121.  
  122. </head>
  123. <body bgcolor="#FFFFFF" >
  124.  
  125.  <div id="menu">
  126.  <hr \>
  127. <form action="nga.php" method="post" name="formasName">
  128. <?php
  129. //select teg?? iek???? javaskripts - uz izmai??u konkr??to formu izpild??t Submit komandu.
  130. ?>
  131. <select name="name" onChange="document.forms['formasName'].submit()">
  132. <?php
  133. echo $options;
  134. ?>
  135. </select>
  136. </form>
  137. <hr \>
  138. </div>
  139.  
  140.  
  141.  <div id="content">
  142. <?php
  143. //datu izvad????ana no izveidot?? mas??va.
  144.  
  145.  
  146. for($x=0;$x<count($story_array);$x++){
  147.     echo "\t<h2><img src=\"http://ngm.nationalgeographic.com" . $story_array[$x]->image . "\"></h2>\n";
  148.     echo "\t\t\n";
  149.     echo "\t" . $story_array[$x]->caption . "<br>\n";
  150.         echo "\t<i>" . $story_array[$x] -> author ."</i>\n";
  151.         echo "<hr><br>";
  152. }
  153.  
  154.  
  155. //s??k??ks klases koda apraksts ??eitan - http://www.kirupa.com/web/xml_php_parse_intermediate.htm
  156. ?>
  157.  </div>
  158. </body>
  159. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top