A better way?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

A better way?

Post by psurrena »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


The code below works just fine. Its job is to return the multiple abbreviations which are separated by spaces and stored in a row in my db. They are renamed and then displayed.

My question is, is there a better way to do this than a big elseif statement?

Regards,
Peter

Code: Select all

$a=explode(' ',$row['project_type']);

foreach ($a as $key=>$value){
	if ($value=="CRG"){$category="City + Region ";}
	elseif ($value=="CIV"){$category="Civic ";}
	elseif ($value=="CMU"){$category="Commercial + Mixed Use ";}
	elseif($value=="ER"){$category="Eco Resorts ";}
	elseif ($value=="EDU"){$category="Education ";}
	elseif ($value=="POS"){$category="Parks + Open Spaces ";}
	elseif ($value=="PAS"){$category="Plazas + Streetscapes ";}
	elseif ($value=="SL"){$category="Senior Living ";}
	elseif ($value=="TRA"){$category="Transportation ";}
	elseif ($value=="WTR"){$category="Waterfronts ";}
	
	echo $category;
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd suggest redesigning the table(s).

You should have a table for the various items and you should have a table to link those items to whatever record this "separated data" is coming from now.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

How would that make it any simpler? I have one row with the data.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. Because the information would be returned from the database, not from your script.
  2. It's easier to maintain the databases' information than a script for non-technical people.
  3. It's the proper way of linking multiple things with an item
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I agree with feyd. You are essentially trying to relate data code side that can easily (and ideally) be done database side.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

My table for this page includes the following rows:
- id
- name
- description
- city
- state
- country
- type (array mentioned earlier)

So you're saying have the array in "type" refer to a table where the abbreviations are spelled out. I assume the purpose would to centralize the data(best practices)?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Proposed structures:

Code: Select all

id
typeid
name
description
city
state
country

Code: Select all

typeid
typename
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

Sorry - the list was just an idea of the names. Each row is "project_rowname"
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I agree with my distinguished forum overlords.

However....if you don't want to modify your database you can also use a "translation array":

Code: Select all

$codes = array("TRA"=>"Transportation","WTR"=>"Waterfronts");//..etc..
$category = $codes[$row['project_type']];
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

I think I get it. So you would have an additional table for say city/st/country/id and join the tables on the query? So I would have "project_loc" in my Projects section that would store the id of the correct location?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

psurrena wrote:I think I get it. So you would have an additional table for say city/st/country/id and join the tables on the query? So I would have "project_loc" in my Projects section that would store the id of the correct location?
You're getting it. That would be a much more desirable relational database.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yeah, basically you want to relate the data in the database and select the data according to its relationship so that by the time PHP gets it, it is already assembled into a usable array set without needing a bunch of resource intensive manipulation.
Post Reply