【问题标题】:Automatic generation of <div> tag into Html webpage<div> 标签自动生成到 Html 网页中
【发布时间】:2015-12-27 15:45:15
【问题描述】:

我有这几行代码:

<div class="signals">
  <ul>
    <li><a href="#signal1" class="fancybox">First Signal</a></li>
    <li><a href="#signal2" class="fancybox">Second Signal</a></li>
    <li><a href="#signal3" class="fancybox">Third Signal</a></li>
  </ul>
</div>

<div id="signal1" style="display:none;">
  <p style="color:#fff">First comment for #signal1 id - it will open in a fancybox -.</p>
  <div id="signal2" style="display:none;">
    <p style="color:#fff">Second comment for #signal2 id - it will open in a fancybox -.</p>
  </div>
  <div id="signal3" style="display:none;">
    <p style="color:#fff">Third comment for #signal3 id - it will open in a fancybox -.</p>
  </div>

这里是 jsfiddle 代码:JsFiddle

现在,当我想显示不同的 cmets 时,我打开我的 html 文件并编辑“id #signal ,在需要时添加更多 id(或删除它们)。
问题是:信号可能比显示的三个多,甚至更少。
所以我的问题是:
有一种方法可以在第二张表中自动生成我需要的 div,我将在其中插入评论和所有 id? (一种后端)
例如:如果有一天我只需要 2 个信号,我将创建 #signal1 和 #signal2 div,我将插入 cmets 并保存第二张表。
当我这样做时,带有 html 内容的主工作表将显示 2 行“li”:

  • 第一信号
  • 第二个信号
  • 当我点击它们时,fancybox 将打开并显示评论,就像将代码放入 jsfiddle 中一样。

    我不太了解 php,但有一种方法可以使用它吗?还是有更好的方法?
    希望我能从你的帮助中学习。

    【问题讨论】:

    • 例如,您可以将它们存储在 db 中,当您需要一些 div 和 li 时,它们会自动作为您的 db,如果您想删除一些,只需从 db 中删除它,您就会得到想要的。
    • 使用 jquery,进行 ajax 调用并检索数据并填充您的列表。您将需要一个 php 文件,该文件可以首先访问数据库中的数据。网上有很多这样的例子,这里有一个:stackoverflow.com/questions/29204934/…

    标签: php html


    【解决方案1】:

    看来你需要 PHP foreach 循环。

    您可以将 cmets 写入 PHP 数组,然后使用 PHP foreach 读取它们。

    这里是示例代码:

    <?php
        $commentsLabels = array('First Signal','Second Signal');
        $commentsTexts = array('First comment for #signal1 id - it will open in a fancybox.','Second comment for #signal2 id - it will open in a fancybox.');       
        //You could use a 2D array to store both comments Labels and comments Texts in one array, but since you are new to PHP, I put them in 2 different arrays to make it easy.
        //You can add more comments texts/labels to the arrays in double/single quotes separated with ','
        //For now, the arrays are filled manually. in future, you can add a DB to store comments and fetch the values from there.
    
        $counter = 0; 
        //we need a counter to make the comment ids dynamic
    ?>
    
    <!--GENERATING LABELS-->
    <div  class="signals"> 
        <ul>
    
        <?php
        foreach ($commentsLabels as $commentLabel) { //loop starts
            $counter++; //increasing $counter by 1, for each iteration      
            //We have to add HTML tags outside the php code block.
        ?>
                <li><a href="#signal<?php echo $counter; ?>" class="fancybox"><?php echo $commentLabel; ?></a></li> 
        <?php
            } //loop ends       
        ?>  
    
        </ul>
    </div>
    
    <!--GENERATING POPUPS-->
    <?php
        $counter = 0; //reset counter
    
        foreach ($commentsTexts as $commentText) { //loop starts
            $counter++; 
    ?>
            <div id="signal<?php echo $counter; ?>" style="display:none;"><p style="color:#fff"><?php echo $commentText; ?></p></div>
    <?php
        } //loop ends
    ?>
    

    我希望这可以帮助您开始。 :-)

    【讨论】:

    • 伟大的代码融入了代码,我明白它是如何工作的!非常感谢!我试图在我发布的代码上使用您的代码。我用 .php 扩展名保存了文件并加载到我的虚拟主机上。不幸的是,它似乎什么也没有,也许我错过了什么?第二个问题:我可以将“生成弹出窗口”代码移动到辅助 php 表中,然后将其调用到第一个表中吗?无论如何,谢谢你,你给了我很大的帮助。
    • 不客气,安德里亚。那是一个快速编码,所以让我现在在我的服务器上运行它并让你知道。您也可以将弹出窗口放在另一个 php 文件中,并使用 php include(file) 函数从外部源加载它。
    • 哦,找到了。我错过了变量名“$cmetsLabel”和“$cmetsText”末尾的“s”(第 2 行和第 3 行)。它们应该是“$cmetsLabels”和“$cmetsTexts”。我已经编辑了它们,代码现在工作正常。答案中的代码也已修复以显示正确的变量名称。请在您的最后检查并确认。谢谢
    • 真的很抱歉耽搁了,我正在工作:)...我刚刚试了一下,它完美无瑕!对不起,我没有注意到's'错误。非常感谢!我将研究 (file) 函数,并将尝试将 php 弹出内容导出到另一张表中。我的目标是在这个辅助 php 表上创建一个未来的“前端”,所以我想将两者分开。再次。非常感谢您的支持和解释
    • 不客气,安德里亚。如果这解决了您的问题,如果您标记答案,我将不胜感激。问候
    猜你喜欢
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多