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

时间:2012-01-19 21:04:47

标签: php parsing limit icalendar limits

所以我使用以下长代码来列出即将发生的事件。它要求您显示事件的天数,比如说10天,并在10天内分组尽可能多的事件。我更改了代码,因此在给定日期下不只有一个标题,其中包含多个事件,而不是显示每个事件日期。但它仍然给了我接下来10天的所有事件。我想限制代码,以便在接下来10天的众多事件中,它只返回/显示/输出6个事件。

我很确定我需要改变的是: for ($i=0; $i<$days_to_display; $i++) 但是$days_to_display位让它变得棘手。

非常感谢任何帮助!

 <?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 个答案:

答案 0 :(得分:0)

在某处放置一个计数器以跟踪已输出的事件数,并在达到限制后终止循环:

$limit = 6;
$displayed = 0;

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

答案 1 :(得分:0)

这可能不是最好的方法!但是你可以在foreach之上添加另一个for循环,例如:

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

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

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

     for($k=0;$k<=$noofevents;$k++) //Where $noofevents is the number of events! 
    {
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 for x events
  } //END if isset
 }//END for days