1

I have custom HTML pagination template:

<ul class="number-list">       
    <li><a href="/article/" class="page-number js-page-filter " data-page="1">1</a></li>
    // Current page with class [.red]
    <li><a href="/article/?page=2" class="page-number js-page-filter red" data-page="2">2</a></li>
    <li><a href="/article/?page=3" class="page-number js-page-filter " data-page="3">3</a></li>
    <li><a href="/article/?page=4" class="page-number js-page-filter " data-page="4">4</a></li>
    <li><a href="/article/?page=5" class="page-number js-page-filter " data-page="5">5</a></li>
    <li><span class="page-number">...</span></li>
    <li><a href="/article/?page=31" class="page-number js-page-filter " data-page="31">31</a></li>
    <li><a href="/article/?page=32" class="page-number js-page-filter " data-page="32">32</a></li>
</ul>

I can get current page url using this code:

$current = $doc->find('.number-list li a.red')->attr('href');

How I can get next page url in my case?

I tired:

$next = $doc->find('.number-list li a.red')->next('a')->attr('href');
$next = $doc->find('.number-list li a.red')->next('li a')->attr('href');
$next = $doc->find('.number-list li a.red')->next('.page-number')->attr('href');
$next = $doc->find('li a.red')->next('li a')->attr('href');
$next = $doc->find('li a.red')->next('li .page-number')->attr('href');

1 Answer 1

1

Andreas. You can get all <li> and check if it <a> has class red. This element will be your current, after that, you can get the next element. Hope it helps you.

$document = phpQuery::newDocument('
<ul class="number-list">       
    <li><a href="/article/" class="page-number js-page-filter " data-page="1">1</a></li>
    <li><a href="/article/?page=2" class="page-number js-page-filter red" data-page="2">2</a></li>
    <li><a href="/article/?page=3" class="page-number js-page-filter " data-page="3">3</a></li>
    <li><a href="/article/?page=4" class="page-number js-page-filter " data-page="4">4</a></li>
    <li><a href="/article/?page=5" class="page-number js-page-filter " data-page="5">5</a></li>
    <li><span class="page-number">...</span></li>
    <li><a href="/article/?page=31" class="page-number js-page-filter " data-page="31">31</a></li>
    <li><a href="/article/?page=32" class="page-number js-page-filter " data-page="32">32</a></li>
</ul>
');

$lis = $document->find('.number-list li');
foreach ($lis as $li)
{
    if(pq($li)->find('a')->hasClass('red'))
    {
       var_dump(pq($li)->next()->find('a')->attr('href')); // get the next li and it a tag
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answer ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.