简体   繁体   English

为阵列加载更多按钮

[英]Load more button for array

I have an array $users 我有一个数组$ users

$statement = $pdo->prepare("SELECT * FROM activity");
$statement->execute();
$users = $statement->fetchAll();

and I display it like so 我像这样显示

<?php
foreach ($users as $key => $row) {

    $dist = 0.0;
            $x1 = $lng;
            $x2 = $row['alng'];
            $y1 = $lat;
            $y2 = $row['alat'];

            $dist = acos(sin($x1=deg2rad($x1))*sin($x2=deg2rad($x2))+cos($x1)*cos($x2)*cos(deg2rad($y2) - deg2rad($y1)))*(6378.137);
            $distn = FLOOR ( ROUND($dist,1) * 2 ) / 2 ; //calculate distn

    $users[$key]['dist'] = $distn; //add dist to array foreach value
}

array_multisort(array_column($users, 'dist'), SORT_ASC, $users); / sort array with dist

foreach($users as $row) { 

?>

<article class="mainusers" id="actvtar">
    <div class="actvtinfo">
        <a class="actvtsnm" href="actvt.php?id=<?php echo ($row['aid']);?>"><?php echo $row['title']; ?></a>

    </div>

    <a class="titlepic" href="actvt.php?id=<?php echo ($row['aid']);?>">
            <img  class="actvtpb" src="./activitys/<?php echo ($row['title']); ?>/activitypic.jpg" alt="Bild nicht gefunden" onerror="this.src='./img/no_title.png';"></img>
        </a>

    <div class="actvtfooter">

        <p id="ua">Tags:</p>
        <p class="tags" id="actvttags"  name="interest"><?php echo $row['interest'];?></p>
        <p id="actvtsdist"><?php echo $row['dist']; ?>km entfernt</p>

    </div>

</article>

<?php }
?>

Now I want that the foreach loop is shown 5 times (5 items) and if I scroll to the bottom the next 5 are loaded. 现在,我希望foreach循环显示5次(5个项目),并且如果我滚动到底部,则会加载下5个循环。 Just like Twitter, Instagram etc... How can I do that? 就像Twitter,Instagram等...我该怎么做? I would appreciate it if you wouldnt mark this as duplicated because I searched for days now and I couldnt find an answear! 如果您不将其标记为重复,我将不胜感激,因为我现在搜索了几天,而我找不到一个天使装! Thanks for your help and have a great day :) 感谢您的帮助,祝您有美好的一天:)

Here is a general answer to your question. 这是您问题的一般答案。

First, what i would do is query for a limit of maybe 25 records and make your loop go over all 25. 首先,我要做的是查询最多25条记录的限制,并使您的循环遍历所有25条记录。

The goal here is to wrap each 5 records inside a div with a numbered id, so put a counter in your loop that does this 这里的目标是将每5条记录包装在具有编号ID的div内,因此将一个计数器放在循环中即可

ie

if ($count % 5 == 0) {
    echo ("</div><div id='mydiv_$myCounter'>");
}

See what i did there, i closed and then opened a new div so you'll have to start out with an opening div and end with a closing div. 看看我在这里做了什么,我关闭了,然后打开了一个新的div,所以您必须从一个开始的div开始,以一个关闭的div结束。

Next, you'll want to hide all but the first div using css display:none. 接下来,您将要使用CSS display:none隐藏除第一个div之外的所有div。 i'll let you figure that out. 我会让你知道。

And finally, once you have this, you'll need to write some javascript. 最后,一旦有了这个,就需要编写一些JavaScript。 The javascript (jQuery is great for this kind of thing) will need to look at the scroll event. javascript(jQuery非常适合这种事情)将需要查看scroll事件。 something like this: 像这样的东西:

$(document).ready(function() {
    $(window).scroll(function() {
      // do whatever you need here.
    });
});

Once you have that, figure out at what scroll locations you need to load more data and use jQuery to either make the existing divs visible or load more data from the backend using ajax. 了解这些内容后,找出需要在哪些滚动位置加载更多数据,并使用jQuery使现有div可见或使用ajax从后端加载更多数据。

Sorry i can't be more specific but this is basically how it's done. 抱歉,我不能更具体,但这基本上是完成的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM