【问题标题】:Creating list within a container in PHP在 PHP 中的容器内创建列表
【发布时间】:2014-12-02 13:16:06
【问题描述】:

我曾尝试在 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>",

【问题讨论】:

  • 您将项目分开,直到到达 list-group div,这是怎么回事?
  • 您还缺少每个 div 的结束标签。
  • 页面的其余部分是用 PHP 完成的......我让容器工作,直到我添加内容
  • ul 的孩子必须是li;您的 "Orange" 引用打破了您的 php 行,还遗漏了一些冒号
  • 但我在 /Applications/XAMPP/xamppfiles 中收到此错误消息“语法错误,意外 '”

标签: javascript php list echo hybrid-mobile-app


【解决方案1】:

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

这是您链接的引导文档中的示例,或多或少与您的代码混合在一起。 我仍然不确定“Orange”、“Beef”、“Pizza”和“Chicken”是否只是字符串,或者您想使用变量(因为您在代码中对它们进行了转义)

$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: Strings manual

【讨论】:

    【解决方案2】:

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

          "<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>"
    

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

    【讨论】:

    • 我仍然在 /Applications/XAMPP/xamppfiles/htdocs 中收到“语法错误,意外 '”
    • @TheGarrett 我建议您像 其他人 那样做,并在不使用数组的情况下正确地回显它,一次尝试一行并连接字符串。
    • @Jonast92 我怀疑 OP 知道常量,更不用说变量了。我认为他真的只是想要它们作为文字字符串,并且混淆/弄乱了"&lt;a href='#' class='list-group-item'&gt;Orange&lt;/a&gt;"的格式@
    • 我从一个示例中复制了它...如果您已阅读问题,我刚刚更改了文本(列表的内容)...
    【解决方案3】:

    使用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;
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-03-05
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 2019-12-11
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      相关资源
      最近更新 更多