Limiting list output to 10 per page when using array_merge

7 posts by 3 authors in: Forums > CMS Builder
Last Post: June 28, 2011   (RSS)

By gadefgaertgqe - September 29, 2010 - edited: September 29, 2010

Hi!

Sorry about the daily questions, but this project I am working on is proving tricky [crazy]

I am merging arrays to output two separate tables (which have identical fields) on the same page as one list.

I would like to be able to limit it to 10 per page, but whatever I try it still outputs everything on the one page.

Here is how I do it:

// load records
list($national_newsRecords, $national_sachs_newsMetaData) = getRecords(array(
'tableName' => 'national_sachs_news',
'allowSearch' => '0',
'perPage' => '10',
));


// Tip: $dealer_newsRecords, $dealer_newsMetaData are the array names!
list($dealer_newsRecords, $dealer_newsMetaData) = getRecords(array(
'tableName' => $final,
'allowSearch' => '0',
'perPage' => '10',
));

// put all the records in one list
$allRecords = array_merge( $dealer_newsRecords, $national_newsRecords );
$allMetaData = array_merge( $dealer_newsMetaData, $national_newsMetaData );


// sort them by date
function createdDateCompareFunction($a, $b) { return -strcmp($a['createdDate'], $b['createdDate']); }
usort($allRecords, 'createdDateCompareFunction');

?>


Then to display I use:
<?php if ($allMetaData['invalidPageNum']): ?>
Results page '<?php echo $allMetaData['page']?>' not found, <a href="<?php echo $allMetaData['firstPageLink'] ?>">start over &gt;&gt;</a>.<br/><br/>
<?php elseif (!$allMetaData): ?>
No records were found!<br/><br/>
<?php endif ?>

<?php foreach ($allRecords as $record): ?>
<div class="post" id="<?php echo $record['identify'] ?>-<?php echo $record['num'] ?>">
<h1><?php echo $record['title'] ?></h1>
<h6><?php echo $record['description'] ?></h6>
<div class="info"><?php echo date("D, M jS, Y g:i:s a", strtotime($record['date'])) ?></div>
<p>
<?php foreach ($record['main_news_image'] as $upload): ?>

<?php if ($upload['hasThumbnail']): ?>
<a href="<?php echo $record['_link'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /></a><br/>

<?php else: ?>
&nbsp;

<?php endif ?>
<?php endforeach ?>
</p>
<a href="<?php echo $record['_link'] ?>" class="button-small">Read More</a></p>
</div>
<br/><br/><br/><br/>
<?php endforeach ?>

<div class="pagination">
<ul class="page-numbers">

<li>
<span class="page-numbers current">Page: <?php echo $national_sachs_newsMetaData['page'] ?> of <?php echo $allMetaData['totalPages'] ?></span>
</li>
<li>
<?php if ($allMetaData['prevPage']): ?>
<a href="<?php echo $allMetaData['prevPageLink'] ?>" class="next page-numbers">&lt;&lt; prev</a>
<?php else: ?>

<?php endif ?>
</li>
<li>
<?php if ($allMetaData['nextPage']): ?>
<a href="<?php echo $allMetaData['nextPageLink'] ?>" class="next page-numbers">next &gt;&gt;</a>
<?php else: ?>

<?php endif ?>
</li>

</ul>
</div>


Where am I going wrong?

Thanks!

Paul

Re: [8bit Gamer] Limiting list output to 10 per page when using array_merge

By Jason - September 29, 2010

Hi,

That's interesting. How many records do you have in each section?

If you could email your CMS login and FTP details to jason@interactivetools.com I can take a look and see what's going on.

Note: Only email this information, don't post it to the forum.

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] Limiting list output to 10 per page when using array_merge

By gadefgaertgqe - September 29, 2010 - edited: September 29, 2010

Hi Jason!

Thanks for replying.

I am not sure what you mean by how many records, sorry.

Details emailed :)

Re: [8bit Gamer] Limiting list output to 10 per page when using array_merge

By Jason - September 29, 2010

Hi Paul,

I took a look and actually the code is working correctly. This is what's happening.
You're taking records from 2 tables (national_sachs_news and test_sach_news). national_sachs_news has 3 records in it and test_sach_news has 1 record in it. In your code, you're telling it to limit it to 10 records per page. So since the tables each have less then 10 records, all of their records are being returned. These are the 4 records that appear on the page. If there were more than 10 records in one or both of those tables, then you would see multiple pages.

Another thing to keep in mind is that your merged array ($allRecords) will be both of your records. If they're limited to 10 per page each, that means that $allRecords could have up to 20 records in it. So if you only want a max of 10 records to appear on the page, you should limit each to 5 records per page.

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] Limiting list output to 10 per page when using array_merge

By gadefgaertgqe - September 29, 2010

Hi Jason,

Let me start with DOH!

I was an idiot and should have seen that!

End of the office day and too much coffee.

Thanks for taking the time to look at it, and now I have made the changes it does indeed work fine.

Paul

Re: [zick] Limiting list output to 10 per page when using array_merge

By Jason - June 28, 2011

Hi,

The perPage option will only limit the number of record at the time the query takes place and can't be applied afterwards.

Another option would be to return everything and then do your pagination manually. Take a look at this post:
http://www.interactivetools.com/forum/gforum.cgi?post=88664#88664.

This is an example of doing pagination for a category section my using a for loop to access elements in the array. This should help get you started.

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/