Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I referece an array element using for loop?

Posted on 2009-07-06
3
Medium Priority
?
166 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 1200 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 800 total points
ID: 24790262
Yes to both questions.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

773 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