Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

access to fields


Reply


5 Posts   1468 Views

Avatar
Nicolaas

Forum Moderator, 213 Posts

17 October 2007 at 12:53pm

Hi Folk

I have the following function:

class eventsPage_Controller extends Page_Controller {
public function eventsItems() {
$Events = DataObject::get("eventSubmissionDataObject", "ID > 0 ", "EventDate DESC");
return $Events;
}
}

I access this on my template using the following:

<% control eventsItems %>
<li>
<div class="eventDate">$EventDate</div>
</li>
<% end_control %>

Works a treat. However, I would like to manipulate the data in this $Events object (i.e. the individual fields) such as EventDate and then return an html snippet rather than using the method above.

Can you tell me how to do this?

Thanks a million

Nicolaas

Avatar
Sean

Forum Moderator, 922 Posts

17 October 2007 at 5:10pm

Edited: 17/10/2007 5:20pm

Hi there,

When you say 'html snippet', do you want the php to return the html, instead of doing it in the template? e.g.

PHP:

function MyObjects() {
$objects = DataObject::get('Events');
if($objects) {
$output = '<ul id="Objects">';
foreach($objects as $object) {
$customTitle = "This is my cool title called $object->Title";
$output .= "<li>$object->ID - $customTitle</li>\n";
}
$output .= '</ul>';
}
return $output;
}

While in the foreach loop you can just output whatever you like by calling fields or methods on $object like $object->ID or $object->Title, if that's what you meant.

The HTML is then simply this:

HTML:

$MyObjects

Not the best example of separation of HTML from PHP code, but it works pretty well for a simple listing like a <ul> with <li> elements inside.

Cheers,
Sean

Avatar
Nicolaas

Forum Moderator, 213 Posts

18 October 2007 at 12:46am

Sean ! wow, so simple - SUCH a beaty. Thank you.... Works a treat

Avatar
Ingo

Forum Moderator, 801 Posts

18 October 2007 at 8:54am

depending what you're trying to do, you can build unordered lists even simpler (with nesting even):
DataObjectSet->UL()
DataObjectSet->buildNestedUL()

Avatar
Nicolaas

Forum Moderator, 213 Posts

18 October 2007 at 8:57am

wow, that is cool. Will look into this. Thank you. For the present exercise, I am actually sending the data to a different class to make a calendar. I just used the list as an example, because that is so common = > definitely will make use of that function.

Thank you

Nicolaas