Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [class] Timeline
Forum PHP.pl > Inne > Oceny
Athlan
Chciałbym zaprezentować kod umożliwiający stworzenie timeline, czyli paska czasu. Pozwala on na stworzenie listy eventów: dziś, jutro, po jutrze, za X dni, za tydzień, itp.

Klasa:

  1. <?php
  2.  
  3. class TimeLine
  4. {
  5. protected $aTimeRank = array();
  6. protected $aElements = array();
  7.  
  8. public function __construct(array $aDaySteps)
  9. {
  10. foreach($aDaySteps as $iStep)
  11. $this->aTimeRank[$iTime = $this->time($iStep, true)] = $iTime;
  12. }
  13.  
  14. public function push($iTime, array $aRow)
  15. {
  16. $iTime = $this->time($iTime);
  17.  
  18. if(!isset($this->aTimeRank[$iTime]))
  19. $iTime = 0;
  20.  
  21. if(!is_array($this->aElements[$iTime]))
  22. $this->aElements[$iTime] = array();
  23.  
  24. $this->aElements[$iTime][] = $aRow;
  25. }
  26.  
  27. public function render()
  28. {
  29. $aResult = array();
  30.  
  31. foreach($this->aTimeRank as $iTime)
  32. if($aItems = (is_array($this->aElements[$iTime])) ? $this->aElements[$iTime] : false)
  33. $aResult[$iTime] = $aItems;
  34.  
  35. if(isset($this->aElements[0]))
  36. $aResult[0] = $this->aElements[0];
  37.  
  38. return $aResult;
  39. }
  40.  
  41. public function time($iTime, $bAddDays = false)
  42. {
  43. if($bAddDays)
  44. return mktime(0, 0, 0, date('m'), date('d') + $iTime, date('Y'));
  45.  
  46. return mktime(0, 0, 0, date('m', $iTime), date('d', $iTime), date('Y', $iTime));
  47. }
  48. }
  49.  
  50. ?>


Konstruktor klasy otrzymuje tablicę z barierami dni co ile ma tworzyć nową grupę eventów. 0 - dzisiaj, 1 - jutro, 2 - po jutrze, 15 - za 15 dni... -4 - 4 dni temu itp.

Użycie, oczywiście tylko przykład:

  1. <?php
  2.  
  3. require_once 'TimeLine.Class.php';
  4.  
  5. $oTimeLine = new TimeLine(array(0,1,2,3,7));
  6.  
  7. mysql_connect('localhost', 'root', '');
  8.  
  9. $rItems = mysql_query('SELECT * FROM dates ORDER BY time DESC');
  10. while($aRow = mysql_fetch_array($rItems))
  11. $oTimeLine->push(strtotime($aRow['time']), $aRow);
  12.  
  13. foreach($oTimeLine->render() as $iTime => $aItems)
  14. {
  15. if($iTime == $iCurr = $oTimeLine->time(0, true))
  16. echo 'Dzisiaj: ' . date("d.m.Y", $iCurr);
  17. elseif($iTime == $iCurr = $oTimeLine->time(1, true))
  18. echo 'Jutro: ' . date("d.m.Y", $iCurr);
  19. elseif($iTime == $iCurr = $oTimeLine->time(2, true))
  20. echo 'Po jutrze: ' . date("d.m.Y", $iCurr);
  21. elseif($iTime <= $oTimeLine->time(7, true))
  22. echo 'W ciągu 7 dni: ';
  23. elseif($iTime <= $oTimeLine->time(14, true))
  24. echo 'W ciągu 14 dni: ';
  25. else
  26. echo 'Pozostałe:';
  27.  
  28. echo '<ul>';
  29.  
  30. foreach($aItems as $iKey => $aRow)
  31. echo '<li>'.$aRow['item'].'</li>';
  32.  
  33. echo '</ul>';
  34. }
  35.  
  36. ?>
Moli
Mógłbyś pokazać przykład działania on-line ? Jakoś nie mogę wymyśleć, do czego była by pomoca klasa smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.