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 

3 Answers 3

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

3 Comments

nice one this seems to work. $row["status"] = 1; i removed the =1 and it takes the int and converts to the string nicely .
i cant ask any more questions on stack...was this question that bad?
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.
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

3 Comments

iam gettign an error, echo "<td>". switch( $row["status"]) { case 0: $var = "hello"; break; case 1: $var = "goodbye"; break; case 2: $var = "whatever"; break; }. "</td>";
I don't think you can use switch statements in line like that, I edited for a better example
great the php one worked nicely . Thanks for the full example..thats where i get lost normally
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>";

3 Comments

i see what it was doing now.. trouble is the answer is concatenated and i need a different word for each number.. i.e there will only be 1 number in each table row.
Using $row["status"] === 0 ?: "Hello" is valid. It just won't do anything if its false
@MattAltepeter won't do anything if it is true

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.