paging function that starts from no. of records returned after 5.

11 posts by 2 authors in: Forums > CMS Builder
Last Post: May 3, 2010   (RSS)

By zaba - April 28, 2010

Hi, I am developing a new section consisting of 2 pages, a current news page which shows the top 5 results list based on sort order in admin (and also the detail of that record when clicked). This I have done without any problems. The second part is a similar page called news archive. Here I want to show the list of records starting from the 6th record onwards, and also have a paging function.
I have created the archive page and paging function, but I can't work out how to tell it to ignore the first five results (as that is taken care of in the news page).
heres the code for the news-archive.php page.

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
require_once "/........../viewer_functions.php";



list($newsRecords_detail, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'where' => @$_REQUEST['detail'] ? "num = ".(int)$_REQUEST['detail'] : '1',
'limit' => '1',
));

$newsRecord = @$newsRecords_detail[0]; // get first record


// show error message if no matching record is found
if (!$newsRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}


list($newsListRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'perPage' => '5',
'allowSearch' => '0',
));

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<div id="left_col">
<!--This is the detail content -->
<h3><?php echo $newsRecord['title'] ?></h3>
<p><?php echo $newsRecord['lead_paragraph'] ?></p>
<?php foreach ($newsRecord['image'] as $upload): ?>
<?php if ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="<?php echo $newsRecord['title'] ?>" title="<?php echo $newsRecord['title'] ?>" />
<?php endif ?>
<?php endforeach ?>
<?php echo $newsRecord['content'] ?>
</div><!--/left_col -->

<!--This is the list content -->
<div id="right_col">
<h1>News</h1>
<?php foreach ($newsListRecords as $record): ?>

<h2><a href="?page=<?php echo $newsMetaData['page'] ?>&detail=<?php echo $record['num'] ?>"><?php echo $record['title'] ?></a></h2>
<?php foreach ($record['image'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="<?php echo $record['title'] ?>" title="<?php echo $record['title'] ?>" /><?php endif ?>
<?php endforeach ?>
<p><?php echo $record['summary'] ?></p>
<?php endforeach ?>

<!--Paging -->
<?php if ($newsMetaData['prevPage']): ?>&lt;&lt;
<a href="<?php echo $newsMetaData['prevPageLink'] ?>">prev</a>
<?php else: ?>
&lt;&lt; prev
<?php endif ?>

- page <?php echo $newsMetaData['page'] ?> of <?php echo $newsMetaData['totalPages'] ?> -

<?php if ($newsMetaData['nextPage']): ?>
<a href="<?php echo $newsMetaData['nextPageLink'] ?>">next</a> &gt;&gt;
<?php else: ?>
next &gt;&gt;
<?php endif ?>

<!--/Paging -->

</div><!--/right_col -->
<div class="clear"></div><!--/clear -->

</body>
</html>

Re: [zaba] paging function that starts from no. of records returned after 5.

By Jason - April 28, 2010

Hi,

We can definitely do that, we just need to change our code a little. Replace the code at the php top of your page with this:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
require_once "/........../viewer_functions.php";



list($newsRecords_detail, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'where' => @$_REQUEST['detail'] ? "num = ".(int)$_REQUEST['detail'] : '1',
'limit' => '1',
));

$newsRecord = @$newsRecords_detail[0]; // get first record


// show error message if no matching record is found
if (!$newsRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}

//Get the first 5 news records
list($firstRecords,$firstMetaData)=getRecords(array(
'tableName'=>'news',
'allowSearch'=>false,
'limit'=>5,
));

//create a list of the first 5 new record numbers
$recordList="";

foreach($firstRecords as $first){
if($recordList){$recordList.=",";}
$recordList.=$first['num'];
}

//get news records, excluding the first 5
list($newsListRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'perPage' => '5',
'allowSearch' => '0',
'where' => "num NOT IN (".mysql_escape($recordList).")",
));

?>


So what we did here was to create a list of the first 5 records, then told the database to only return records that didn't match that list.

Give this a try and let me know if you run into any issues.
---------------------------------------------------
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] paging function that starts from no. of records returned after 5.

By zaba - April 28, 2010

Thanks Jason, this works but what is happening is the detail content is not showing the first record of the page, (it is sowing the very first record not the 6th) and likewise when you go to the second page it shows the very first record and not the 11th. Please could you help me fix this.
Really appreciate your help.

Re: [zaba] paging function that starts from no. of records returned after 5.

By Jason - April 28, 2010

Hi,

I took a look. The problem we were having is that we were still selecting our news record from the full list of records, not the shortened list that we had created.

I changed this to select from the shorter list, and now the news-archive will always display the first record that is displayed in the list on the side. Take a look and make sure everything is working the way you want it to.

Thanks.
---------------------------------------------------
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] paging function that starts from no. of records returned after 5.

By zaba - April 28, 2010

Hi Jason, thanks for all your work on this. It does display the first record of each page but when you click on a link from the list on the right it still displays the first record, almost there I think. Once again you guys are amazing!!!

Re: [zaba] paging function that starts from no. of records returned after 5.

By Jason - April 29, 2010

Hi,

I revisited the issue we were having. I changed it so that if the user clicks on a specific news story, it will display that. If not, it will display the first record in the list.

Let me know if this now works the way you want.

Thanks.
---------------------------------------------------
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] paging function that starts from no. of records returned after 5.

By zaba - April 30, 2010

Thanks Jason, just had a look, one little issue, if you (on page one) click on the right hand links it loads the correct page, but if you then click on next (page 2) it doesn't load the correct story, likewise if you do it in the opposite direction. I'd really appreciate you taking another look. Sorry about that. :-)

Re: [Jason] paging function that starts from no. of records returned after 5.

By zaba - April 30, 2010

Hi Jason, yes I think it makes more sense to reveal the first story in that set of stories, rather than remain on the story being shown. It makes sense to do this to me anyway as that's what I was expecting to happen when I did it the first time.
Thanks again.

Re: [zaba] paging function that starts from no. of records returned after 5.

By Jason - May 3, 2010

Hi,

I made a small change to the code. Now every time the "next" or "prev" page buttons are clicked, the page will display the first story in the list.

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/