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


Reply

17 Posts   5368 Views

Avatar
gakenny

16 June 2008 at 5:36pm Community Member, 153 Posts

Thanks Will...

Anyone have any ideas on how to update the counter? I am struggling with the DB update...

Avatar
Willr

16 June 2008 at 5:49pm Forum Moderator, 5511 Posts

I dont see why $Link->update() doesnt work.

I would throw a Debug::show() in the function to see whats going on!

..
$link->Counter++;
$link->update(); // pretty sure this works
Debug::show($link); // is the counter updated!?!?!?!
die(); // dont redirect so you can see the debug statement

return Director::redirectTo($link->Link);

Avatar
gakenny

16 June 2008 at 7:20pm Community Member, 153 Posts

Hello Will,

False alarm! It appears that the database field was updated - it just wasn't displaying in the backend. Why would the counter not be updated when selecting the relevant LinkPage when logged in as Administrator.

Anyway, here is my solution:

In my link list, add this code:
<li><a href="{$Link}go" title="Read more on &quot;{$Title}&quot;">$Title</a></li>

In the LinkPage type controller:
function go() {
$link = DataObject::get_by_id("LinkPage", $this->ID);
$link->Counter++;
$link->write();
return Director::redirect("$link->URL");
}

Cheers,

Gary

Avatar
Willr

16 June 2008 at 7:52pm Forum Moderator, 5511 Posts

Does that actually work?

<a href="{$Link}go"...

Wouldnt that point to like

<a href="www.google.comgo" ?

The link would need to be something like

<a href="links/go?=$Link">

as that would point to the go method on the links Page?

Avatar
gakenny

16 June 2008 at 8:06pm Community Member, 153 Posts

Hi Will,

Heh heh - It sure does work! $Link contains the URL to the relevant LinkPage that has previously been defined. It seems that this already has the slash ('/') added as "$Link/go" contains two slashes.

Because, I am linking to the relevant LinkPage, I did not need to supply the ID as a parameter to the function and instead used the $this->ID variable within the function itself.

I could PM the site to you if you want to have a look - it is still work in progress.

Cheers,

Gary

Avatar
Willr

16 June 2008 at 8:12pm Forum Moderator, 5511 Posts

Ok so if the DB is being updated? and its not displaying the new value in the CMS?.. Tried flushing the cms - admin?flush=1 as if thats pulling from the DB and the DB has the correct value somethings not right :P is a link a DataObject?

Avatar
simon_w

16 June 2008 at 8:21pm Forum Moderator, 474 Posts

I had a similar problem with the kudos widget. This was because the live version of the object was being updated, but not the stage which is shown in the database.

The code I used to fix:

      $this->value++;
      $this->writeToStage("Stage");
      // Make it think that we've changed the variable so the Live site gets updated as well
      $this->value++;
      $this->value--;
      $this->write();

Avatar
gakenny

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

Hi Will,

By dataobject do you mean have I defined a page type called LinkPage and defined URL and Counter as additional fields.

I have removed the Counter from being displayed in the CMS and have instead shown this in the LinkHolder when displaying all links (and sorting by Counter).

The database is being updated using the code shown earlier and it is incrementing at each link click.

Cheers,

Gary