0

I have a query where I'm already pulling back the results from a Sql query via an array as below.

<?php

$sql = "SELECT inc.STATUS as STATUS,
FROM dbo.HELP_DESK as inc";

$result = sqlsrv_query($conn, $sql);

$status=(
'1' => "Request For Authorization",
'3' => "Pending",
'4' => "Scheduled For Review",
'5' => "Scheduled For Approval",
'8' => "Pending",
);

echo "<table><tr id=\"header\"><td><center>Status</center></td><tr>";

while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {

echo "<tr><td><b><center> " . $row["STATUS"] . "</center></b></td></tr>";
}

echo "</table>";

sqlsrv_close( $conn );
?>

The $row fetches back a number from the database. However, I'm wanting to display the text from my $status array.

I've tried several methods but end up with a blank screen returned.

Any suggestions? Appreciate the help.

3 Answers 3

2

change your while loop as below. check status number is available in $status or not if available then display its value otherwise display blank

while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
    $status_str = isset($status[$row["STATUS"]])?$status[$row["STATUS"]]:"";
    echo "<tr><td><b><center> " . $status_str . "</center></b></td></tr>";
}

Also change your array to(You forgot array keyword)

$status=array(
'1' => "Request For Authorization",
'3' => "Pending",
'4' => "Scheduled For Review",
'5' => "Scheduled For Approval",
'8' => "Pending",
);
Sign up to request clarification or add additional context in comments.

Comments

0

Read also about this: http://php.net/manual/en/function.strtr.php

strtr($row["STATUS"], $status);

Comments

0

Is the the blank screen caused by a PHP error? You should check your logs. Because, in your example, your wrote the array like this:

    $status=(
'1' => "Request For Authorization",
'3' => "Pending",
'4' => "Scheduled For Review",
'5' => "Scheduled For Approval",
'8' => "Pending",
);

But you have to use brackets instead:

$status=[
'1' => "Request For Authorization",
'3' => "Pending",
'4' => "Scheduled For Review",
'5' => "Scheduled For Approval",
'8' => "Pending",
];

Then you will have to display the the label by something like this:

echo $status[$row['STATUS']];

BTW, you should use a (micro) framework to develop, because you should (or must) not write a query in your DB and display the resuslts in the same file.

HTH.

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.