Reputation: 3253
I have a small problem maintain a count for the position. i have written a function function that will select all the users within a page and positions them in the order.
Eg:
Mike Position 1
Steve Postion 2..............
....
Jacob Position 30
but the problem that i have when i move to the second page, the count is started from first Eg: Jenny should be number 31 but the list goes,
Jenny Position 1
Tanya Position 2.......
Below is my function
function nrk($duty,$page,$position)
{
$url="http://www.test.com/people.php?q=$duty&start=$page";
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
$result=curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($result);
$xpath=new DOMXPath($dom);
$elements = $xpath->evaluate("//div");
foreach ($elements as $element)
{
$name = $element->getElementsByTagName("name")->item(0)->nodeValue;
$position=$position+1;
echo $name." Position:".$position."<br>";
}
return $position;
}
Below is the for loop where i try to loop thru the page count
for ($page=0;$page<=$pageNumb;$page=$page + 10)
{
nrk($duty,$page,$position);
}
I dont want to maintain a array key value in the for each coz i drop certain names...
Upvotes: 0
Views: 154
Reputation: 23978
Or you can store the counter in a session variable so that is retrievable from any point of the site.
Upvotes: 0
Reputation: 459
If my understanding of question is correct, then you need to pass $position variable by a reference, like:
for ($page=0;$page<=$pageNumb;$page=$page + 10)
{
nrk($duty,$page,&$position);
}
Upvotes: 0
Reputation: 454970
You are modifying the value of position
in the function nrk
and returning the changed value which is not being assigned back to position
in the loop. So you need to do:
$position = nrk($duty,$page,$position);
or you can avoid returning the modified value and pass the variable position
by reference.
Upvotes: 0
Reputation: 8826
$position = $position + ($page - 1) * $count_per_page;
//In your case $count_per_page == 30;
Upvotes: 1
Reputation: 7997
Pass as a paremeter of the second page an offset, let's say 30. So you position will be position+offset+1
Upvotes: 0