drozwmi5440 2016-12-01 17:12
浏览 42
已采纳

从foreach生成的项目中获取变量

I'm generating a menu with list items generated from a foreach on a select query.

I'm printing the 'name' of each row. But, I also wanted to store some other data from that row inside variables so when I click on an <li> I would get those specific vars.

I would like to do this with Javascript so I don't reload the page.

Here's the code I have so far:

$query = "SELECT * FROM credenciais_sensores where ambiente = '1'";
$results = mysqli_query($conn, $query);

<ul class="treeview-menu">
  <?php foreach ($results as $result) {
    $local = $result['local'];
    $local = substr($local,0,7);
    echo "<li><a class='post' href='#'>".$local."</a></li>";
  }?>
</ul>

How can I achieve this?

  • 写回答

2条回答 默认 最新

  • dpb35161 2016-12-01 18:47
    关注

    Here's the code

    <ul class="treeview-menu">
       <?php
        foreach ($results as $result){
             $local = $result['local'];
             $local = substr($local,0,7); ?>
             <li><a class="post" href="#<?=$local;?>"><?=$local;?></a></li>
             <div id="<?=$local;?>">some details...</div>
        <?php } ?>
    </ul>
    
     <?php  foreach ($results as $result){
                 $local = $result['local'];
                 $local = substr($local,0,7); ?>
                 <div id="<?=$local;?>">some details...</div>
      <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题