PHP changing output from the database
I'm creating an computer inventory website that is displayed in a table. I'm trying to change the output display 开发者_StackOverflow中文版for example the following
echo "<td>".$row['WindowsVersion']."</td>\n";
displays 5.0 or 5.1 or 5.2 or 6.0 or 6.1
but I want it to display
5.0 as 2000
5.1 as XP
5.2 as 2003
6.0 as Vista
6.1 as 7
Any help would be greatly appreciated
You can always create a predefined array, using the Windows version as keys like this one.
$win_ver = array('5.0' => '2000','5.1' => 'XP');
echo $win_ver[$row['WindowsVersion']];
DB Version:
SELECT
CASE WindowsVersion
WHEN '5.0' THEN '2000'
WHEN '5.1' THEN 'XP'
WHEN '5.2' THEN '2003'
WHEN '6.0' THEN 'Vista'
ELSE '7' AS WindowsVersion
FROM ...
OR PHP
<?php
function getWinName($win) {
switch($win) {
case '5.0': $version = '2000'; break;
case '5.1': $version = 'XP'; break;
case '5.2': $version = '2003'; break;
case '5.0': $version = 'Vista'; break;
default: $version = '7';
}
return $version;
}
echo "" . getWinName($row['WindowsVersion']) . "\n";
In such cases you can use an array as display/rewrite map:
$rewrite = array(
"5.0" => "2000",
"5.1" => "XP",
..
);
print $rewrite[ $row['WindowsVersion'] ];
It simply uses your original value as an index key in the rewrite array, and gets you the associated alternative value for that.
$versions = array( '5.0' => '2000',
'5.1' => 'XP',
'5.2' => '2003',
'6.0' => 'Vista',
'6.1' => '7'
);
if( array_key_exists( $row['WindowsVersion'], $versions ) ) {
echo $versions[ $row['WindowsVersion'] ];
} else {
echo $row['WindowsVersion'];
}
Why don't you put the things you want to see directly in the database. It would be easier. But if this is not possible just use:
$map = array('5.0'=>'2000', ...);
...
print $map[$row['WindowsVersion']];
精彩评论