 |
Jobs New Media Creative Forum Topics ranging across the Creatives Art & Design and New Media Sectors
|
| Author |
Message |
Kafu
Joined: 02 Oct 2007 Posts: 2
|
Posted: Tue Oct 02, 2007 8:03 am Post subject: Help with PHP and strings |
|
|
Hi, I'm fairly new to PHP.
I have just built a MySQL table collecting garden centres names and addresses and I want to be put them online. I can do all of this but I a having a re-occuring problem.
When ever I display an address I use the following:
echo "$store_name, $address_1, $address_2, $city, $post_code.";
But if there are areas of the address missing, for instance the postcode, I get this as an echo:
"Flowers Garden Centre, 1 Berkley street, , , ."
If there any way I can get rid or hide those ',' or fullstop symbols.
Kafu. |
|
| Back to top |
|
 |
|
|
jamesmiddz Site Admin
Joined: 22 Aug 2007 Posts: 9 Location: UK
|
Posted: Tue Oct 02, 2007 8:13 am Post subject: Use if else statements |
|
|
There are lots of ways in which this can be done with PHP. I will show you one method that I used recently within a content management system that I built.
<?php
$address = ""; ///This establishes a blank string
if($store_name)
{
$address .= "$store_name, "; ///This adds the conditional content of '$store_name' to the string
}
if($address_1)
{
$address .= "$address_1, ";
}
if($address_2)
{
$address .= "$address_2, ";
}
if($city)
{
$address .= "$city, ";
}
if($post_code)
{
$address .= "$post_code, ";
}
$address = substr($address, 0, strlen($address)-2); ///Trim end of ', ' (2 characters)
echo "$address.";
?>
Now some might say there are quicker ways of doing this, and ofcourse there are. But this is a simple approach for a beginner and demostrates some very useful string handling including 'if' conditional statements and 'substr' string trimmer.
All the best, jamesmiddz |
|
| Back to top |
|
 |
Kafu
Joined: 02 Oct 2007 Posts: 2
|
Posted: Tue Oct 02, 2007 8:59 am Post subject: PHP strings |
|
|
Thanks for the quick reply.
I have tried the PHP code out and now it works perfectly!
Thanks Jamesmidds
Kafu |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group
|