简体   繁体   English

如何为触发JavaScript函数的按钮使用php foreach循环

[英]how to use php foreach loops for buttons that trigger JavaScript functions

I am using js and php to build an app. 我正在使用js和php来构建应用程序。 I've used a foreach loop in php to create buttons for each row fetched from mysql table. 我已经在php中使用了foreach循环为从mysql表获取的每一行创建按钮。 Each button has a unique value (row id). 每个按钮都有一个唯一的值(行ID)。

If someone clicks on of the buttons (each with unique id), there will be a total likes count that holds how many times a button is clicked. 如果有人点击按钮(每个按钮都有唯一的ID),那么总的点击次数将保存一次按钮被点击的次数。

If we have three buttons, and button 1 is clicked, then total likes will be increase by 1. if the same button is clicked again, then the value would decrease by 1. 如果我们有三个按钮,并且单击按钮1,则总点赞数将增加1。如果再次单击同一按钮,则值将减少1。

However, if two buttons of different values are clicked, the total likes add. 但是,如果单击两个具有不同值的按钮,则总喜欢数会增加。

When using js, I cant get each button id to pass to the js function loaded from the foreach loop. 使用js时,我无法获取每个按钮ID传递给从foreach循环加载的js函数。 only the last button value loads to the js function. 仅最后一个按钮值加载到js函数。

Here is what i've tried. 这是我尝试过的。 the js works but it doesnt apply to each button of the foreach loop. js可以工作,但不适用于foreach循环的每个按钮。

My Code 我的密码

        <?php foreach($values as $value){ 
        $value['button_id'];

        <!-- button --->
        <button id="button"    
           class="button-<?= $value['button_id']; ?>"    
           value="<?= $value['button_id']; ?>"  
           onclick="showUser(this.value)" >LIKE
        </button>
        <!-- button --->

        } ?>


        <script>
            var i = parseInt(document.getElementById('button').value, 10);
            var x = i;
            function showUser(this.value) {
            /// changing value //
            if (x == i){
            i++;
            document.getElementById('button').value = i;

            }
            else {

             i = x;
             document.getElementById('button').value = i;

            }   
          }
        </script>           

Here is an illustration explaining what I mean 这是一个说明我的意思的插图 在此处输入图片说明

Thanks in advance 提前致谢

 <script>
        var likes = new Array();

        function calc(value) {
            if(likes[value] == 0 || !likes[value]) {
                likes[value]=1;
            } else {
                likes[value]=0;
            }

        var sum=0;
        for(i=0; i<likes.length; i++) {

            if(likes[i]==1){ sum ++ }
        }

        document.getElementById("total").innerHTML = sum;
      }

    </script>  


    <div id=total>0 </div>
 <?php 
    $values = [1,2,3]; //here you can generate values

 foreach($values as $value){ 


    ?>
    <button id="button"    
       class="button-<?php echo $value; ?>"    
       value="<?php echo $value; ?>"  
       onclick="calc(<?php echo $value; ?>)" >LIKE
    </button>
    <?php

    } ?>
    <script>
        var i = parseInt(document.getElementById('button').value, 10);
        var x = i;
        function showUser(this.value) {
        /// changing value //
        if (x == i){
        i++;
        document.getElementById('button').value = i;

        }
        else {

         i = x;
         document.getElementById('button').value = i;

        }   
      }
    </script> 

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

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