【发布时间】: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
<td>'. $row['attributename'] .'</td>? Well you can easily just put fixed text, e.g.<td>some text here</td>, 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) useswichorif/elseblocks 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