duandong1869 2012-01-19 21:04
浏览 39

PHP解析遍历#天,但希望限制输出的数量

So I'm using the following long bit of code to list upcoming events. It asks for the number of days you want to display events for, let's say 10 days, and it groups as many events in those 10 days. I changed the code so there isn't just one header for a given date with multiple events under it, rather than each event date is shown. However it still gives me all the events of the next 10 days. I would like to limit the code so that of the numerous events of the next 10 days it only returns/displays/outputs 6 events.

I'm pretty sure what I need to change is here: for ($i=0; $i<$days_to_display; $i++) but the $days_to_display bit makes it tricky.

Any help would be greatly appreciated!

 <?php

// List View of Coming Events v .9
// Plug-in for PHPiCalendar 2.0 / 2.1
// developed by Johnathon Wright (my initials @ mustmodify.com)

// Original Publication Date: 2005 10 28
// Originally Developed for St. Barnabas Episcopal Church website

// Include this file from your PHP-enabled front page.
// Do something like this:
// <?PHP include 'd:/inetpub/.../phpical/event_list.php'; 

// DEFINE the base PHPiCalendar directory below.
define('BASE', 'c:/inetpub/ouhsd.k12.ca.us/phpicalendar/');

// create an actual PHPiCalendar instance
require_once(BASE.'functions/ical_parser.php');
require_once(BASE.'functions/list_functions.php');
require_once(BASE.'functions/template.php');
header("Content-Type: text/html; charset=$charset");

// at this point, the calendar has been generated and its data
// is stored in $master_array. Test to ensure that this is working by
// un-commenting the lines below.

/*
echo "<pre>";
print_r($master_array);
echo "</pre>";
*/

// A few settings...

// days_to_display_default should be a positive integer.
$days_to_display_default = 14;

// SHOW PRIVATE EVENTS {TRUE, FALSE}
// In Mozilla Sunbird, events can be set to PRIVATE.
// PHPiCalendar does not display the title or description for
// these events, but does display the time, if applicable.
// If you want to hide these events, set $show_private_events to FALSE.
// Otherwise, it should be TRUE

$show_private_events = TRUE;

// day_to_begin should be a text string. See http://www.php.net/strtotime
// for full documentation. Some values I tested:
// -------------------------------------------------------
// last monday - the most recent Monday.
// thursday - this gave me the date of the next Wednesday.
// yesterday - yesterday
// today - today
// next week - +2 weeks (How can this be? Don't ask me, it's PHP...)
// 1 week - a week from today

$day_to_begin = "today";


// Replace with the commented code if you would NOT like 
// users to be able to set this value on their own.



$days_to_display = $days_to_display_default;

/*

// this code replaces the above code if you want to prevent
// users from setting the number of days to display

$days_to_display = $days_to_display_default;

*/


// This next section seems terribly inefficient. We calculate the unix
// timestamp of first-thing-this-morning by finding out the date
// and then looking up the timestamp for that date... 

// Again, the commented code prevents users from setting their own start date.


$date_start = date('Ymd', strtotime("now")); 
$timestamp_start = strtotime(dateOfWeek($getdate, $date_start));

/*
$date_start = date('Ymd', strtotime("now")); 
$timestamp_start = strtotime(dateOfWeek($getdate, $date_start));
*/

// Display the date range for this list

$timestamp_end = $timestamp_start + ($days_to_display * 24 * 60 * 60);

// seed the iterator $thisdate with todays timestamp.
// It will loop through $days_to_display... 

$thisdate = $timestamp_start;

// Loop through the days, finding events in each day

for ($i=0; $i<$days_to_display; $i++) 
   {

   $thisday = date("Ymd", $thisdate);

   if (isset($master_array[$thisday])) 
      {
      echo "<br/>";

      foreach($master_array[($thisday)] as $event_start_time => $event_set)
         {
         foreach ($event_set as $event_id => $event_info)
            {
            // Where are we?
            //    date is $thisdate
            //    time is $event_start_time
            //    event is $event_id;

            if (! (($event_info['event_text'] == '**PRIVATE**') && ($show_private_events == FALSE)))
               {
// event title
                echo "<li>";
               echo "<span class='bold_text'>" . stripslashes(urldecode($event_info['event_text'])) . "</span><br/>";


// event time range, if not an all-day event
               echo " <span style='display: block;'>". date("D. F jS", $thisdate) . "";

                if (! (($event_info['event_start'] == '0000') && ($event_info['event_end'] == '0000')))
                    {
                  echo ":
" . date("g:ia", $event_info['start_unixtime']) . " - " . date("g:ia", $event_info['end_unixtime'])."</span>";
                  }

// event location
               if (strlen($event_info['location']) > 0)
                  {
                  echo " <span class='italic_text'>" . stripslashes(urldecode($event_info['location'])) . "</span>";
                  }


                /*  
                if ( (($event_info['event_start'] == '0000') && ($event_info['event_end'] == '0000')))
                    {
                  echo "all day";
                  }*/

               // event description.

                echo "</li>";
               } // IF private event AND we don't want to see those
            }// END enter this event
         } // END foreach event
      } // END if there are any events today in the array

   // iterator should be the next day.
   // Why is this 25? Shouldn't it be 24?
   $thisdate = ($thisdate + (25 * 60 * 60));
   }



?>
  • 写回答

2条回答 默认 最新

  • doumao9363 2012-01-19 21:09
    关注

    Put a counter in there somewhere to keep track of how many events have been output, and terminate the loop once you reach the limit:

    $limit = 6;
    $displayed = 0;
    
    foreach(...) {
       if (show_event()) {
          ... show event ...
          $displayed++;
          if ($displayed >= $limit) {
              break;
          }
       }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能