php - Pagination inside foreach loop

994

I try create pagination inside loop foreach for show results , the problem no works for me , i create this little script :

foreach ($array as $pag_id=>$array_2)
{


$results_by_page="2";

if ($_REQUEST['page']=="")
{
$pag_ini=0;
$pag_end="$results_by_page";
}

if ($_REQUEST['page']=="0")
{
$pag_ini=0;
$pag_end="$results_by_page";
}


if ($_REQUEST['page']=="1")
{
$pag_ini="".$results_by_page."";
$pag_end="".($results_by_page+$pag_ini)."";
}

if ($_REQUEST['page']>"1")
{

$pag_end="".($_REQUEST['page']*$results_by_page)."";
$pag_ini="".($pag_end-$results_by_page)."";

}


if ($pag_id==$pag_ini && $pag_id<=$pag_end)
{
break;
}



}

I don´t know if it´s possible create paginaton using this loop buy i need use it

With loop for no give me problem do this , but in this case i need use foreach

Also if exists other method for pagination inside this kind of loop you can put here

Thank´s for community the help

728

Answer

Solution:

If you had it working before with afor loop, why not do this:

$i = 0;
foreach ($array as $pag_id=>$array_2)
{
    // Your existing code
    //Do whatever you were doing with $i before
    $i++;
}

You then can do what you were doing in thefor loop while keeping the benefits of theforeach loop.

People are also looking for solutions to the problem: mysql - PHP optimize website language Pack

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.