简体   繁体   English

如何传递一个PHP id来创建两个bootstrap button diffrent id并将其用于jquery functin?

[英]How to pass a PHP id to create two bootstrap button diffrent id's and use its for jquery functin?

<?php
    include_once('config.php');
    $sql="SELECT * FROM events";                                                         
    $result = mysqli_query($con,$sql);
    if(!$result)
    {
     echo "no record found";
    }
    else
    {                                                                   
        while ($row =mysqli_fetch_assoc($result))
        {
         $e_id=$row['e_id'];         
    ?>
                    <?php 
                        $_SESSION['r']=$e_id."rr";
                        $_SESSION['l']=$e_id."ll";
                    ?>
                <p>
                    <a class="btn btn-default" id="<?php echo $_SESSION['r']; ?>" role="button" data-toggle="collapse" href="#<?php echo  $e_id; ?>" aria-expanded="false" aria-controls="<?php echo  $e_id; ?>">View More &raquo;</a>
                    <a class="btn btn-default" id="<?php echo  $_SESSION['l']; ?>" role="button" data-toggle="collapse" href="#<?php echo  $e_id; ?>" aria-expanded="false" aria-controls="<?php echo  $e_id; ?>">View Less &laquo;</a></br>
                </p>



                 <script>
                 var l = "<?php echo $_SESSION['l'];?>";
                 var r = "<?php echo $_SESSION['r'];?>";
                 alert(r);
                    $(document).ready(function(){
                      $("#"+r).show();
                      $("#"+l).hide();

                      $("#"+l).click(function(){
                        $("#"+l).hide();
                        $("#"+r).show();
                      });

                      $("#"+r).click(function(){
                        $("#"+r).hide();
                        $("#"+l).show();
                      });
                     });
                 </script>


<?php        
        }//while

    }//else 
 ?>

I fetch data $e_id from database then use it to create two buttons with added char to differ rr and ll as their id's then store in session. 我从数据库中获取数据$ e_id,然后使用它创建两个按钮,其中添加了char,以将rr和ll更改为id,然后将其存储在会话中。 Now how to use it in jquery to show/hide each one. 现在如何在jquery中使用它来显示/隐藏每一个。 data-toggle on button is different div works fine. 按钮上的数据切换是不同的div工作正常。 If i alert(r) gives 1rr 2rr 3rr, and alert(l) 1ll 2ll 3ll. 如果我alert(r)给出1rr 2rr 3rr,而alert(l)给出1ll 2ll 3ll。 I have 1,2,3 as id in database how can i use this button id's in script. 我在数据库中有1,2,3作为ID,我该如何在脚本中使用此按钮ID。 session_start() was on top of page. session_start()位于页面顶部。 The problem is hide/show not working guess I'm doing wrong. 问题是隐藏/显示无法正常工作,猜测我做错了。 How can I call that id's properly? 我怎样才能正确地称呼那个ID?

You can do it this way. 您可以这样进行。 The script is outside the loop. 该脚本不在循环中。 I can also add in a non jquery version in a bit as well if you want. 如果需要,我也可以再添加一个非jquery版本。

<?php
include_once('config.php');

$sql = "SELECT * FROM events";
$result = mysqli_query($con, $sql);

if(!$result)
    exit('no record found');

while($row = mysqli_fetch_assoc($result)) { ?>
    <p>
        <a class="btn btn-default button-right" id="e-left-<?=$row['e_id']; ?>" data-id="<?=$row['e_id']; ?>" role="button" data-toggle="collapse" href="#<?=$row['e_id']; ?>" aria-expanded="false" aria-controls="<?=$row['e_id']; ?>">View More &raquo;</a>
        <a class="btn btn-default button-left" id="e-right-<?=$row['e_id']; ?>" data-id="<?=$row['e_id']; ?>" role="button" data-toggle="collapse" href="#<?=$row['e_id']; ?>" aria-expanded="false" aria-controls="<?=$row['e_id']; ?>">View Less &laquo;</a></br>
    </p>
<?php } ?>

<script>
    $(document).ready(function() {

        $('.button-right').each(function() { 

            $(this).show(); 

            $(this).click(function() {
                $(this).hide();
                $('#e-left-'+this.dataset.id).show();
            }); 
        });

        $('.button-left').each(function() {

            $(this).hide();

            $(this).click(function() {
                $(this).hide();
                $('#e-right-'+this.dataset.id).show();
            }); 
        });
     });
</script>

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

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