简体   繁体   中英

how to fetch int value from database and display string value instead of that int value in table using php and mySQL

I need to display the string values in HTML table based on the int value fetched from mySQL database. it is a checking process of users prime member or not.

My HTML Code is

<table class="table table-bordered">
        <thead>
          <tr>
            <th>Store Name</th>
            <th class="text-center" style="width: 10%;">  Prime </th>
          </tr>
        </thead>    
        <tbody>

            <?php 
                while($eachitem = $result->fetch_assoc())
                {
            ?>
  
           <tr>
               <td> <?php echo remove_junk($eachitem['name']); ?></td>
               <td class="text-center"> <?php echo        remove_junk($eachitem['prime']); ?></td>
                    
              </td>
            </tr>

            <?php
            }
            ?>
        </tbody> 
      </table>
      

And I have my db configuration in config.php

  <?php
     include('config.php');
  
    // SQL query to select data from database
    $sql = "SELECT * FROM store ";
    $result = $con->query($sql);
  ?>

The HTML code for adding prime to database is

 <select name="prime" class="form-control">
   <option value="0">Not prime</option>
   <option value="1">prime</option>
 </select>

I have Int values 0 and 1 in store table row names prime. Now i need to show No instead on 0 and Yes instead of 1 without changing drop down values in HTML

use the ternary operator ? (aka conditional operator). It has the form

(condition) ? (what to do if condition is true) : (what to do if condition is false);

For your code, change

<?php echo remove_junk($eachitem['prime']); ?>

to

<?php echo remove_junk($eachitem['prime']) ? 'Yes' : 'No'; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM