Question

I am using a with statement for values in a table cell. Prior to inserting this statement all was fine. All I am trying to do is to change the color label on the text depending on the string value, but now the Cell is blank which tells me that none of the cases are true. I suspect that if there was an error instead then the cells after the statement would also be blank, but they are complete.

If I had to guess, I would say there is an error in the string declaration, but I am not sure.

<?php
$result = mysql_query("SELECT * FROM logs ORDER BY logid DESC");
while($data = mysql_fetch_array($result))
{
                            echo '<tr>
                                <td>'.$data["logid"].'</td>
                                <td class="center">'.$data["date"].'</td>
                                <td class="center">';

                                    $log_type='.$data["log_type"].';
                                    switch ($log_type) {
                                        case "Check in":
                                            echo '<span class="label label-success">'.$data["log_type"].'</span>';
                                            break;
                                        case "Log":
                                            echo '<span class="label">'.$data["log_type"].'</span>';
                                            break;
                                        case "Sensor Event":
                                            echo '<span class="label label-important">'.$data["log_type"].'</span>';
                                            break;
                                    }

                                echo '</td>
                                <td class="center">'.$data["log_entry"].'</td>
                                <td class="center">'.$data["customer_id"].'</td>
                            </tr>';
}
?>

The full code block simply reads data from a mysql table and spits it out in a nice table format into a website. By using different CSS classes, I can change the label color.

Question is why is this Case only a blank cell?

UPDATE: for those of you reading this and looking for the specific answer I have changed the line:

$log_type='.$data["log_type"].';

to

$log_type=$data["log_type"];

and that fixed it.

Was it helpful?

Solution

It's a pretty obvious mistake. Variables under single quotes will not be parsed which is what you have done. With your current code, $log_type will have the value .$data["log_type"]. (tested)

So, your code of:

$log_type= '.$data["log_type"].';

should be:

$log_type= $data["log_type"];

If you want single quotes around the value, you should do:

$log_type = "'".$data["log_type"]."'";

OTHER TIPS

$data holds your result set array.

In this line :

$log_type='.$data["log_type"].';

you are accesing the result set in incorrcet manner, to access the attributes of the result set, you just have to enclose them in quotes against the object ( $data in this case ) holding the array.Below is the correct way to solve it :

$log_type=$data["log_type"];

to concat or enclose your solution in ' :

"'".$log_type."'" OR "'".$data["log_type"]."'"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top