LiveEn
LiveEn

Reputation: 3253

problem calculating the position using php

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?

Upvotes: 0

Views: 95

Answers (2)

dnagirl
dnagirl

Reputation: 20446

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

Upvotes: 0

janmoesen
janmoesen

Reputation: 8020

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?)

Upvotes: 2

Related Questions