Jump to content

PHP - Help ASAP Please!


micky007

Recommended Posts

Hi Guys,

 

I've managed to code what i need it do to but i have an issue, below is my code:

 

$postcode=$_GET['postcode'];

function countchar ($string) { 
    
$resultpostcode = strlen ($string)  -   substr_count($string, ' '); 
echo $resultpostcode;  
} 

countchar ($postcode);


if ( $resultpostcode == 6 ) {

	echo "Postcode1 ";
	echo substr($postcode, 0, 3);
	echo "<br>";
	echo "Postcode2 ";
	echo substr($postcode, 3);

} elseif( $resultpostcode == 7 ){

	echo "Postcode1 ";
	echo substr($postcode, 0, 4);
	echo "<br>";
	echo "Postcode2 ";
	echo substr($postcode, 4);

}else {
echo "error";
}

 

If the value of postcoderesult is 6 (it even outputs 6) then it wont echo what it should be doing, the same if its 7. It just keeps echoing Error.

 

Any help would be great please.

 

Thank you.

 

Link to comment
Share on other sites

I'm not entirely sure what you are trying to achieve, if you could elaborate on that please. But, good practice is change your function to return the result rather than echo, then call it in a variable afterwards;

 

$postcode=$_GET['postcode'];

function countchar ($string) { 
    
$resultpostcode = strlen ($string)  -   substr_count($string, ' '); 
return $resultpostcode;  
} 

$resultpostcode = countchar ($postcode);


if ( $resultpostcode == 6 ) {

	echo "Postcode1 ";
	echo substr($postcode, 0, 3);
	echo "<br>";
	echo "Postcode2 ";
	echo substr($postcode, 3);

} elseif( $resultpostcode == 7 ){

	echo "Postcode1 ";
	echo substr($postcode, 0, 4);
	echo "<br>";
	echo "Postcode2 ";
	echo substr($postcode, 4);

}else {
echo "error";
}

Link to comment
Share on other sites

What im trying to do is get how many characters there are in the variable called postcode. If theres 6 characters then it will echo the code. If theres 7 characters then it will echo different code.

 

The script also removes any spaces in the data that the variable holds.

 

Thanks.

Link to comment
Share on other sites

I just tested the alteration of the code that I provided earlier and it does exactly what you want... I enter postcode 'AB12 3CD' and it outputs, 'Postcode1 AB12, <br/> Postcode2 3CD'. I enter postcode 'AB1 3CD' and it outputs 'Postcode1 AB1, <br/> Postcode2 3CD'. FYI, you could make those 5 echo's one echo using the '.' operator to join the sections together:

 

echo "Postcode1 ".substr($postcode, 0, 3)."<br>Postcode2 ".substr($postcode, 3);

Link to comment
Share on other sites

If you want some form of validation for your postcode consider this one way of doing it. It ensure the correct format is entered i.e Letter Numbers etc....(Uk Postcodes)

 

 function IsPostcode($postcode)
{
$postcode = strtoupper(str_replace(' ','',$postcode));
if(preg_match("/^[A-Z]{1,2}[0-9]{2,3}[A-Z]{2}$/",$postcode) || preg_match("/^[A-Z]{1,2}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{2}$/",$postcode) || preg_match("/^GIR0[A-Z]{2}$/",$postcode))
return true;
else
return false;
}

$codevalid=$postcode;
    if (IsPostcode($codevalid))
    echo "Valid";
    else
    echo "Invalid";

 

Hope this helps.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.