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

Data Model Questions /

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

Linking to a page based on page ID

Go to End

10 Posts   3431 Views


Community Member, 6 Posts

24 November 2016 at 12:29pm

This is the code here, it is grabbing records from the pageview table that records page views. Its returning the id fine, now I just need to get the url and page name so I can use them in the view.

Sorry I should have started with this...

public function MostViewed($limit = 5) {
		//Returns a list of page id's
		$ID = PageView::get()

			->sort('Count', 'DESC');

        /*loop through each of the records retrieved and grab the link by matching the page id to the
        * corresponding data object.
        foreach ($ID as $id){

            //get the page id from the retrieved record
           $PageId = $id->PageID;
            $DataObject = DataObject::get("Page", "`ID` = '$PageId'");


        //should be returning the page url here or object to retrieve the url from
        return $ID;



Community Member, 13 Posts

24 November 2016 at 5:44pm

Edited: 24/11/2016 5:44pm

This code makes no sense, have you have trimmed out a heap of lines?
There is nowhere that you have built the data to be returned, you are returning $ID, the foreach loop is doing nothing.
Maybe you need to be using a join between your page and PageVew to get what you're after rather than a for loop.
Either that or the for loop needs to build an array to return.

Go to Top