html页面怎么循环显示,在html中显示来自foreach循环的5个结果(Show 5 Results from foreach loop in html)...

在html中显示来自foreach循环的5个结果(Show 5 Results from foreach loop in html)

嗨,我想用我的foreach循环对我的数组进行一些分页,所以我这样做了:

$page = 0;

$i = 0;

foreach($time as $indTime){

if($i % 5 == 0){

$page++;

}

$i++;

}

所以基本上前5个结果页= 1下5页= 2下5页= 3等等。 现在我使用twitter bootstrap,我想使用基本上与<< 1 2 3 4 5 >>链接的分页组件来点击你的页面。 我需要的是当你点击1时,它会显示来自foreach循环的每个结果,当我点击2,$ page = 2时,$ page = 1等等。任何人都知道我该怎么做?

Hi I want to do some pagination with my array with my foreach loop so I did this:

$page = 0;

$i = 0;

foreach($time as $indTime){

if($i % 5 == 0){

$page++;

}

$i++;

}

So basically the first 5 result page = 1 the next 5 page=2 the next 5 page = 3 and so on. Now Im using twitter bootstrap and I want to use the pagination component which is basically links with << 1 2 3 4 5 >> to click on your page. What I need is when you click on 1, it shows every result from the foreach loop where $page = 1 when I click 2, $page = 2 etc. Anyone know how I could do this?

原文:https://stackoverflow.com/questions/21213164

更新时间:2020-01-19 00:52

最满意答案

资源的路线

您必须为分页创建路径,例如: example.com/times/2其中2将是页码,因此您可以在链接使用它们。 然后从URL中获取数字 。 或者,您可以将数字作为GET或POST参数传递。

验证号码

您无法信任用户输入,因此请检查给定的数字是否有效。

打印页面

最后,只需将页面与给定/选定的页码进行比较,然后打印项目。

$time = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

$page_selected = 2;

$page = 1;

$i = 1;

foreach ($time as $indTime) {

if ($page == $page_selected) {

echo $indTime;

echo ' ';

}

if ($i % 5 == 0) {

$page++;

}

$i++;

}

这将打印第二页的项目: 6 7 8 9 10 。

Route for the resource

You have to create routes for the pagination like: example.com/times/2 where 2 will be the page number, so you can use them in your links . Then get the number from the URL. Alternatively you can just pass the number as a GET or POST parameter.

Validate the number

You cannot trust user input, so check if the given number is valid.

Print the page

Finally just compare the page with the given/selected page number and print the items.

$time = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

$page_selected = 2;

$page = 1;

$i = 1;

foreach ($time as $indTime) {

if ($page == $page_selected) {

echo $indTime;

echo ' ';

}

if ($i % 5 == 0) {

$page++;

}

$i++;

}

This will print the items of the second page: 6 7 8 9 10.

相关问答

你可以这样: <?php foreach($data as $key => $row): $row2 = $data2[$key]; ?>

//html goes here

这将有效地使您能够同时循环两个数组。 You could to something like this: <?php foreach($data as $key => $row): $row2 = $data2[$key]; ?>

//html goes here

...

实际上,你并没有被困在.each()循环中,你被困在了.text() 。 .text()用新文本替换当前文本,所以在这种情况下你只能看到最后一个文本。 也许使用.append() 。 你也可以用这种方式使用.text() : $('.playlist1').text(function(index, text){

return text + item.title;

});

假设.playlist1是ul或ol ,你可以像这样追加它: $('.playlist1').append('

'+

...

合并pieces数组时,请尝试使用array_merge_recursive ,因为array_merge会覆盖前一个数组中的键。 在两种情况下使用相同的密钥 ,即0和1 。 array_merge_recursive创建新的keys 。 https://3v4l.org/RBZDo 更新 相反,将碎片连接到每片水果,并将其从阵列中拉出。 $fruit = array();

$fruit[] = array('id' => 1, 'name' => 'Banana 1', 'pieces' =>

...

City:

Any

sort($city_list); //

foreach($city_list as $city) : ?>

分类 City:

使用它们。 然后从URL中获取数字 。 或者,您可以将数字作为GET或POST参数传递。 验证号码 您无法信任用户输入,因此请检查给定的数字是否有效。 打印页面 最后,只需将页面与给定/选定的页码进行比较,然后打印项目。 $time = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

...

您可以对最后的项目使用带负值的Array#slice 。 array.slice(-5).forEach()

You could use Array#slice with negative value for the last items. array.slice(-5).forEach()

您应该使用System.Diagnostic.StopWatch类而不是使用DateTime.Now进行适当的时间比较 You should be using System.Diagnostic.StopWatch class instead of using DateTime.Now for proper time elapsed comparison

你可以尝试类似的东西: - DataTable dt = new DataTable();

foreach (string word in words)

{

SqlCommand cmd= new SqlCommand("select * from skills where (skills like '%"+word+"%')",con);

SqlDataAdapter da = new SqlDataA

...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值