Master Yoda
Master Yoda

Reputation: 4412

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.

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.

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.

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.

Upvotes: 1

Views: 5026

Answers (5)

MrVimes
MrVimes

Reputation: 3312

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

$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;

Upvotes: 1

Subhashi
Subhashi

Reputation: 4081

Did you try using javaScript?

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

Upvotes: 1

Orel Eraki
Orel Eraki

Reputation: 12196

PHP is Server Side while HTML is presentation at Client Side.

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.

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.

JavaScript code:

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

Upvotes: 1

Martin149
Martin149

Reputation: 23

if you're trying to process html code, you could try to do this:

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

Upvotes: 1

Tivie
Tivie

Reputation: 18923

If you're trying to count DOM elements, you should use an HTML parser. Here's an example with 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

This works even if the list items have attributes (ids, classes, etc...) or strange formats. Example:

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

Upvotes: 4

Related Questions