php - 根据html中的DIV ID设置php变量
问题描述
我试图找到一种从包含文件的 html 中获取 Div ID 值的方法,并将它们用于在 php if 语句中进行比较。我有一个名为 templates.php 的页面,它使用 href 值来获取某个模板,在本例中为 fullwidth.php页面模板。该功能工作正常:
<!-- Get value from href -->
<?php $value = isset($_GET['value']) ? $_GET['value'] : 1;
if($value == 1){
include 'class/fullWidth.php';
}else{
include 'class/halfWidth.php';
}
?>
一旦$value
指定了要加载的模板,我就会尝试$panelType
从面板 Div 中设置一个值,这样我就知道要保存到我的数据库中的面板类型。例如,在我的 fullwidth.php 页面上,我有一个全宽面板:
<div class="col-lg-12 fullWidth" id="full">
<div class="fullContent" style="background-color: white; height: 100%;">
</div>
</div>
所以回到templates.php,我会把它#full
作为div ID并用它来设置我的变量$panelType
。
这是我添加这个并尝试的地方:
<?php $value = isset($_GET['value']) ? $_GET['value'] : 1;
$panel = $html->find('div[id]'); //including this to pull in DIV IDs
if($value == 1){
include 'class/fullWidth.php';
if($panel == 'full'){
$panelType = 1;
}// including this block to say if the Div ID is 'full', set $panelType = 1
}else{
include 'class/halfWidth.php';
}
?>
但这会破坏页面,看起来像是一般的 php 或格式错误。我不确定我在这里到底做错了什么,也许有更好的方法来使用我想要的 php 变量的 div ID。最终我会扩展它,所以我只想知道如何修改它,只说“如果 Div ID 是这个,将 $panelType 设置为这个,等等。”
解决方案
您可以在全角模板中设置 $panel,例如:
$panel = "full";
<div class="col-lg-12 fullWidth" id="full">
<div class="fullContent" style="background-color: white; height: 100%;">
</div>
</div>
然后当您加载它时,您编写的第二个代码将起作用;)
但这只有在您可以手动设置面板的情况下才有效?如果还可以有其他“面板 ID”,那么这将不起作用,我认为您有多个 ID?
如果身份证总是满的,我不明白你为什么需要检查?
推荐阅读
- python - 如何创建两个注册系统?
- angular - 带有@Host参数的单元测试Angular 9组件无法解析参数
- azure-data-factory - 如何调试 DataFlow 异常
- django - 我在哪里可以保存可重用应用程序的设置数据
- azure-cognitive-search - 将 Azure SQL DB 数据自动同步到 Azure 搜索索引
- python - 为什么我不能在 tkinter 画布中绘制 numpy 矩阵?
- android - 如何旋转 TextView 而不仅仅是它的文本
- javascript - 控制台错误无法解构“el.position(...)”的属性“top”,因为它未定义。WordPress jQuery
- java - 在 Netty 中,我想验证证书是否由 CA 颁发
- python - 如何从 pytest 注入 pygame 事件?