简体   繁体   English

使用php计算无序列表中列表项的总数

[英]count total number of list items in unordered list using php

I want to count the total number of items within an ordered list, i though this would be a fairly common question on Google but apparently not. 我想计算订购清单中的物品总数,尽管这在Google上是一个相当普遍的问题,但显然不是。

I have tried using PHP's count function but im not sure if it works for lists as it appears to be only relevant to PHP arrays. 我试过使用PHP的count函数,但是我不确定它是否适用于列表,因为它似乎仅与PHP数组有关。

echo '<p>' . count($TheList, 1) . '</p>';

The total of the list should appear in a paragraph within a Div lower down my page but it throws an error. 列表的总数应显示在我页面下方的Div内的一个段落中,但会引发错误。

The list looks like this: 该列表如下所示:

echo '<ul id="TheList" >';
echo '<li> peas </li>';
echo '<li> carrots </li>';
echo '<li> onions </li>';
echo '</ul>';

Really all i am trying to do is count how many items are in the unordered list. 我真正想做的就是计算无序列表中有多少项。

If you're trying to count DOM elements, you should use an HTML parser. 如果要计算DOM元素,则应使用HTML解析器。 Here's an example with DOMDocument 这是DOMDocument的示例

$html = '<ul id="TheList">
<li> peas </li>
<li> carrots </li>
<li> onions </li>
</ul>';

$doc = new DOMDocument();

$doc->loadHTML($html);

print $doc->getElementsByTagName('li')->length;

OUTPUT: 输出:

3 3

This works even if the list items have attributes (ids, classes, etc...) or strange formats. 即使列表项具有属性(ID,类等)或奇怪的格式,此方法也有效。 Example: 例:

$html = '<ul id="TheList">
<li      > peas </li>
<li id="foo"> carrots </li>
<li class="bar"> onions </li>
</ul>';

Did you try using javaScript? 您尝试使用javaScript吗?

var theList = document.getElementById("TheList").getElementsByTagName("li");
var numberOfItems = theList.length;

As others have pointed out, there are more appropriate ways to do this, but in the intrests of learning, here is a way you could use php to count the number of list items in a string of html code... 正如其他人指出的那样,还有其他更合适的方法可以做到这一点,但是在学习的兴趣上,这是一种您可以使用php来计数一串html代码中列表项的数量的方法...

$liststring = "<ul id=\"TheList\" >"
    . "<li> peas </li>"
    . "<li> carrots </li>"
    . "<li> onions </li>"
    . "</ul>";


$numitems = substr_count($liststring, "<li>");

echo "<p>$numitems</p>";

echo $liststring;

if you're trying to process html code, you could try to do this: 如果您尝试处理html代码,则可以尝试执行以下操作:

function count_list($list){
   $array = explode("</li>",$list);
   return count($list)-1;
}

PHP is Server Side while HTML is presentation at Client Side. PHP是服务器端,而HTML是客户端。

If you generate those li elements from a PHP script, than you can just save the count when echoing the results and echo the Count of it everywhere you want. 如果从PHP脚本生成这些li元素,则可以在回显结果时保存Count ,并在需要的任何地方回显Count

I'm guessing you're not using some PHP script in order to generate and echo the list, therefore you can use JavaScript to help you solve this case. 我猜您不是在使用某些PHP脚本来生成和回显列表,因此您可以使用JavaScript来解决这种情况。

JavaScript code: JavaScript代码:

<html>

    <head>
            <title></title>
    </head>

    <body>
        <p id="TheList_Count">0</p>
        <ul id="TheList">
            <li> peas </li>
            <li> carrots </li>
            <li> onions </li>
        </ul>

        <script>
            document.getElementById("TheList_Count").innerHTML = document.getElementById("TheList").children.length;
        </script>
    </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM