Iterating and printing content

22 views
Skip to first unread message

Francesco

unread,
Nov 24, 2009, 10:36:04 PM11/24/09
to phpQuery
Hi, I use jQuery very much and now that I've discovered this class I
like this too, thanks for developing it :)

I'm trying to access content of some links, one by one, but it seems
I'm not managing to do it right. Example html:

..
<ul>
<li><a href="#1">title 1</a></li>
<li><a href="#2">title 2</a></li>
<li><a href="#3">title 3</a></li>
</ul>
..

Using pq("ul li a") will dump "title 1 title 2 title 3"

I need to access title x one by one. I tried:

foreach(pq("ul li a") as $elem)
print $elem->html()

But it didn't work, because $elem is a DOMElement and I didn't find a
method for doing such a basic thing. Are there any workaround?

Thanks very much :)

Tobiasz Cudnik

unread,
Nov 25, 2009, 5:12:45 AM11/25/09
to phpQuery
You should do it just like in jQuery:

foreach(pq("ul li a") as $elem)
print pq( $elem )->html();

Check examples on project's main page.

Just

unread,
Nov 25, 2009, 5:13:46 AM11/25/09
to phpQuery
Hi Francesco,

You could try to use the each iteration perhaps :
http://code.google.com/p/phpquery/wiki/Utilities
or you have to do a "$var = pq($elem);" in your foreach.

On Nov 25, 4:36 am, Francesco <ban...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages