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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to show last guestbook entry in the sidebar


Go to End


6 Posts   2212 Views

Avatar
Mauro74

Community Member, 30 Posts

1 November 2011 at 4:57am

Edited: 01/11/2011 4:59am

Hi all.

Does anyone knows how or point me in the direction to show the last guestbook entry(using the simple guestbook module) in the sidebar?

Thanks in advance.

Avatar
Euphemismus

Community Member, 82 Posts

2 November 2011 at 9:06pm

Good morning,

do you have programming skills? Otherwise I'd have to implement that ;-)

If you do it yourself:
Create a new dataobject and select from table 'GuestbookEntry', either by date ('Created') or by MAX ID (latest should be the current guestbook entry).

Kind regards,
Marc

Avatar
Mauro74

Community Member, 30 Posts

3 November 2011 at 12:18am

Hi Marc,
I do have 'some' php programming skills, but mostly front-end.

Do you mean having something like:

function guestPosts(){
		$posts = DataObject::get('GuestbookEntry', '', 'Created', '');
		return $posts;
	}

I assume that the function has to be in Page.php to be available for all the pages and then having something in the Sidebar.ss like:

<% control guestPosts %>
		<p>$Comment</p>
                <p>By $FirstName - $Created.Nice</p>
                <br>
<% end_control %>

The thing I'm not sure of, is the query itself -

 DataObject::get(...); 
- could you help me with that please?

Thanks a lot! :)

Avatar
Euphemismus

Community Member, 82 Posts

3 November 2011 at 12:54am

Hi Mauro74,

yes, that was the solution I'd suggest ;-)
Problematic is the relation of entries to comments if you want to show those, too.
A good resource for this issue is this
What you need is (sql):

SELECT *
FROM `GuestbookEntry`
ORDER BY Created DESC
LIMIT 5 

put into a dataobject:
$objData = DataObject::get(
	'GuestbookEntry',
	'', //filter
	'Created', //sort by
	'', //join
	'5' //limit
);

That's also correct in your example - I'm not sure about the sort... I'm faintly remembering, one has to add "ORDER BY" or sth, even in the method call, but I can't test right now (and I mostly use raw SQL and covert results to dataObjectSets). Nevertheless you're also able to add the comments in the join clause.

Packed into your suggested method and calling it via frontend should do the trick. If you create an own extension to the page class in your /mysite directory (see the tutorials), you should be able to add this data to the sidebar on all pages.

Kind regards,
Marc

Avatar
Mauro74

Community Member, 30 Posts

3 November 2011 at 2:17am

Hi Marc,

I don't need to show the comments, no problem about it. I implemented the query as you suggested and simply by putting the function into Page.php and calling it in an include file called in the sidebar it works like a breeze!

One more thing. I have an issue with reCaptcha. When I try to enter a new guestbook entry from the website (not in the cms) I have this error:

Fatal error: Call to undefined function curl_init() in C:\xampp\htdocs\website\recaptcha\code\RecaptchaField.php on line 363

Is it because I'm running the website on my localhost? By the way I've got the reCaptcha keys and all that set in the _config file.

Thanks for you help!

Avatar
Euphemismus

Community Member, 82 Posts

3 November 2011 at 5:10am

Hi,

the CURL error could have a few causes, most likely your local webserver or php-configuration. CURL is used to access remote server content (I think, the captcha images) and if that is blocked or CURL not installed/initialized in your local environment: error.

Kind regards,
Marc