1

I select a column 'status' from my db that holds 3 ints. i will want the 0,1,2 to echo hello, goodbye, whatever to the user once they are on the page.

0 = hello
1= goodbye
2= whatever

i cant find any examples of how you echo this with php.

Ive seen loads of examples that just give cold code for $var = 1 $var= hello... etc . I cant see how this fits into a real example.

So far i select the column and the status will echo but with a 1 in front of staus 0 and a 1 at end of status 1 .

echo "<td>".( $row["status"]==0 ?  : 'Hello' ). ( $row["status"]==1 ?  : 'Goodbye' ) ."</td>";

Table result

Status:
hello1
hello1
hello1
1goodbye
hello1
1goodbye 
chheky
  • 19
  • 7

3 Answers3

2

You have a couple options:

PHP switch statement:

switch ($row["status"]) {
    case 0: $var = "hello"; break;
    case 1: $var = "goodbye"; break;
    case 2: $var = "whatever"; break;
}

full example:

$str = "<td>";
switch ($row["status"]) {
    case 0: $str .= "hello"; break;
    case 1: $str .= "goodbye"; break;
    case 2: $str .= "whatever"; break;
}
$str .= "</td>";
echo $str;

Or you could do a case statement in your mysql:

SELECT status,
CASE
    WHEN status = 1 THEN "Hello"
    WHEN status = 2 THEN "Goodbye"
    WHEN status = 3 THEN "Whatever"
END
FROM yourTable
Matt Altepeter
  • 956
  • 1
  • 8
  • 18
  • iam gettign an error, echo "". switch( $row["status"]) { case 0: $var = "hello"; break; case 1: $var = "goodbye"; break; case 2: $var = "whatever"; break; }. ""; – chheky Apr 23 '18 at 18:18
  • I don't think you can use switch statements in line like that, I edited for a better example – Matt Altepeter Apr 23 '18 at 18:38
  • great the php one worked nicely . Thanks for the full example..thats where i get lost normally – chheky Apr 23 '18 at 18:45
2

You could use an array to hold the various options that it can be. It almost acts like a small lookup table, but saves linking to another database table...

$options = [ 0 => "hello", 1 => "goodbye", 2 => "whatever" ];
$row["status"] = 1;

echo "<td>". $options[$row["status"]]."</td>";

gives...

<td>goodbye</td>
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • nice one this seems to work. $row["status"] = 1; i removed the =1 and it takes the int and converts to the string nicely . – chheky Apr 23 '18 at 18:35
  • i cant ask any more questions on stack...was this question that bad? – chheky Apr 30 '18 at 15:27
  • Can't see anything wrong with it or why you can't ask questions. You can ask on meta.stackoverflow.com if there is a problem and they may be able to help. – Nigel Ren Apr 30 '18 at 15:36
0

Just fixing what you currently have, because you are using a ternary operator, you need to use it correctly. Also need to be aware that checking ==1 checks for truthiness and not for 1, and so 2 also would match, so change to use === instead:

Change to this:

echo "<td>". ( $row["status"]===0 ? 'Hello' : '' ) . ( $row["status"]===1 ? 'Goodbye' : '' ) . ( $row["status"]===2 ? 'Whatever' : '' ) . "</td>";
Andrew
  • 18,680
  • 13
  • 103
  • 118