1

I have a mysql data table with "itemLevels" (numbers). So if i echo the itemlevels out i want that they get replaced with a certain text.

itemLevels 1 => Text1; itemLevels 2 => Text2; itemLevels => Text3

At the moment i have the following code:

<?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);
?>

I´ve read about "php str_replace" but im not sure if it is the right idea and how to use it in my code.

1

1 Answer 1

1

You can just use an array

$itemLevelNames = [
    0 => 'Text0',
    1 => 'Text1',
    2 => 'Text2',
    42 => 'Text42',
    // and so on
];

And then output the text via

echo $itemLevelNames[$itemLevel];
Sign up to request clarification or add additional context in comments.

2 Comments

Where do i put in the array? Inside the while loop?
@Hertus No, you can put it outside of the loop, because – I assume – the names of the item levels don’t change inside the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.