tags:

views:

72

answers:

3

I am trying to search and check for the duplicate details that is available in the database using php.The user enters several names and then a phone number to check for the duplicates. Below is my function. I just cropped out some parts because it was too long.

function gtc($names,$phone)
{

    $pageNumb=20;
    $position = array(5);

    $sepname=explode(",","$names");

foreach ($sepname as $sepname1)
{
    for ($page=0;$page<=$pageNumb;$page=$page + 1)
    {
    $turl="http://192.168.111.2119/search.php?qry=$sepname1&amp;page=$page";

    $search=curl_init();

    curl_setopt($search,CURLOPT_URL,$turl);
    curl_setopt($search,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($search,CURLOPT_FAILONERROR,true);
    curl_setopt($search,CURLOPT_AUTOREFERER,true);
    $result=curl_exec($search);

        $dom = new DOMDocument();
        @$dom->loadHTML($result);

        $xpath=new DOMXPath($dom);
        $elements = $xpath->evaluate("//div[@id='inf']");

        foreach ($elements as $element)
        {

            $position[$sepname1] = $position[$sepname1] + 1;

            foreach($position as $key=>$val)

            $contct  = $element->getElementsByTagName("contact")->item(0)->nodeValue;

            if (preg_match("/$phone/i",$contct)) {
                echo "Found In Database>";

            }

            }

        ob_flush();
        flush();

        }
}
    }


?>

The function works perfectly but the only problem that I am having is although a match is found it goes until the last page which is given in the for loop and then goes through the next name. I would like to know is it possible to make when a match is found stop processing and then search for the next name?

I don’t want to use exit(); because it completely stops the execution

The input format is as below
User inputs names: john, Sam, Michael, Steve
Phone number:0084554741

Any help will be appreciated

+8  A: 

You are likely looking for break

break ends execution of the current for, foreach, while, do-while or switch structure.

// output numbers 1,2,3,4
foreach( range(1,10) as $number) {
    if($number === 5) break;
    echo $number, PHP_EOL;
}

and/or continue

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

// output only even numbers
foreach( range(1,10) as $number) {
    if($number % 2) continue;
    echo $number, PHP_EOL;
}
Gordon
Maybe you want to add that you can tell continue how many loops to escape.
Kristoffer S Hansen
@Kristoffer I leave that for the OP to find out at the respective manual pages :)
Gordon
A: 

Take a look at:

http://pl.php.net/break

http://pl.php.net/continue

Piotr Pankowski
+2  A: 

@Gordon offered the formally correct answer, a "best practices" answer would be to split your function into logical parts (especially as you mention it's too long), for example:

 function search($term) {
    $xml = loadXmlFromRemoteHost();
    if(!$xml)
       echo "An error occured";
    $found = findTermInXml($xml, $term);
    if($found)
       echo "Found In Database>";

The search function contains the loop in question and uses "return" to exit the loop (and the function) when it finds something:

    function findTermInXml($xml, $term) {
        foreach(...whatever...) {
             if(...some condition...)
                 return true;
        }
        return false; // nothing found
    }
stereofrog
+1 for refactoring
Gordon