php - Represent image form database
问题描述
I’d like to represent an image stored in a database every time I press a button. The column name is “solution_path”. In my code I can represent the image outside the button, but I would like it to appear when I press the button. Part of the code is shown below. I know how to represent images taken from the database, but in this case, I do not know why it’s not working. Can you help me? I am already connected to the database and to the table I need.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";
$conn = new mysqli($servername, $username, $password, $dbname);
/*echo*/ $id=$_GET['id'];
$sql = "SELECT * FROM exercises where exercise_id='$id'";
$result = $conn->query($sql); /*Check connection*/
?>
<div id="centered_B" class="header">
<?php
while($id = $result->fetch_assoc())
echo '<h1>' . $id["exercise_id"]. ". " . $id["title"] . '</h1>' . "<br>" . '<p>' . $id["text"] . '</p> <img width="603" height="auto" src="' . $id["image_path"] . '"><br><br>
<input type="radio" name="choice" value= "1" /><img width="550" height="auto" src="' . $id["image_path_A"] . '"/>
<input type="radio" name="choice" value= "2" /><img width="550" height="auto" src="' . $id["image_path_B"] . '"><br>
<input type="radio" name="choice" value= "3" /><img width="550" height="auto" src="' . $id["image_path_C"] . '"><br>';
/*var_dump($id)*/
?>
<button onclick="solutionFunction()" class="button_Solution" >Solution</button>
<div id="solution" style="display: none;">
This is the solution.
<img width="500" height="auto" src="<?php echo $id["solution_path"]; ?>" >
</div>
<script>
function solutionFunction() {
var x = document.getElementById("solution");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
解决方案
Giving my comment as an answer for you to close your question with ;)
If you are only dealing with one result row for that specific page build, you should get rid of the while
loop and simply assign the one result row to your $id
variable, to use throughout the page.
So change this one line:
while($id = $result->fetch_assoc())
To just:
$id = $result->fetch_assoc();
Now $id
will persist through the page generation, and your lower echo of $id["solution_path"]
should present you with the solution image url string you need.
The reason your original code was not working, is a while
has the specific behavior of 'wiping out' the assigned variable on the last loop iteration (thus causing the condition to turn 'false' and end the loop).
Also, since you did not have curly braces, that while
was only controlling the immediate following echo
line. This then meant the further echo you had for the $id["solution_path"]
variable, would have been echoing a null result. If you had php error reporting fully enabled, you may had seen a warning about undefined index too.
I hope that helps you out!
推荐阅读
- r - 时间序列自相关函数
- docker - Strapi docker中本地文件更改权限不足
- python - 如何将两个列表组合成一个json,其中包含一个字典,里面有一个python中的键值对(对于django)?
- javascript - HTML/CSS/JS 到 iOS 应用和应用安装
- r - 如何将面板数据中的列相乘?
- python - 我想在 python 中使用 pandas 对 csv 文件进行排序
- javascript - 使用百里香片段时出现“无法读取属性”错误
- python - 有效地按其值缓冲栅格
- php - 试图将json从表转移到花药php
- r - 为什么 gsynth 有时会因“'data' must be of a vector type, was 'NULL''”而失败?