Solved

How can I referece an array element using for loop?

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
[form-control] Retain values after a POST action 21 68
JSON decode 5 42
Why is my implode function empty 8 39
php mail() send emails but the message is lost 4 28
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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

738 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