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
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');

  }

});

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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