我想在点击时显示更多商家信息

时间:2014-04-27 22:39:50

标签: php

我正在为一个课程项目做一个房地产的事情,我已经到了一个障碍。

<? 

    echo '<section id="listings">';
    echo "<h1>Browse our selection</h1>";

    $i = 0;
    $limit=5;

    foreach($data as $d){


        echo "<article>"; 
        echo "<div class='photo'> <img src='db/images/" . $d["img"] . ".jpg' alt='Nancy Stone Realty'> </div>";
        echo "<h5>$<strong>" . $d["price"] . "</strong><em>month</em></h5>";
        echo "<h3>" . $d["address"] . "</h3>";
        echo "<h4>" . $d["city"] . ", <em>FL. " . $d["zip"] . "</em></h4>";
        echo "<dl>
                  <dt>Bedrooms</dt>
                  <dd>"  . $d["bed"] . "</dd>
                  <dt>Bath</dt>
                  <dd>" . $d["bath"] . "</dd>
                  <dt>Pets</dt>
                  <dd>" . $d["pets"] . "</dd>
             </dl>";
        echo "<p>" . $d["description"] . "</p>";
        //echo "<a href='#' class='btn btn-primary btn-mini paulund_modal'>MORE</a>";
        echo " <a class='btn btn-primary btn-mini' href=?action=properties&ID="    .$d["ID"].">More</a>";
        echo "</article>";

        if (++$i == $limit) break;
    }


    echo "<form id='next5' method='POST' action=''>";
    echo "  <input type='submit' name='More'  value='More Houses'>";
    echo "</form>";

    if (isset($_POST['More'])) 
    { 
       $limit=$limit + 5;
       echo "$limit";
    } 

?>

我限制有多少人在5块中显示。我想当我按'更多'时限制增加5.我在最后打印限制以查看它是否正常工作并且在第一次显示后显示10点击以及所有未来的点击,所以我认为它不起作用。我也在想,我可能会在点击后将限制设置为+5,然后再返回默认值5。

做正在尝试的事情的正确方法是什么。

谢谢。

2 个答案:

答案 0 :(得分:1)

<? 

    echo '<section id="listings">';
    echo "<h1>Browse our selection</h1>";

    $i = 0;
    $step = isset($_REQUEST['step'])? $_REQUEST['step']: 0;
    $limit=5 + $step*5;

    foreach($data as $d){


        echo "<article>"; 
        echo "<div class='photo'> <img src='db/images/" . $d["img"] . ".jpg' alt='Nancy Stone Realty'> </div>";
        echo "<h5>$<strong>" . $d["price"] . "</strong><em>month</em></h5>";
        echo "<h3>" . $d["address"] . "</h3>";
        echo "<h4>" . $d["city"] . ", <em>FL. " . $d["zip"] . "</em></h4>";
        echo "<dl>
                  <dt>Bedrooms</dt>
                  <dd>"  . $d["bed"] . "</dd>
                  <dt>Bath</dt>
                  <dd>" . $d["bath"] . "</dd>
                  <dt>Pets</dt>
                  <dd>" . $d["pets"] . "</dd>
             </dl>";
        echo "<p>" . $d["description"] . "</p>";
        //echo "<a href='#' class='btn btn-primary btn-mini paulund_modal'>MORE</a>";
        echo " <a class='btn btn-primary btn-mini' href=?action=properties&ID="    .$d["ID"].">More</a>";
        echo "</article>";

        if (++$i == $limit) break;
    }


    echo "<form id='next5' method='POST' action=''>";
    echo "  <input type='hidden' name='step'  value='".($step+1)."'>";
    echo "  <input type='submit' name='More'  value='More Houses'>";
    echo "</form>";

    if (isset($_POST['More'])) 
    { 
       echo "$limit";
    } 

?>

答案 1 :(得分:1)

每次页面加载PHP (在提交时发生)它会加载变量,就像编写页面时一样。当然你可以这样做:

session_start();
if(!isset($_SESSION['limit']))$_SESSION['limit'] = 0;
if(!isset($_SESSION['inc']))$_SESSION['inc'] = 0;
$_SESSION['limit'] += 5;

稍后在你的代码中,它就像:

if(++$_SESSION['inc'] === $_SESSION['limit'])