Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Links Section on Website

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

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

Page: 1 2 3
Go to End
Author Topic: 5338 Views
  • gakenny
    Avatar
    Community Member
    153 Posts

    Links Section on Website Link to this post

    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

  • spenniec
    Avatar
    Community Member
    37 Posts

    Re: Links Section on Website Link to this post

    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.

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Links Section on Website Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Links Section on Website Link to this post

    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

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Links Section on Website Link to this post

    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

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Links Section on Website Link to this post

    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

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Links Section on Website Link to this post

    Hi,

    One last post tonight...

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

    Cheers,

    Gary

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Links Section on Website Link to this post

    <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.

    5338 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.