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

Links Section on Website


Go to End
Reply

17 Posts   5369 Views

Avatar
gakenny

14 June 2008 at 1:13pm Community Member, 153 Posts

Hello,

It's been a while since I ventured on the forums and it's good to see that they are still active - what a great product!

I am developing a website that will include a popular links section. These will be administered in the CMS backend by the site administrator.

However, what I would like to do is to supply a 'click-through' where a link is selected and a counter is automatically added to before redirecting to the site.

The bit that I struggle with is specifically the incrementing of the counter associated with the LinkPage type that I have defined. I am sure this is quite straightforward but I am struggling as to where to start.

Any support offered would be gratefully received!

Kind regards,

Gary

Avatar
spenniec

14 June 2008 at 4:33pm Community Member, 37 Posts

Gary
Do you want the link info to be defined as individual pages or as entries in a CTF?
If you create a DB field called Counter when defining the link (whether it extends Page or DataObject) then you could code to update this field in the DB when the link is clicked on. Then your site administrator could see the value of the counter in the CMS if desired (or have it added to the Statistics).
There are probably a few ways to do, as there is everything.

Avatar
gakenny

14 June 2008 at 5:58pm Community Member, 153 Posts

Hi Spencer,

Thanks for the reply! You are on the button - I'm not how to do that DB update though and I was wondering if someone could point me to something in the documentation to help.

Cheers,

Gary

Avatar
Willr

15 June 2008 at 10:44am (Last edited: 15 June 2008 10:46am), Forum Moderator, 5511 Posts

Well I doubt theres much docs on this but you could do something like the Complex table field as mentioned. So you have a list of links. Then in the template you could have rather then

<% control Links %>
<a href="linkYouPut">Title!</a>
<% end_control %>

Instead have something like

<% control Links %>
<a href="url of your links page/goto?link={$ID}">Title!</a>
<% end_control %>

Then in your links page type (Im assuming you have a page type since it probably should!) You would define that goto method that link is pointing to. And that would be something like (Note: didnt test, its brief so not to take up too much of your time!)

function goto() {
$link = DataObject::get_by_id("NameOfYourLinkDataObject", (int) $_GET['link ']);
$link->Counter++;
$link->update(); // pretty sure this works
return Director::redirectTo($link->Link);
}

Ok so you pass the ID to the goto method in the link in the template. /goto?link=$ID.. as before. the goto method gets your Link DataObject by the id you passed. (you might need to add the checking around link to make sure everything exists!.

Update the database field 'Counter' by 1. Save the new Count into the dataobject. Then redirect the user to the link!

Then it

Avatar
gakenny

15 June 2008 at 8:24pm Community Member, 153 Posts

Thanks Will - that looks the ticket...!

I'm just trying to get my head around this:
<a href="url of your links page/goto?link={$ID}">Title!</a>

What would the URL be? I have added the code that you recommended to LinkPage.php (that is my page type) and I assume that the URL would need to point to this somehow...?

Maybe the URL should point to the relevant LinkHolder.php (say called 'links') and the the code should be contained in LinkHolder.php instead of LinkPage.php. What do you think?

Gary

Avatar
gakenny

15 June 2008 at 9:14pm Community Member, 153 Posts

Hi Will,

Actually, I have now created another PageType called RedirectPage and added a page called 'redirect' with this type in the root of the site (but hidden). I amended your HTML to:
<a href="redirect/goto?link={$ID}">Title!</a>

This works (in that it redirects to the site) but the DB update does not work. I do have a field called Counter but this remains at 0. I am not 100% of the syntax of the PHP and my mnd is blank tonight...

Cheers,

Gary

Avatar
gakenny

15 June 2008 at 9:32pm Community Member, 153 Posts

Hi,

One last post tonight...

I changed $link->update(); to $link->write(); but the counter still doesn't update.

Cheers,

Gary

Avatar
Willr

16 June 2008 at 9:21am Forum Moderator, 5511 Posts

<a href="url of your links page/goto?link={$ID}">Title!</a>
What would the URL be?

It would be the URL of the LinksPage page type. So if you create a LinksPage in the CMS with the URL of /links/ then the link would be link/goto?link=1 or something.

This would go to the controller of the page type 'link' (LinksPage) and call the goto() method.

Go to Top