【问题标题】:How to display a Custom text on fetched value?Custom text display on fetched value
【发布时间】:2022-12-01 19:37:07
【问题描述】:

I'm fetching the data from a SQLite table and looks like in the capture printscreen:

Probably a dumb question but i can't sort it out. There is a way to override the value returned from the table (marked in this image) and define a custom text to be displayed for each fetched value? How? Thank you.

my code is:

<?php
   try {
    $conn = new PDO('sqlite:db/Mydatabase.db');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT attributename, attributevalueEN, attributeimage FROM attributes ORDER BY attributename ASC");
    $stmt->execute();
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo '<table style="width: 100%; padding-left: 2vw; padding-right: 2vw;">';
    echo '<tr><th style="width: 20%; ">Iconography</th>
              <th style="width: 30%; ">Attribute property</th>
              <th style="width: 50%; ">Attribute group</th>
          </tr>';
    if ( !empty($data) ) {
      foreach ( $data as $row ){
         echo '<tr><td>'.'<img src="data:image/jpeg;base64,'.base64_encode($row['attributeimage']).'" style="width: 8vw; height: 8vw; " />'.'</td>
                   <td>'. $row['attributevalueEN'] .'</td>
                   <td>'. $row['attributename'] .'</td>
               </tr>';
      }
    } else {
    }
  echo '</table>';
  }
  catch(PDOException $e) { echo "Error: " . $e->getMessage(); }
  $conn = null;
?>

【问题讨论】:

  • You mean instead of &lt;td&gt;'. $row['attributename'] .'&lt;/td&gt;? Well you can easily just put fixed text, e.g. &lt;td&gt;some text here&lt;/td&gt;, but is that what you really mean? Or do you want to output text which changes depending on the value which came from the database? If so then you could a) use a CASE statement in the SQL or b) use swich or if/else blocks in the PHP, or c) use a lookup via an associative array in PHP, or d) use a lookup via a JOIN to another table in the SQL query. There are probably other ways I haven't thought of. Where are you stuck exactly?
  • I have also different values in that table, I want to output text which changes depending on the value which came from the database. But I don' know how to do it.
  • Well I've just suggested 4 different ways you could potentially do it...

标签: php


【解决方案1】:

I probably should usedswitchorif/elseas was proposed, but after all i've done it withreplaceinspired from here.

     echo '<tr><td>'.'<img src="data:image/jpeg;base64,'.base64_encode($row['attributeimage']).'" style="width: 8vw; height: 8vw; " />'.'</td>
               <td>'. $row['attributevalueEN'] .'</td>
               <td>'.  str_replace(['old_expression1', 'old_expression2'], ['new_expression1', 'new_expression2'], $row['attributename']) .'</td>
           </tr>';

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 2022-12-02
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 1970-01-01
    相关资源
    最近更新 更多