Solved

How can I referece an array element using for loop?

Posted on 2009-07-06
3
155 Views
Last Modified: 2012-05-07
In the function, it uses foreach loop but I only want to retrieve and print out the 1st 3 events.  I uses these code in for loop but not working.  This is my 1st PHP and trying to apply C# code syntax doesn't work.
for($i=1; $i<=3; $i++) {
                              $event = $eventFeed[i];
                              echo "\t<li>" . $event->title->text .  " (" . $event->id->text . ")\n";
                              echo "\t\t<ul>\n";
                              foreach ($event->when as $when) {
                                echo "\t\t\t<li>Starts: " . $when->startTime . "</li>\n";
                              }
                              echo "\t\t</ul>\n";
                              echo "\t</li>\n";
                          }
function outputCalendar($client) 

{

  $gdataCal = new Zend_Gdata_Calendar($client);

  $eventFeed = $gdataCal->getCalendarEventFeed();

  echo "<ul>\n";

  foreach ($eventFeed as $event) {

    echo "\t<li>" . $event->title->text .  " (" . $event->id->text . ")\n";

    echo "\t\t<ul>\n";

    foreach ($event->when as $when) {

      echo "\t\t\t<li>Starts: " . $when->startTime . "</li>\n";

    }

    echo "\t\t</ul>\n";

    echo "\t</li>\n";

  }

  echo "</ul>\n";

}

Open in new window

0
Comment
Question by:lapucca
3 Comments
 
LVL 12

Accepted Solution

by:
kevin_u earned 300 total points
ID: 24790185

your code forgot the $ in $event = $eventFeed[i];

function outputCalendar($client)
{
  $gdataCal = new Zend_Gdata_Calendar($client);
  $eventFeed = $gdataCal->getCalendarEventFeed();
  echo "<ul>\n";
  for ($i = 0; $i < 3; $i++) {
    $event = $eventFeed[$i];
    echo "\t<li>" . $event->title->text .  " (" . $event->id->text . ")\n";
    echo "\t\t<ul>\n";
    foreach ($event->when as $when) {
      echo "\t\t\t<li>Starts: " . $when->startTime . "</li>\n";
    }
    echo "\t\t</ul>\n";
    echo "\t</li>\n";
  }
  echo "</ul>\n";
}
0
 

Author Comment

by:lapucca
ID: 24790202
Ah, got that.  Does arrary index starts with 0 in PHP array?  Also, if I want to break out of the for looop when $event is assign a null can I code it like:
function outputCalendar($client)
{
  $gdataCal = new Zend_Gdata_Calendar($client);
  $eventFeed = $gdataCal->getCalendarEventFeed();
  echo "<ul>\n";
  for ($i = 0; $i < 3; $i++) {
    $event = $eventFeed[$i];
if($event == null)
   break;
    echo "\t<li>" . $event->title->text .  " (" . $event->id->text . ")\n";
    echo "\t\t<ul>\n";
    foreach ($event->when as $when) {
      echo "\t\t\t<li>Starts: " . $when->startTime . "</li>\n";
    }
    echo "\t\t</ul>\n";
    echo "\t</li>\n";
  }
  echo "</ul>\n";
}
0
 
LVL 36

Assisted Solution

by:Zyloch
Zyloch earned 200 total points
ID: 24790262
Yes to both questions.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now