Solved

How can I referece an array element using for loop?

Posted on 2009-07-06
3
158 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 count occurrences of each item in an array.

786 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