简体   繁体   English

将一个功能更改为第二个按钮单击

[英]change one function into second in button click

Hi I have array with years in my controller: 嗨,我的控制器中有年份数组:

public function getNextYear() {

    $years = $this->getYears();
    foreach ($years as $key => $value) {
        $y[] = $value + 1;
    }
    return $y;
}

I display this in my index: 我将其显示在索引中:

  <?php foreach ($years as $year): ?>

        <div class="">

            <?= $year; ?><br>

        </div>

    <?php endforeach; ?>
<?php endif; ?><br>

It is possibe to add one year using only php? 仅使用php添加一年是可能的吗?

Nope. 不。 You will need use Ajax to load more content into your view. 您将需要使用Ajax将更多内容加载到视图中。

Also, you will check if there is more years to load, then display a button that calls a Ajax with Javascript/Jquery. 另外,您将检查是否还有更多的加载时间,然后显示一个使用Javascript / Jquery调用Ajax的按钮。 This request will put content directly into you page, with means, your function getNextYear(), must return a Json or Html content. 该请求会将内容直接放入您的页面,这意味着您的函数getNextYear()必须返回Json或HTML内容。

If the result is a Json, in AJax callback, format in HTML. 如果结果是Json,则在AJax回调中以HTML格式设置。

After all this, along with the button, put a div, with some class to put more content: 所有这些之后,连同按钮一起,放置一个div,并添加一些类来放置更多内容:

<button id="callAjaxNextYears"></button>
<div class="nextYears"></div>

Don't forget, in your ajax send the last year showed. 别忘了,在您的ajax发送中显示了去年。 Then you can use something like: 然后,您可以使用类似:

<button value="2012"></button>

And in your function, must to recognize a _GET or _POST or the the param: 并且在您的函数中,必须识别_GET或_POST或参数:

getNextYear($start){}

As better practices, all this list of years must be implemented by ajax, to avoid a standard code in your project. 作为更好的做法,所有这些年份列表都必须由ajax实现,以避免在项目中使用标准代码。 Or, you can learn more about "AngularJS". 或者,您可以了解有关“ AngularJS”的更多信息。


Try change your PHP function to work better with ajax call: 尝试更改您的PHP函数,使其更好地与ajax调用配合使用:

public $actualYear;
public $maxYear = 2030; // Just a exemple, otherwhise, will create infinite years list

public static function getYears() {

        if(isset($this->actualYear)){
            $nowYear = $this->actualYear;
        } else {
            $nowYear = date("Y");
            $this->actualYear = 2010;
        }

        if($nowYear < $maxYear){

            $i = 0;
            for ($yearNum = $this->actualYear; $yearNum <= $nowYear; $yearNum++) {
                $year[] = $yearNum;
                $i++;
                if ($i == 3)
                    break;
            }

        }

        return $year;
    }

public function getNextYear() {

    $this->actualYear = $_POST['start']; // Wherever your ajax has sended, can be a _GET as well

    $years = $this->getYears();
    foreach ($years as $key => $value) {
        $y[] = $value + 1;
    }
    return $y;
}

If you chose not use ajax, try this: 如果您选择不使用ajax,请尝试以下操作:

<a href="?start=2012">Load more years</a>

Then in controller: 然后在控制器中:

public $maxYear = 2030; // Just a exemple, otherwhise, will create infinite years list

public static function getYears() {

        if(isset($_GET['start'])){
            $nowYear = $_GET['start'];
        } else {
            $nowYear = date("Y");
            $this->actualYear = 2010;
        }

        if($nowYear < $maxYear){

            $i = 0;
            for ($yearNum = $this->actualYear; $yearNum <= $nowYear; $yearNum++) {
                $year[] = $yearNum;
                $i++;
                if ($i == 3)
                    break;
            }

        }

        return $year;
    }

暂无
暂无

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

相关问题 如何更改 $(&quot;#button&quot;).one(&quot;click&quot;, first ); 到 $(&quot;#button&quot;).one(&quot;click&quot;,second); 通过点击一个按钮 - how to change $("#button").one("click", first ); to $("#button").one("click",second); by clicking on a button 如何在单击时调用一个 function 但在第二次单击(相同按钮)时调用另一个 function? Angular - How to call one function at click but on second click (same button) call another function? Angular 有2个页面,如何通过第一个按钮上的click事件更改第二个页面的内容? - Having 2 pages, How to change the content of a second page by a click event on a button in the first one? 第二次单击按钮时未调用按钮单击功能 - Button click function not being called the second time of button click 第二次单击时jQuery禁用按钮功能 - jQuery Disable Button function on second click 第二次单击按钮时调用的函数(不是第一次单击) - Function called on second click of button ( not on first ) 第二次单击时具有不同功能的按钮并更改文本 - Button with different function on second click AND changes text 第二次单击后触发按钮的功能 - Button's function triggered after second click jquery - 单击时更改按钮的文本仅在第二次单击时起作用 - jquery - change button's text on click works only on second click 我无法在按钮的第二次单击上使用第二个功能 - I can’t get the second function to work on the second click of the button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM