tags:

views:

45

answers:

2

I have a function below which gets name from a site. Below is the partial code, not complete. The values are passed thru a for loop using php.

function funct($$name,$page)

    {
        $url="http://testserver.com/client?list=$name&page=$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;
            }   
        }

The code works fine but when i get a name i need to add a position and for each name it will be incremented by 1 to make it contentious. But when the values for the pages are passed, for an example when i move from page 1 to page 2. the count starts again from first, next page... same problem.

How can i make it continues on every page?

+2  A: 

Either make $position a global variable (global $position;) or pass it to the function: function funct($name, $page, &$position). (What's with the variable variable $$name in your function signature?)

janmoesen
Not that I am advocating global variables, of course. :-)
janmoesen
+1 but not for global ;)
Felix Kling
A: 

Use $_SESSION. It's designed specifically to maintain state.

dnagirl
I think in this case session is not necessary. Moving form page 1 to 2 means in this case: calling the function with `$page=1` and `$page=2`... I guess :)
Felix Kling
@Felix: right, but the position number is relative to the preceding page. Assumably something makes the number of elements on any page variable. If he stores `$_SESSION['last_position']`, then `$position=$_SESSION['last_position'] +1;` Of course this means that jumping several pages still only moves 1 position.
dnagirl