在 PHP 中的容器中创建列表


Creating list within a container in PHP

我尝试在PHP的容器中创建列表,但我很挣扎,因为这是我第一次用PHP做列表。我希望容器中的列表看起来像我在"列表组"下找到的示例 http://getbootstrap.com/components/#list-group 然后它是"链接项"示例。

我的代码

// List
      echo_lines(array(
// All cases
    "<ul>", 
      "<div class='row'>",
      "<div class='col-md-6'>",
      "<div class='panel panel-default'>",
      "<div class='panel-heading clearfix'>",
      "<h4 style='margin:0; margin-top:5px; padding:0;'>All Projects</h4>",
      "</div>",
        "<div class='panel-body'>",
        // Content
        "<div class='list-group'>"
          "<a href='#' class='list-group-item'>"Orange"</a>"
          "<a href='#' class='list-group-item'>"Pizza"</a>"
          "<a href='#' class='list-group-item'>"Beef"</a>"
          "<a href='#' class='list-group-item'>"Chicken"</a>"
    "</ul>",

你给我们的代码很混乱。您没有关闭大多数html标签,我不确定您要实现什么

下面是您链接的引导文档中的示例,或多或少与您的代码混合在一起。我仍然不确定"橙色"、"牛肉"、"披萨"和"鸡肉"是否只是字符串,或者您想使用变量(因为您在代码中转义了它们)

$str = "";
$str .= '<div class="list-group">';
$str .= '   <a href="#" class="list-group-item active">Orange</a>';
$str .= '   <a href="#" class="list-group-item">Beef</a>';
$str .= '   <a href="#" class="list-group-item">Pizza</a>';
$str .= '   <a href="#" class="list-group-item">Chicken</a>';
$str .= '   <a href="#" class="list-group-item">' . $variable . '</a>'; // this is an example, if you want to use variable
$str .= '</div>';
echo $str;

如果要在字符串中使用"',则必须使用反斜杠'对其进行转义,具体取决于字符串的写入方式。

例:

$str = "";
$str .= 'Here you can use "double quotes" without escaping<br />';
$str .= "There you will need to escape '"double quotes'"<br />";
$str .= 'There you will need to escape ''simple quotes''<br />';
$str .= "There, escaping 'simple quotes' is unnecessary";

更多信息在 PHP : 字符串手册

PHP中,您必须使用 . 运算符将变量或常量连接到字符串中,并且您忘记将>保留在引号内 ( " ):

      "<a href='#' class='list-group-item'>". Orange. "</a>"
      "<a href='#' class='list-group-item'>". Pizza. "</a>"
      "<a href='#' class='list-group-item'>". Beef. "</a>"
      "<a href='#' class='list-group-item'>". Chicken. "</a>"

另外,为什么有时使用逗号,有时不使用逗号?

使用 php 数组:

<?php
$arr_foods = array('Orange', 'Pizza','Beef', 'Chicken');
$str_list_group = '';
foreach($arr_foods as $str_food) 
{
//Concatenation
    $str_list_group =.  "<a href='#' class='list-group-item'>". $str_food. "</a>'n";
}
print $str_list_group;
?>