Page 1 of 1

A better way?

Posted: Thu May 24, 2007 10:51 am
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]

Posted: Thu May 24, 2007 11:02 am
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.

Posted: Thu May 24, 2007 11:07 am
by psurrena
How would that make it any simpler? I have one row with the data.

Posted: Thu May 24, 2007 11:11 am
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

Posted: Thu May 24, 2007 11:20 am
by RobertGonzalez
I agree with feyd. You are essentially trying to relate data code side that can easily (and ideally) be done database side.

Posted: Thu May 24, 2007 11:22 am
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)?

Posted: Thu May 24, 2007 11:35 am
by RobertGonzalez
Proposed structures:

Code: Select all

id
typeid
name
description
city
state
country

Code: Select all

typeid
typename

Posted: Thu May 24, 2007 11:40 am
by psurrena
Sorry - the list was just an idea of the names. Each row is "project_rowname"

Posted: Thu May 24, 2007 12:08 pm
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']];

Posted: Fri May 25, 2007 12:32 pm
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?

Posted: Fri May 25, 2007 2:53 pm
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.

Posted: Fri May 25, 2007 4:10 pm
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.