我有一个带有“itemLevels”(数字)的mysql数据表。因此,如果我回应了项目级别,我希望它们被某个文本替换。
itemLevels 1 =>文本1; itemLevels 2 =>文本2; itemLevels => Text3
目前我有以下代码:
<?php
$con = mysqli_connect("localhost","XXXXXX","XXXXX","XXXXX");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
/* change character set to utf8 */
if (!mysqli_set_charset($con, "utf8")) {
printf("Error loading character set utf8: %s\n", mysqli_error($con));
exit();
} else {
}
$sql = "SELECT * FROM TEST WHERE name=test";
$result = mysqli_query($con,$sql)or die(mysqli_error());
echo "<table>";
while($row = mysqli_fetch_array($result)) {
$name= $row['name'];
$itemLevel= $row['itemLevel'];
echo
"<tr><td class='name'>".$name."</td></tr>
<tr><td class='name'>Gegenstandsstufe ".$itemLevel."</td></tr>
</tr>";
}
echo "</table>";
mysqli_close($con);
?>
我读过“php str_replace”,但我不确定这是不是正确的想法以及如何在我的代码中使用它。
答案 0 :(得分:1)
您可以使用数组
$itemLevelNames = [
0 => 'Text0',
1 => 'Text1',
2 => 'Text2',
42 => 'Text42',
// and so on
];
然后通过
输出文本echo $itemLevelNames[$itemLevel];