首页 > 解决方案 > 根据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 设置为这个,等等。”

标签: phphtml

解决方案


您可以在全角模板中设置 $panel,例如:

$panel = "full";
<div class="col-lg-12 fullWidth" id="full">
    <div class="fullContent" style="background-color: white; height: 100%;">

    </div>
</div>

然后当您加载它时,您编写的第二个代码将起作用;)

但这只有在您可以手动设置面板的情况下才有效?如果还可以有其他“面板 ID”,那么这将不起作用,我认为您有多个 ID?

如果身份证总是满的,我不明白你为什么需要检查?


推荐阅读