1

I want the loop just to output once. Instead it outputs twice. Here is the code:

$results = mysql_query($query);
    while ($c = mysql_fetch_array($results)){
        $individualPostcode = explode(",", $c['postcode']);
        foreach($individualPostcode as $val){ 
            $val = trim($val); //Get rid of spaces
            if($val === $postcode){
                echo $c['url']."<br>";
            }
        }
    }
}

Here is the output:

http://www.dyno.com/home-security/local-experts/greater-london/dyno-locks-and-alarms-enfield
http://www.dyno.com/home-security/local-experts/greater-london/dyno-locks--alarms-enfield
http://www.dyno.com/home-security/local-experts/greater-london/dyno-locks-and-alarms-enfield
http://www.dyno.com/home-security/local-experts/greater-london/dyno-locks--alarms-enfield

I've tried taken out the foreach loop but I need to go through that array checking against a user input.

Here is the initialisation of $postcode:

$userInput = $_POST["input"];
if(strlen($userInput) < 4) 
    echo "User Input : ".$userInput."<br>";
else //Below gets the first three chars of the users string
    echo "User Input : $userInput<br>What is being used : ".mb_substr($userInput, 0, 3)."<br>";

$postcode = mb_substr($userInput, 0, 3);    
6
  • mysql_* syntax is Evil !! Commented Jun 29, 2015 at 9:23
  • What is value of $postcode ? And if 2 records have same Comma separated list for post code. Its going to repeat for 2nd record and 1st one ! Commented Jun 29, 2015 at 9:26
  • stackoverflow.com/questions/12859942/… Commented Jun 29, 2015 at 9:27
  • erm... do you mean the way I've wrote the code? Commented Jun 29, 2015 at 9:27
  • $postcode is a user input that is formatted so its the first three characters i.e. E10 78P would become E10 Commented Jun 29, 2015 at 9:28

2 Answers 2

1

You can always create an array of the URL's to stop them from duplicating by checking if the url has been put into the array:

$results = mysql_query($query);
$urlsArr = array();
while ($c = mysql_fetch_array($results)){
    $individualPostcode = explode(",", $c['postcode']);
    foreach($individualPostcode as $val){ 
        $val = trim($val); //Get rid of spaces
        if($val === $postcode){
            if (!in_array($c['url'], $urlsArr)) echo $c['url']."<br>";
            $urlsArr[] = $c['url'];
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

mysql_fetch_array returns both an associative and index array for each of your returned results. The foreach loop is going to loop over both and output twice. Try using mysql_fetch_assoc()

http://php.net/manual/en/function.mysql-fetch-array.php

Better still, try moving to the mysqli class. It's faster and mysql is depricated.

http://php.net/manual/en/intro.mysqli.php

1 Comment

I've tried changing the mysql_fetch_array to the mysql_fetch assoc but the output is still the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.