开发者

PHP changing number to words

I have stored in my DB a row and in that row SubCategories

it has the following 2,56,81

What I need to do is change the numbers to the correct SubCategories ie: 1 = Sports 2 = Camping 3 = Climing

etc

I thought I would be ab开发者_如何学运维le to do the following

$parts = explode(',', $row['SubCategories']);

But now I ran into a slight issue with how do I turn that into the words?


You'll have to create an array with the word definitions, and then use the numbers as keys:

$words = array (
    1 => 'Sports',
    2 => 'Camping',
    3 => 'Climbing'
);

$parts = explode(',', '1,3');

for ( $i = 0; $i < count($parts); $i++ )
{
    $parts[$i] = $words[ $parts[$i] ];
}

See it in action: http://codepad.org/vs2dJi5E


You're going to have to do a query for those category names, using a WHERE IN() (Details here).

However, it would be better to just have a parent column in the row for the subcategory, and query WHERE parent = 2 to get subcategories for that category

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜