TheTimes
TheTimes

Reputation: 77

PHP + MySQL + HTML Displaying Different information onTable

I have a query that I am going to display as a table. But I would like the integer value within my table to be displayed as something else within the table.

For example:

Thw query will display Product ID, and Product completion. Production completion within my table is stored as either 1 or 0.

So in the table, I would have instead of a 1 or 0, a "yes" or "no" respectively.

How can I do this?

Simple I know, but I have been searching to no avail. I'm sure its because I don't know what the term is for what I'm looking to do.

I'm almost certain this has been asked before, but I cannot figure it out.

Upvotes: 1

Views: 51

Answers (5)

Jay
Jay

Reputation: 2686

There is alot of unknows with this question. But assuming you have queried the database and have a value returned and all you need to do is switch 0 and 1 to yes or no then you can just do an if statement and insert that into the table.

  // assuming $value = 1 
  <td><?php if($value){echo "yes";}else{echo "No";}?></td>

or have it in whatever format you want

Upvotes: 2

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

There are many good answers to choose from, that have been given.

This is one/another way of doing this:

$query = mysqli_query($conn,"SELECT * FROM your_table");

    while($row = mysqli_fetch_array($query)) {

        $column = $row['ID'];
        if ($column == "1") {

        $column = "Yes";
               echo $column;
            }

        else{

            $column = "No";

               echo $column;

        }

}

Upvotes: 1

Dan
Dan

Reputation: 9468

If you are just trying to display "Yes" or "No" then I would use a ternary operator.

<table>
    <tr>
      <th>Col 1</th>
      <th>Col 2</th> 
      <th>Col 3</th>
    </tr>
    <tr>
      <td><?=$var == 1 ? 'Yes' : 'No'?></td>
      <td>Col 2 data</td> 
      <td>Col 3 data</td>
    </tr>
</table>

The ternary operator says, if $var==1 display Yes else display No.

Upvotes: 2

Brian DeMilia
Brian DeMilia

Reputation: 13248

This may help you out a little, depending on how far you are. This assumes you are using mysqli and that your connection string has a variable of $con (adjust as needed)

<?php

$sql="select product_id, product_completion from tbl";
$sql_result = mysqli_query($con,$sql);

?>

<table>

    <tr>
        <th>Product ID</th>
        <th>Completed</th>
    </tr>

<?php $z=0; while ($row = mysqli_fetch_array($sql_result)) { ?>

    <tr>
        <td><?php echo $row['product_id']; ?></td>
        <td><?php if ($row['product_completion'] == 1) echo 'yes'; else echo 'no'; ?></td>
    </tr>

<?php $z++; } ?>

</table>

Also change your query to your actual query and the column names to your actual column names, and "tbl" to you actual table name, etc.

Upvotes: 0

George Eco
George Eco

Reputation: 464

Getting a result into a variable, and using it with an IF statememt is a solution. A boolean variable can be hardcoded all right. If the boolean is TRUE you echo a Yes or else a NO.

Upvotes: 0

Related Questions