3

I'm creating an computer inventory website that is displayed in a table. I'm trying to change the output display 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

5 Answers 5

6

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']];
Sign up to request clarification or add additional context in comments.

Comments

6

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";

1 Comment

Without break at the end of any case line, he outputs always '7'.
5

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.

Comments

4
$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'];
}

Comments

4

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']];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.