Question

I'm new to cakephp and I'm trying to write a for each loop that will get each event that is less than or equal to (<=) today's date.

First of all I'm not really sure if a for each loop is the best way to go about this, I have also been considering a while loop or an if statement but I don't know how else to get each entry from the database.

So here's where I've got to so far.

DATABASE HEADINGS

<?php foreach ($events['Event']['startDate'] <= $date): ?>

DATABASE RESULTS

Unfortunately I receive the following error:Parse error: syntax error, unexpected ')' in /homepages/3/d439567456/htdocs/cakephp/app/View/Events/live.ctp on line 22 (which is the for each loop line.

Any help would be great thanks and examples would be appreciated!

Était-ce utile?

La solution

you cannot just change how PHP works.

Usually, events also have a numeric index for more than one "Event".

foreach ($events as $event) {
    if ($event['Event']['startDate'] <= $date) {} else {}
}

To access a specific startDate directly, e.g. the first, you would need 0 as key:

if ($events[0]['Event']['startDate'] <= $date) {} else {}

But you cannot abuse foreach this way, though.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top