tags:

views:

44

answers:

5

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

A: 

Pass as a paremeter of the second page an offset, let's say 30. So you position will be position+offset+1

LucaB
+1  A: 
$position = $position + ($page - 1) * $count_per_page; 
//In your case $count_per_page == 30;
Petr Peller
A: 

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.

codaddict
pass-by-reference has been deprecated :( $position = nrk($duty,$page,$position); doesn't seem to e working.....
LiveEn
A: 

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);
        }
dchekmarev
I tried that earlier but it has been deprecated.. Deprecated: Call-time pass-by-reference has been deprecated
LiveEn
A: 

Or you can store the counter in a session variable so that is retrievable from any point of the site.

tunnuz