Solved

How can I referece an array element using for loop?

Posted on 2009-07-06
3
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

690 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