PatakSudbine

zadnjipatak

Jul 18th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.97 KB | None | 0 0
  1. <?php
  2. // index.php
  3.  
  4. include 'VozniRed.php';
  5. $sviVozniRedovi = VozniRed::getInstance('BusLines.csv');
  6. echo $sviVozniRedovi->showPolasci();
  7. ?>
  8.  
  9.  
  10. <?php
  11. // VozniRed.php
  12.  
  13. class VozniRed{
  14.     var $file_array;
  15.     private static $instance;
  16.  
  17.     public static function getInstance($filename){
  18.         if(!isset(self::$instance)){
  19.             self::$instance = new self($filename);
  20.         }
  21.         return self::$instance;
  22.     }
  23.  
  24.     private function __construct($filename){
  25.         $this->file_array=file($filename);
  26.         foreach($this->file_array as $key=>$line){
  27.             $line = trim($line, "\n");
  28.             $line = trim($line, "\r");
  29.             $line = explode(";", $line);
  30.             $this->file_array[$key]= $line;
  31.         }
  32.             /*
  33.             echo '<pre>';
  34.             print_r($this->file_array);
  35.             echo '</pre>';
  36.             */
  37.         $this->vozniRedovi();  
  38.     }
  39.  
  40.     function vozniRedovi(){
  41.         unset($this->file_array[0]);
  42.         $vozniRedoviKljuc=[];
  43.  
  44.             foreach($this->file_array as $key=>$value){
  45.                 $vozniRedoviKljuc[$value[1]][]=$value[2];
  46.             }
  47.                 $this->file_array=$vozniRedoviKljuc;
  48.                 ksort($this->file_array);
  49.             foreach($this->file_array as $key=>$value){
  50.                 sort($this->file_array[$key]);
  51.             }
  52.     }
  53.  
  54.     function showPolasci(){
  55.         $prikaz="";
  56.             foreach($this->file_array as $key=>$value){
  57.                 $prikaz.='<h1>'.$key.'</h1><div style="margin-top:-20px;">';
  58.                     foreach($value as $k=>$v){
  59.                         if(array_key_exists($k+1,$value)){
  60.                             $prikaz.=$v.', ';
  61.                         }else{
  62.                             $prikaz.=$v;
  63.                         }
  64.                     }
  65.                 $prikaz.='</div>';
  66.             }
  67.     /*
  68.     echo '<pre>';
  69.     print_r($this->file_array);
  70.     echo '</pre>';
  71.     */
  72.         return $prikaz;
  73.     }
  74. }
  75.  
  76. ?>
  77.  
  78.  
  79. GradOd;GradDo;VrijemePolaska
  80. Zagreb;Split;17:00
  81. Zagreb;Zadar;14:00
  82. Zagreb;Rijeka;11:00
  83. Zagreb;Zadar;12:30
  84. Zagreb;Dubrovnik;08:00
  85. Zagreb;Osijek;10:00
  86. Zagreb;Pula;07:30
  87. Zagreb;Makarska;15:00
  88. Zagreb;Šibenik;09:30
  89. Zagreb;Bjelovar;18:00
  90. Zagreb;Split;11:00
  91. Zagreb;Rovinj;18:00
  92. Zagreb;Makarska;06:00
  93. Zagreb;Pula;17:30
  94. Zagreb;Rovinj;08:00
Advertisement
Add Comment
Please, Sign In to add comment