开发者

php: how can i change Stored value into user friendly values !

开发者 https://www.devze.com 2023-03-04 15:46 出处:网络
the problem in short, Field:Profil开发者_C百科eItems = \"action,Search,Work,Flow,pictures\"; Mysql query = \"SELECT ProfileItems FROM addUsers\";

the problem in short,

Field:Profil开发者_C百科eItems = "action,Search,Work,Flow,pictures";

Mysql query = "SELECT ProfileItems FROM addUsers";

then I explode with , making array e.g.: array('action','search',...etc)

and create fields for ,

Result:

<form>
action : <input type=text name=action>
search : <input type=text name=search>
...etc
<input type=submit>
</form>

My problem is how can I replace names in the database with more user friendly ones (add description) to fields without using an IF statement??

//created asoc array with Key = search item and value = user friendly value
$prase = array("ABS" => "ABS (Anti lock braking System)"
         ,"DriverAirBag" => "Air bags");
$string= "ABS,DriverAirbag,GOGO,abs";

foreach($prase as $db=>$eu){
    echo "if $db will be $eu<br>";
    echo str_ireplace($eu,$db,$string);
}
echo $string;

Tried above but was an epic fail :D !.. can you please help me out ?


Having a map inside PHP is not an unreasonable approach, but you're doing the str_ireplace() backwards: it's search, replace, subject, so in your case str_ireplace($db, $eu, $string);

But just doing a str_ireplace() on a comma-separated list of strings is not ideal anyway. For one thing, imagine if after you did the substitution for ABS you then encountered another profile item that matched lock (which just so happens to appear in "Anti-lock braking system"). Oops. Now you've overwritten your earlier replacement!

How about something like this:

$prase = array("ABS" => "ABS (Anti lock braking System)"
         ,"DriverAirBag" => "Air bags");
$string= "ABS,DriverAirbag,GOGO,abs";
$fields = explode(',', $string);

foreach($fields as $field) {
    $friendly = $field;
    if (isset($phrase[$field]))
        $friendly = $phrase[$field];

    echo htmlspecialchars($friendly) . ': <input type="text" name="' . htmlspecialchars($field) . '" />
}

The key here is that you're handling each field separately. And you're never just doing a replacement; you're looking specifically for the keywords "ABS" or "DriverAirbag". If there's not an exact match, you don't have a human-friendly name for that item, and there's no point doing any replacement.

All this can be improved even further if you have the ability to change the database schema. Storing a comma-separated list is never desirable. You should have a table with a schema something like:

  • field_id (e.g., "ABS")
  • name (e.g., "Anti-lock Braking System")

And another table like:

  • user_id (I'm inferring a little here from the name addUsers — whatever field/s you have in addUser now identifying the person)
  • field_id (i.e., foreign key to the above field table)

Note that you may end up with many rows in this table for each person (1, 'ABS'), (1, 'DriverAirbag')

But then your query can become

SELECT field, name
FROM user_field
INNER JOIN field USING (field_id)

Now you get back one row for each field (no explode required!) and each row includes both the computer-friendly and human-friendly name.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号