在foreach循环外访问变量

时间:2018-01-26 19:36:51

标签: php html foreach

编辑:通过添加模态内循环解决

有人可以向我解释如何访问class Shape { protected: int shape = 10; public: int get() { return shape; } virtual void process() { shape = 30; } }; class Rectangle : public Shape { public: void process() { shape = 40; } }; class Object : Shape { Shape m_sh; public: Object(Shape &sh) { m_sh = sh; m_sh.process(); } int getShape() { return m_sh.get(); } }; int main(int argc, char *argv[]) { Rectangle rect; Object obj1 = Object(rect); std::cout << obj1.getShape() << std::endl; // am expecting 40, but getting 30. } 之外的foreach循环变量吗?

foreach

例如,我想在我的模态中使用<?php foreach($sup as $data){ ?> <tr> <td><i class="circular blue lock icon"></i><?php echo $data['id']; ?></td> <td><div class="ui large green label"> Solved! </div></td> <td><?php echo $data['subject']; ?></td> <td><?php echo $data['text']; ?></td> <td><?php echo $data['date']; ?></td> <td> <div class="ui buttons"> <input type="hidden" name="id" value="<?php echo $data['id']; ?>" /> <input type="submit" name="cancel" value="Lock" class="ui button" tabindex="5"> <div class="or"></div> <button id="sup" class="ui right labeled icon positive button"> Reply </button> </div> </td> </tr> <?php } ?>

1 个答案:

答案 0 :(得分:0)

将数据属性添加到按钮:

<button data-text="<?php echo $data['text']; ?>" class="ui right labeled icon positive button">

然后,在您的模态中,您可以使用Javascript访问它,如下所示:

var text = document.getElementById('sup').dataset.text

此外,您不能在foreach循环中使用唯一ID,否则它将与其余代码一起复制。删除&#34; id&#34;按钮中的字段。