-2

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

enter image description here

and I'm looking to display a custom text instead of the text marked in the above print screen. My question is: there is a way to override the value returned from my table and define a custom text to be displayed for each fetched value? How? Thanks.

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 `'. $row['attributename'] .'`? Well you can easily just put fixed text, e.g. `some text here`, 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? – ADyson Nov 15 '22 at 13:33
  • 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. – Szabolcs Horvath Nov 15 '22 at 13:54
  • Well I've just suggested 4 different ways you could potentially do it... – ADyson Nov 15 '22 at 13:58

1 Answers1

1

I probably should used switch or if/else as was proposed, but after all i've done it with replace inspired 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>';