Hi Guys,
I am trying to setup a database search using postcodes but I have a little issue.
Some of the customers will type in there full postcode but the database may not have theres listed as it only lists the companies poscode.
I have changed my companies poscodes to the 1st 4 characters i.e. ls25, br16 etc
Can anyone help me in trimming down the postcode from the input form I have so that it only contains 4 characters? or provide a better solution to using postcode searching?
My form is called form1
Thanks in advance.
Searching a database for a postcode
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
function format_postcode($input) {
if (strlen($input) >= 4) {
return substr(0,4,$input);
}
else {
return $input;
}
}
echo format_postcode('432840329489234239');
echo '<br />';
echo format_postcode('1234');Code: Select all
4328
1234read up on strlen and substr
Thanks for the quick reply, Very much appreciated.
I am still new to php and I would appreciate it very much if you could show me in my code where to place this function.
Thanks
I am still new to php and I would appreciate it very much if you could show me in my code where to place this function.
Code: Select all
<?php
include_once("connect.php");
$sql = "SELECT DISTINCT status, companyname, address, phone, fax, type, postcode from companies WHERE postcode='$_REQUEST[form1]' ORDER BY postcode";
$result = mysql_query($sql)
or die ("Couldn't select $_REQUEST[form1]");
echo "
\n";
//-------------get each type ------
while ($line = mysql_fetch_array($result))
{
extract($line);
echo '<LINK REL=StyleSheet HREF="123.css" TYPE="text/css">
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<th width="10%" height="23" scope="col"><div align="left"><span class="style2"></span></div></th>
<th width="55%" valign="top" background="bb.jpg" scope="col"><div align="left" class="style3 style4 style7"> <a href="nextpage.php?companyname=' . $companyname . '"> <span class="style10">' . $companyname . '</span></div></th>
<th width="35%" scope="col"><div align="left" class="style8 style5"><strong> </strong></div></th>
</tr>
<tr>
<th height="66" scope="row"><div align="left"><span class="style2"></span></div></th>
<th valign="top" bgcolor="#F5F5F5" scope="row"><div align="left" class="style4">' . $address . '</div></th>
<th scope="row"><div align="left"> </div></th>
</tr>
<tr>
<th scope="row"><div align="left"><span class="style2"></span></div></th>
<th valign="top" scope="row"><div align="left"> </div></th>
<th scope="row"><div align="left"> </div></th>
</tr>
</table>';
}
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I'm not that great with SQL but try
Althought I would either entirely handle the postal code conversion through PHP or SQL, not both. Either format the code before inserting it in your database, or simply fix all the existing postal code in the database to your new standard.
Code: Select all
$sql = "SELECT DISTINCT status, companyname, address, phone, fax, type, SUBSTRING(1,5,postcode) as `code` from companies WHERE code='".format_postal($_REQUEST['form1']."' ORDER BY postcode";