Jump to content

Get number of <LI> on page & output result


doa24uk

Recommended Posts

Hey guys,

 

Lets say this is a portion of my HTML output page

 

<ul>
<li>Listing One</li>
<li>Listing Two</li>
<li>Listing Three</li>
</ul>

 

Here's my psuedo code for what I want to do.

 


$text to find = "Listing Three";

if $text to find = found {

echo Found in position 3.
}
else Sorry, text not found

 

I do have a Javascript script that will number the results ...

 

javascript:var%20p=document.getElementsByTagName('li');var%20j=1;function%20gc(){for(i=0;i<p.length;i++){if(p[i].className=='g'){p[i].innerHTML=j+'.%20'+p[i].innerHTML;j++;}};};gc();

 

But I've got no idea if I have to pre-number the results in PHP or if PHP can just find it straight off.....

Link to comment
https://forums.phpfreaks.com/topic/195447-get-number-of-on-page-output-result/
Share on other sites

Well, this is a tricky question.

 

First - your best bet is using javascript...use jQuery (www.jquery.com) and learn the basics of the framework.  You will be able to solve this problem in 30 min tops.

 

Now, if you are echoing out these <li> in PHP (say in a loop) then you can just count the number of times you are looping.

 

If this is an HTML file, and you want PHP to do some processing...then you'd have to load the HTML file into PHP, then run some regex to pull those results.

 

 

Trust me tho, for what you want to do it sounds like javascript is giong to be your best option...and jquery is the golden ticket.

 

Let me know if you have any questions.

 

(The jquery code for what you'd want to do)

 

$("li").each(function(){

  if ($(this).text == "Listing Three)

  {

    alert('listing three is on page');

  }

});

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.