Advertisement
Guest User

Table code for JSON

a guest
Apr 4th, 2011
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.84 KB | None | 0 0
  1.                 <!-- column headers -->
  2.                 <thead>
  3.                     <tr>
  4.                         <th class="hide">Units</th>
  5.                         <th>Hours</th>
  6.                         <th>Monday</th>
  7.                         <th>Tuesday</th>
  8.                         <th>Wednesday</th>
  9.                         <th>Thursday</th>
  10.                         <th>Friday</th>
  11.                         <th>Saturday</th>
  12.                         <th>Sunday</th>
  13.                     </tr>
  14.                 </thead>
  15.                 <tbody>
  16.                                    
  17.                     <tr id="units-0">
  18.                         <td class="hide">0</td>
  19.                         <!-- Every other row echo an hour block -->
  20.                         <td class="time" rowspan="2">00:00</td>
  21.                         <!-- start polling days -->
  22.                        
  23.                         <!-- finish echo a row for each of the 48 units -->
  24.                     </tr>
  25.                     <tr id="units-1">
  26.                         <td class="hide">1</td>
  27.                         <!-- Every other row echo an hour block -->
  28.                     </tr>
  29.                    
  30.                     <!-- echo a row for each of the 48 units -->
  31.                     <tr id="units-2">
  32.                         <td class="hide">2</td>
  33.                         <!-- Every other row echo an hour block -->
  34.                         <td class="time" rowspan="2">01:00</td>
  35.                         <!-- start polling days -->
  36.                        
  37.                         <!-- finish echo a row for each of the 48 units -->
  38.                     </tr>
  39.                    
  40.                     <tr id="units-3">
  41.                         <td class="hide">3</td>
  42.                         <!-- start polling days -->
  43.                         <!-- finish echo a row for each of the 48 units -->                
  44.                     </tr>
  45.                    
  46.                     <tr id="units-4">
  47.                         <td class="hide">4</td>
  48.                         <!-- Every other row echo an hour block -->
  49.                         <td class="time" rowspan="2">02:00</td>
  50.                     </tr>
  51.            
  52.                     <tr id="units-5">
  53.                         <td class="hide">5</td>
  54.                         <!-- start polling days -->
  55.                        
  56.                         <!-- finish echo a row for each of the 48 units -->
  57.                     </tr>
  58.                    
  59.                     <tr id="units-6">
  60.                         <td class="hide">6</td>
  61.                         <!-- Every other row echo an hour block -->
  62.                         <td class="time" rowspan="2">03:00</td>
  63.                         <!-- start polling days -->
  64.                         <!-- finish echo a row for each of the 48 units -->
  65.                     </tr>
  66.                    
  67.                     <tr id="units-7">
  68.                         <td class="hide">7</td>
  69.                         <!-- start polling days -->
  70.                         <!-- finish echo a row for each of the 48 units -->
  71.                     </tr>
  72.                    
  73.                     <tr id="units-8">
  74.                         <td class="hide">8</td>
  75.                         <!-- Every other row echo an hour block -->
  76.                         <td class="time" rowspan="2">04:00</td>
  77.                         <!-- start polling days -->
  78.                         <!-- finish echo a row for each of the 48 units -->
  79.                     </tr>
  80.            
  81.                     <tr id="units-9">
  82.                         <td class="hide">9</td>
  83.                         <!-- start polling days -->
  84.                         <!-- finish echo a row for each of the 48 units -->
  85.                     </tr>
  86.                    
  87.                     <tr id="units-10">
  88.                         <td class="hide">10</td>
  89.                         <!-- Every other row echo an hour block -->
  90.                         <td class="time" rowspan="2">05:00</td>
  91.                         <!-- start polling days -->
  92.                         <!-- finish echo a row for each of the 48 units -->
  93.                     </tr>
  94.                    
  95.                     <tr id="units-11">
  96.                         <td class="hide">11</td>
  97.                         <!-- start polling days -->
  98.                         <!-- finish echo a row for each of the 48 units -->
  99.                     </tr>
  100.                            
  101.                     <tr id="units-12">
  102.                         <td class="hide">12</td>
  103.                         <!-- Every other row echo an hour block -->
  104.                         <td class="time" rowspan="2">06:00</td>
  105.                         <!-- start polling days -->
  106.                         <!-- finish echo a row for each of the 48 units -->
  107.                     </tr>
  108.            
  109.                     <tr id="units-13">
  110.                         <td class="hide">13</td>
  111.                         <!-- start polling days -->
  112.                         <!-- finish echo a row for each of the 48 units -->
  113.                     </tr>
  114.                    
  115.                     <tr id="units-14">
  116.                         <td class="hide">14</td>
  117.                         <!-- Every other row echo an hour block -->
  118.                         <td class="time" rowspan="2">07:00</td>
  119.                         <!-- start polling days -->
  120.                     <!-- finish echo a row for each of the 48 units -->
  121.                     </tr>
  122.                    
  123.                     <tr id="units-15">
  124.                         <td class="hide">15</td>
  125.                         <!-- start polling days -->
  126.                         <!-- finish echo a row for each of the 48 units -->
  127.                     </tr>
  128.                    
  129.                     <tr id="units-16">
  130.                         <td class="hide">16</td>
  131.                         <!-- Every other row echo an hour block -->
  132.                         <td class="time" rowspan="2">08:00</td>
  133.                         <!-- start polling days -->
  134.                         <!-- finish echo a row for each of the 48 units -->
  135.                     </tr>
  136.                    
  137.                     <tr id="units-17">
  138.                         <td class="hide">17</td>
  139.                         <!-- start polling days -->
  140.                         <!-- finish echo a row for each of the 48 units -->
  141.                     </tr>
  142.                    
  143.                     <tr id="units-18">
  144.                         <td class="hide">18</td>
  145.                         <!-- Every other row echo an hour block -->
  146.                         <td class="time" rowspan="2">09:00</td>
  147.                         <!-- start polling days -->
  148.                         <!-- finish echo a row for each of the 48 units -->
  149.                     </tr>
  150.            
  151.                     <tr id="units-19">
  152.                         <td class="hide">19</td>
  153.                         <!-- start polling days -->
  154.                         <!-- finish echo a row for each of the 48 units -->
  155.                     </tr>
  156.                    
  157.                     <tr id="units-20">
  158.                         <td class="hide">20</td>
  159.                         <!-- Every other row echo an hour block -->
  160.                         <td class="time" rowspan="2">10:00</td>
  161.                         <!-- start polling days -->
  162.                         <!-- finish echo a row for each of the 48 units -->
  163.                     </tr>
  164.                    
  165.                     <tr id="units-21">
  166.                         <td class="hide">21</td>
  167.                         <!-- start polling days -->
  168.                         <!-- finish echo a row for each of the 48 units -->
  169.                     </tr>
  170.                    
  171.                     <tr id="units-22">
  172.                         <td class="hide">22</td>
  173.                         <!-- Every other row echo an hour block -->
  174.                         <td class="time" rowspan="2">11:00</td>
  175.                         <!-- start polling days -->
  176.                         <!-- finish echo a row for each of the 48 units -->
  177.                     </tr>
  178.                    
  179.                     <tr id="units-23">
  180.                         <td class="hide">23</td>
  181.                         <!-- start polling days -->
  182.                         <!-- finish echo a row for each of the 48 units -->
  183.                     </tr>
  184.                    
  185.                     <tr id="units-24">
  186.                         <td class="hide">24</td>
  187.                         <!-- Every other row echo an hour block -->
  188.                         <td class="time" rowspan="2">12:00</td>
  189.                         <!-- start polling days -->
  190.                         <!-- finish echo a row for each of the 48 units -->
  191.                     </tr>
  192.            
  193.                     <tr id="units-25">
  194.                         <td class="hide">25</td>
  195.                         <!-- start polling days -->
  196.                         <!-- finish echo a row for each of the 48 units -->
  197.                     </tr>
  198.                    
  199.                     <tr id="units-26">
  200.                         <td class="hide">26</td>
  201.                         <!-- Every other row echo an hour block -->
  202.                         <td class="time" rowspan="2">13:00</td>
  203.                         <!-- start polling days -->
  204.                         <!-- finish echo a row for each of the 48 units -->
  205.                     </tr>
  206.                    
  207.                     <tr id="units-27">
  208.                         <td class="hide">27</td>
  209.                         <!-- start polling days -->
  210.                         <!-- finish echo a row for each of the 48 units -->
  211.                     </tr>
  212.                    
  213.                     <tr id="units-28">
  214.                         <td class="hide">28</td>
  215.                         <!-- Every other row echo an hour block -->
  216.                         <td class="time" rowspan="2">14:00</td>
  217.                         <!-- start polling days -->
  218.                         <!-- finish echo a row for each of the 48 units -->
  219.                     </tr>
  220.                    
  221.                     <tr id="units-29">
  222.                         <td class="hide">29</td>
  223.                         <!-- start polling days -->
  224.                         <!-- finish echo a row for each of the 48 units -->
  225.                     </tr>
  226.                    
  227.                     <tr id="units-30">
  228.                         <td class="hide">30</td>
  229.                         <!-- Every other row echo an hour block -->
  230.                         <td class="time" rowspan="2">15:00</td>
  231.                         <!-- start polling days -->
  232.                         <!-- finish echo a row for each of the 48 units -->
  233.                     </tr>
  234.                    
  235.                     <tr id="units-31">
  236.                         <td class="hide">31</td>
  237.                         <!-- start polling days -->
  238.                         <!-- finish echo a row for each of the 48 units -->
  239.                     </tr>
  240.                    
  241.                     <tr id="units-32">
  242.                         <td class="hide">32</td>
  243.                         <!-- Every other row echo an hour block -->
  244.                         <td class="time" rowspan="2">16:00</td>
  245.                         <!-- start polling days -->
  246.                         <!-- finish echo a row for each of the 48 units -->
  247.                     </tr>
  248.                    
  249.                     <tr id="units-33">
  250.                         <td class="hide">33</td>
  251.                         <!-- start polling days -->
  252.                         <!-- finish echo a row for each of the 48 units -->
  253.                     </tr>
  254.                    
  255.                     <tr id="units-34">
  256.                         <td class="hide">34</td>
  257.                         <!-- Every other row echo an hour block -->
  258.                         <td class="time" rowspan="2">17:00</td>
  259.                         <!-- start polling days -->
  260.                         <!-- finish echo a row for each of the 48 units -->
  261.                     </tr>
  262.                    
  263.                     <tr id="units-35">
  264.                         <td class="hide">35</td>
  265.                         <!-- start polling days -->
  266.                         <!-- finish echo a row for each of the 48 units -->
  267.                     </tr>
  268.                    
  269.                     <tr id="units-36">
  270.                         <td class="hide">36</td>
  271.                         <!-- Every other row echo an hour block -->
  272.                         <td class="time" rowspan="2">18:00</td>
  273.                         <!-- start polling days -->
  274.                         <!-- finish echo a row for each of the 48 units -->
  275.                     </tr>
  276.                    
  277.                     <tr id="units-37">
  278.                         <td class="hide">37</td>
  279.                         <!-- start polling days -->
  280.                         <!-- finish echo a row for each of the 48 units -->
  281.                     </tr>
  282.                    
  283.                     <tr id="units-38">
  284.                         <td class="hide">38</td>
  285.                         <!-- Every other row echo an hour block -->
  286.                         <td class="time" rowspan="2">19:00</td>
  287.                         <!-- start polling days -->
  288.                         <!-- finish echo a row for each of the 48 units -->
  289.                     </tr>
  290.                    
  291.                     <tr id="units-39">
  292.                         <td class="hide">39</td>
  293.                         <!-- start polling days -->
  294.                         <!-- finish echo a row for each of the 48 units -->
  295.                     </tr>
  296.                    
  297.                     <tr id="units-40">
  298.                         <td class="hide">40</td>
  299.                         <!-- Every other row echo an hour block -->
  300.                         <td class="time" rowspan="2">20:00</td>
  301.                         <!-- start polling days -->
  302.                         <!-- finish echo a row for each of the 48 units -->
  303.                     </tr>
  304.                    
  305.                     <tr id="units-41">
  306.                         <td class="hide">41</td>
  307.                         <!-- start polling days -->
  308.                         <!-- finish echo a row for each of the 48 units -->
  309.                     </tr>
  310.                    
  311.                     <tr id="units-42">
  312.                         <td class="hide">42</td>
  313.                         <!-- Every other row echo an hour block -->
  314.                         <td class="time" rowspan="2">21:00</td>
  315.                         <!-- start polling days -->
  316.                         <!-- finish echo a row for each of the 48 units -->
  317.                     </tr>
  318.                    
  319.                     <tr id="units-43">
  320.                         <td class="hide">43</td>
  321.                         <!-- start polling days -->
  322.                         <!-- finish echo a row for each of the 48 units -->
  323.                     </tr>
  324.                    
  325.                     <tr id="units-44">
  326.                         <td class="hide">44</td>
  327.                         <!-- Every other row echo an hour block -->
  328.                         <td class="time" rowspan="2">22:00</td>
  329.                         <!-- start polling days -->
  330.                         <!-- finish echo a row for each of the 48 units -->
  331.                     </tr>
  332.                    
  333.                     <tr id="units-45">
  334.                         <td class="hide">45</td>
  335.                         <!-- start polling days -->
  336.                         <!-- finish echo a row for each of the 48 units -->
  337.                     </tr>
  338.                    
  339.                     <tr id="units-46">
  340.                         <td class="hide">46</td>
  341.                         <!-- Every other row echo an hour block -->
  342.                         <td class="time" rowspan="2">23:00</td>
  343.                         <!-- start polling days -->
  344.                         <!-- finish echo a row for each of the 48 units -->
  345.                     </tr>
  346.                    
  347.                     <tr id="units-47">
  348.                         <td class="hide">47</td>
  349.                         <!-- start polling days -->
  350.                         <!-- finish echo a row for each of the 48 units -->
  351.                     </tr>
  352.                    
  353.                     <tr id="units-48">
  354.                         <td class="hide">48</td>
  355.                         <!-- start polling days -->
  356.                         <!-- finish echo a row for each of the 48 units -->
  357.                     </tr>                  
  358.                    
  359.                    
  360.                 </tbody>
  361.             </table>
  362.  
  363. ----
  364. // This is JSON code now...
  365. <script type="text/javascript">
  366.  
  367. var doFades = true;
  368.  
  369. var Stations = {
  370.     Schedule : {}
  371. };
  372.  
  373. $(document).ready(function(){
  374.    
  375.     Stations.Schedule.days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
  376.    
  377.     Stations.Schedule.Show = function () {
  378.         _s = null;
  379.         _startDate = null;
  380.         _endDate = null;
  381.         this.days = Stations.Schedule.days;
  382.         _selector = '';
  383.        
  384.         this.setShow = function(s) {
  385.             this._s = s;
  386.            
  387.             this._startDate = new Date( parseInt(s.startminutes, 10) * 1000);
  388.             this._endDate = new Date(parseInt(s.endminutes, 10) * 1000 );
  389.            
  390.         };
  391.        
  392.         this.getEndDate = function(){
  393.             return this._endDate;
  394.         }
  395.        
  396.         this.getStartDate = function(){
  397.             return this._startDate;
  398.         }
  399.        
  400.         this._getShowDay = function (){
  401.             return this.days[this.getStartDate().getDay()];
  402.         };
  403.        
  404.         this._getShowUnitsTaken = function(){
  405.             // if it's the same day
  406.             return this._getEndUnits() - this._getStartUnits();
  407.         };
  408.        
  409.         this._getEndUnits = function(){    
  410.             if(this.getEndDate().getHours() == 0)
  411.             {
  412.                 //console.log(this._s.longname  +' ends at midnight');
  413.                 return 48;
  414.             }
  415.            
  416.             return this.getEndDate().getMinutes() !== 0 ? (this.getEndDate().getHours() * 2) : (this.getEndDate().getHours() * 2);
  417.         };
  418.        
  419.         this._getStartUnits = function(){
  420.             if(this.getStartDate().getHours() == 0)
  421.             {
  422.                 return 0;
  423.             }
  424.            
  425.             return this.getStartDate().getMinutes() !== 0 ? (this.getStartDate().getHours() * 2) : (this.getStartDate().getHours() * 2);
  426.         };
  427.        
  428.         this.getCellPositions = function() {
  429.             return {
  430.                 'start' : this.getStartDate(),
  431.                 'end'   : this.getEndDate(),
  432.                 'colIndex' : this.getStartDate().getDay() + 2,
  433.                 'startUnits' : this._getStartUnits(),
  434.                 'endUnits' : this._getEndUnits(),
  435.                 'unitsTaken' : this._getShowUnitsTaken()
  436.             }
  437.         };
  438.        
  439.         this.pad = function(number){
  440.             return number < 10 ? '0'+number : number;
  441.         };
  442.        
  443.         // return the table cell html.
  444.         this.toHtml = function () {
  445.            
  446.             var d = new Date();
  447.             var units = this._getStartUnits();
  448.                    
  449.             var rowspan = this._getShowUnitsTaken();       
  450.             var desc = this._s.description;
  451.             var name = this._s.longname;
  452.            
  453.             var starttime   = this.pad(this.getStartDate().getHours()) + ':' + this.pad(this.getStartDate().getMinutes());
  454.             var endtime     = this.pad(this.getEndDate().getHours()) + ':' + this.pad(this.getEndDate().getMinutes());
  455.            
  456.             var site = this._s.websitelink;
  457.            
  458.             var cls = this.isActive() ? 'current-program' : '';
  459.            
  460.             var isToday = this.getStartDate().getDay() === d.getDay() ? 'active-program' : '';
  461.            
  462.             var html = '<td class="schedule-show ' + isToday + ' ' + cls + '" rowspan="' + rowspan + '" data-start="' + this.getStartDate() + '" data-end="' + this.getEndDate() + '">';
  463.                 html += '<div>';
  464.                 html += '<a href="' + site + '" title="' + desc + '">' + name + '</a>';
  465.                 html += '</div>';
  466.                 if(doFades)
  467.                 {
  468.                     html += '<div class="schedule_details clearfix" style="display:none;">';
  469.                     html += '<img width="105px" height="105px" alt="' + desc + '" src="' + this._s.showimage + '">';
  470.                     html += '<strong>' + name + '</strong>';
  471.                     html += '<p>' + desc + '</p>';
  472.                     html += '<span>' + starttime + ' - ' + endtime +'</span>';
  473.                     html += '</div>';
  474.                 }
  475.                 html += '</td>';           
  476.            
  477.             return html;
  478.         };
  479.        
  480.         this.setTableSelector = function(sel){
  481.             this._selector = sel;
  482.         };
  483.        
  484.         // check if we should add the active class.
  485.         this.isActive = function(){
  486.             var t = new Date();
  487.            
  488.             return  t >= this.getStartDate() && t <= this.getEndDate();
  489.         };
  490.     };
  491.    
  492.     Stations.Schedule.ScheduleGen = function(){
  493.  
  494.         return {
  495.             insertShow : function(show) {
  496.                 var p = show.getCellPositions();
  497.                
  498.                 $('tr#units-' + p.startUnits).append(show.toHtml());
  499.             },
  500.        
  501.             init : function (stationName){
  502.                 var self = this;
  503.                
  504.                 // load the schedule.
  505.                 $.getJSON('http://localhost/myradio1.php?callback=?', {
  506.                     name: 'Minnesota FM'
  507.                 }, function(json){
  508.                     // loop each show and append to our giant table.
  509.                     // this is well sick.
  510.                    
  511.                     if(json.success === false)
  512.                     {
  513.                         $('.content-inner table').remove();
  514.                        
  515.                         $('<div>errors</div>').appendTo('.content-inner');
  516.                     }
  517.                     else
  518.                     {
  519.                         var currentDay = '';
  520.                         var day = 0;
  521.                         // highlight the current time..
  522.                         var d = new Date();
  523.                         var weekStart = new Date();
  524.  
  525.                         weekStart.setDate(d.getDate()-6-(d.getDay()||7));
  526.                        
  527.                         $.each(json.data.schedule, function(i, broadcast){
  528.                            
  529.                            
  530.                             var dStart = new Date( parseInt(broadcast.startminutes, 10) * 1000);
  531.                             var dEnd = new Date(parseInt(broadcast.endminutes, 10) * 1000 );
  532.  
  533.                    
  534.                             /*// transform to a show object defined above, if the show spans 2 days we create two show objects.
  535.                             // IF THE SHOW STARTS/ENDS AT MIDNIGHT, DON'T SPLIT IT.
  536.                             if(dStart.getHours() !== 0 && dEnd.getHours() !== 0 && dStart.getDate() != dEnd.getDate())
  537.                             {
  538.                                 var showOne = new Stations.Schedule.Show();
  539.                                 showOne.setShow(broadcast);
  540.                                
  541.                                 // set to midnight
  542.                                 showOne.getEndDate().setHours(0);
  543.                                 showOne.getEndDate().setMinutes(dStart.getMinutes());
  544.    
  545.    
  546.                                 // append first half of show.
  547.                                 self.insertShow(showOne);
  548.                                
  549.                                 // handle second half.
  550.                                 var showTwo = new Stations.Schedule.Show();
  551.                                 showTwo.setShow(broadcast);
  552.                                
  553.                                 showTwo.getStartDate().setDate(showTwo.getStartDate().getDate() + 1);
  554.                                 showTwo.getStartDate().setHours(0);
  555.                                 showTwo.getStartDate().setMinutes(dEnd.getMinutes());
  556.                                
  557.                                 //console.log('2nd Half Start: ' + showTwo.getStartDate());
  558.                                 //console.log('2nd Half End: ' + showTwo.getEndDate());
  559.                                
  560.                                 self.insertShow(showTwo);
  561.                                
  562.                                
  563.                             }
  564.                             else
  565.                             {*/
  566.                                 var show = new Stations.Schedule.Show();
  567.                                
  568.                                 show.setShow(broadcast);
  569.                                 show.setTableSelector('table#schedule');
  570.  
  571.                                 // add the show to the table. Thankfully the order these come out the API means they get added
  572.                                 // in the right place. So don't change the schedule builder code!
  573.                                
  574.                                 self.insertShow(show);
  575.                             //}
  576.            
  577.                            
  578.                         });
  579.                        
  580.                         var days = Stations.Schedule.days;     
  581.                        
  582.                         // apply the current day / time classes
  583.                         $('th:contains('+ days[d.getDay()]+')').addClass('active');
  584.                        
  585.                         $('td.time').each(function(i, cell){
  586.                             // get the value, convert to int.
  587.                             var hours = $(cell).html().split(':')[0];
  588.                            
  589.                             // compare the hours with now, add class if matched.
  590.                             if(parseInt(hours, 10) === d.getHours())
  591.                             {
  592.                                 $(cell).addClass('current_time');
  593.                             }
  594.                         });
  595.                     }
  596.                    
  597.                    
  598.                     if(doFades)
  599.                     {
  600.                         // apply events to show info fade in / out.
  601.                         $('td.schedule-show').hover(function(){
  602.                             $(this).find('.schedule_details').fadeIn('fast');
  603.                         }, function(){
  604.                             $(this).find('.schedule_details').fadeOut('fast');
  605.                         });
  606.                     }
  607.                 });
  608.                
  609.                
  610.             }
  611.         };
  612.     }();
  613.    
  614.  
  615.     Stations.Schedule.ScheduleGen.init();
  616.    
  617. });
  618. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement