Blog Page Navigation - Early Thoughts

6 posts by 2 authors in: Forums > CMS Builder
Last Post: August 3, 2011   (RSS)

Re: [Perchpole] Blog Page Navigation - Early Thoughts

By Perchpole - August 2, 2011

Further to my post yesterday, here's a bit more info which may help people better understand what it is I'm trying to achieve.

I've set-up a basic section in CMSB called "blog". It's set-up to output the records in a list - with 5 entries per page.

This is the basic code...

// load records
list($blogRecords, $blogMetaData) = getRecords(array(
'tableName' => 'blog',
'perPage' => '5',
'loadUploads' => '0',
'allowSearch' => '0',
));


//Loop Thru results
<?php foreach ($blogRecords as $record): ?>
<a name="<?php echo $record['num'] ?>" id="<?php echo $record['num'] ?>"></a>
<strong><?php echo $record['title'] ?></strong><br/>
<?php echo $record['content'] ?><br/>
<hr/>
<?php endforeach ?>


For each entry there's just a title and one or two paragraphs of text. Nothing more. For this reason there is no need to go to a detail page.


What I want to do is create a navigation widget for the home page so that readers can click on a link and jump straight to the related entry in the blog. You'll notice I've used named anchors next to the title of each record. I'll be able to use these in the links to line-up the correct entry on the page.

The trouble is, how do I factor in the page number?

Using the example code above we can see there will be 5 records perPage. So, if there were 7 blog entries in total, record numbers 7, 6, 5, 4 & 3 would be shown on the first page. Record numbers 1 and 2 will be on the second page.

If I want to link to record num 5 on the first page the link would be something like this...

./index.php?blog.php#5

However if I want to link to record num 2 it would be like this...

./index.php?blog.php?page=2#2

How do I generate a link that takes this into account and modifies the link code accordingly?

:0/

Perch

Re: [Perchpole] Blog Page Navigation - Early Thoughts

By Jason - August 2, 2011

Hi Perch,

Here is a possible solution. For your navigation, return a list of all your blog entries like this:

// load records
list($blogRecords, $blogMetaData) = getRecords(array(
'tableName' => 'blog',
'loadUploads' => '0',
'allowSearch' => '0',
));


Then, you can use a counter to increment a "page" variable every X records, X being the number of records per page being shown on your blog page. You could then output your navigation list like this:

<?php
$perPage = 5;
$recordCount = 0;
$currentPage = 1;
?>
<?php foreach ($blogRecords as $record): ?>
<?php

if (++$recordCount % $perPage == 0) { $currentPage ++; }

?>
<a name="./index.php?blog.php?page=<?php echo $currentPage;?>#<?php echo $recordNum;?>">
<strong><?php echo $record['title'] ?></strong>
</a>

<?php endforeach ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Blog Page Navigation - Early Thoughts

By Perchpole - August 2, 2011

Hi, Jason -

This is great. Thanks.

Just one issue. The counter seems to be out by 1 place. The 5th record should be the last item on the first page - but the link sends it to page 2!

:oS

Perch

Re: [Perchpole] Blog Page Navigation - Early Thoughts

By Jason - August 3, 2011

Hi,

Yes, you're right, try this change:

<?php
$perPage = 5;
$recordCount = 0;
$currentPage = 1;
?>
<?php foreach ($blogRecords as $record): ?>
<?php

if ((++$recordCount % ($perPage + 1)) == 0) { $currentPage ++; }

?>
<a name="./index.php?blog.php?page=<?php echo $currentPage;?>#<?php echo $recordNum;?>">
<strong><?php echo $record['title'] ?></strong>
</a>

<?php endforeach ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Blog Page Navigation - Early Thoughts

By Perchpole - August 3, 2011

Brilliant!

Thanks, Jason.

:0)

Perch