在PHP中向大型数组添加分页
我创建并显示了这个非常大的数组,现在它显示在一个长列表中。我需要在下面的代码中添加一些分页,我想知道这是否可能。我通常使用php/mysql记录集,而不是数组。谢谢在PHP中向大型数组添加分页,php,arrays,paging,Php,Arrays,Paging,我创建并显示了这个非常大的数组,现在它显示在一个长列表中。我需要在下面的代码中添加一些分页,我想知道这是否可能。我通常使用php/mysql记录集,而不是数组。谢谢 <?php $invitation_codes = $mwx_settings['invitation_codes']; $idx=0; ?> <?php foreach ($invitation_codes as $idx=>$invitation_code) : ?> <tr&
<?php
$invitation_codes = $mwx_settings['invitation_codes'];
$idx=0;
?>
<?php foreach ($invitation_codes as $idx=>$invitation_code) : ?>
<tr>
<td style="background-color:white;"><div align="center"><input type="text" name="invitation_codes[<?php echo $idx; ?>][invitation_code]" value="<?php echo $invitation_code['invitation_code']; ?>" size="30" /></div></td>
<td style="background-color:#EEE;"><div align="center" style="color:red;font-weight:bold;"><?php echo $invitation_code['total_use_count']?$invitation_code['total_use_count']:'0'; ?></div></td>
<td style="background-color:white;"><div align="center"><input type="text" name="invitation_codes[<?php echo $idx; ?>][max_use_count]" value="<?php echo $invitation_code['max_use_count']; ?>" size="6" /></div></td>
<td style="background-color:white;"><div align="center"><input type="text" name="invitation_codes[<?php echo $idx; ?>][invitation_code_expiry]" value="<?php echo $invitation_code['invitation_code_expiry']; ?>" size="20" /></div></td>
<td style="background-color:white;"><div align="center"><input type="text" name="invitation_codes[<?php echo $idx; ?>][assigned_product]" value="<?php echo $invitation_code['assigned_product']; ?>" size="60" /></div></td>
<td style="background-color:white;"><div align="center"><input type="text" name="invitation_codes[<?php echo $idx; ?>][product_lifetime_or_expiry]" value="<?php echo $invitation_code['product_lifetime_or_expiry']; ?>" size="25" /></div></td>
<td style="background-color:white;"><div align="center"><input type="text" name="invitation_codes[<?php echo $idx; ?>][referred_by_id]" value="<?php echo $invitation_code['referred_by_id']; ?>" size="10" /></div></td>
<td style="background-color:white;">
<div align="center">
<input type="hidden" name="invitation_codes[<?php echo $idx; ?>][active]" value="0" /><input type="checkbox" name="invitation_codes[<?php echo $idx; ?>][active]" style="float:none;" value="1" <?php if ($invitation_code['active']) echo 'checked="checked"'; ?> />
</div>
</td>
<td style="background-color:white;">
<div align="center">
<input type="hidden" name="invitation_codes[<?php echo $idx; ?>][delete]" value="0" /><input type="checkbox" name="invitation_codes[<?php echo $idx; ?>][delete]" style="float:none;" value="1" />
</div>
</td>
<?php endforeach; ?>
</tr>
请参阅此PEAR模块:这里有一个快速模板,用于对数组进行分页,以防其他人发现此模板,并且不希望包含外部库:
// $items is the array to be paged
$items = $this->get_my_array();
// Set the page size you'd like
$pageSize = 100;
// Leave these
$currentPage = 0;
$pages = ceil(count($skus) / $pageSize);
do
{
$start = $currentPage++ * $pageSize;
$page_items = array_slice($items, $start, $pageSize);
// Do stuff with $page_items
foreach($page_items as $page_item)
{
$this->doStuff($page_item)
}
} while ($currentPage < $pages);
/$items是要分页的数组
$items=$this->获取我的数组();
//设置您想要的页面大小
$pageSize=100;
//留下这些
$currentPage=0;
$pages=ceil(计数($SKU)/$pageSize);
做
{
$start=$currentPage++*$pageSize;
$page\u items=array\u slice($items,$start,$pageSize);
//使用$page\u项目进行操作
foreach($page\u项目作为$page\u项目)
{
$this->doStuff($page\u项目)
}
}而($currentPage<$pages);