php - Using str_replace to translate a MySQL Table in html -



i have website contains pricelist of cs:go items. now, how did this:

mysql db w/ prices --> select db x ( item name ) --> echo table 

( works fine )


now want translation of item names. how planned it:

mysql db --> $result= ( signature character ) --> $translate = str_replace (signature character $result) 

however, there's issue code or str_replace think.

$translate returns $result no filtering.

        <?php require 'lang.php'; $servername = "localhost"; $username = ""; $dbname = ""; $password = "";  // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }   // filter name $sql = "select name, price utf name 'k%'"; $result = $conn->query($sql);  // translate $tr = str_replace('k','karambit','$result');  // echo translation ( testing purposes ) if (isset($tr)) {     echo "<tr><td>".$tr."</td><td>";  }  //draw   if ($tr->num_rows > 0) {     echo "<div class=\"csstablegenerator style=\"width:600px;height:150px;\"><table><tr><th>name</th><th>price</th></tr></div>";     // output data of each row     while($row = $tr->fetch_assoc()) {         echo "<tr><td>".$row["name"]."</td><td>".$row["price"]." ".$row["trend"]."</td></tr>";     }    echo "</table>"; } else {     echo "0 results"; }  //close  $conn->close(); ?> 

original code if matters:

<?php $servername = "localhost"; $username = ""; $dbname = ""; $password = "";  // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }   $sql = "select name, price paid name '%kar%'"; $result = $conn->query($sql);  if ($result->num_rows > 0) {     echo "<div class=\"csstablegenerator style=\"width:600px;height:150px;\"><table><tr><th>name</th><th>price</th></tr></div>";     // output data of each row     while($row = $result->fetch_assoc()) {         echo "<tr><td>".$row["name"]."</td><td>".$row["price"]." ".$row["trend"]."</td></tr>";     }     echo "</table>"; } else {     echo "0 results"; } $conn->close(); ?> 

you can use sql's replace() function:

$sql = "select replace(name, 'k', 'karambit') name, price utf name 'k%'"; 

if want in php, have after fetching row:

while($row = $result->fetch_assoc()) {     $name = str_replace('k', 'karambit', $row['name']);     echo "<tr><td>".$name."</td><td>".$row["price"]." ".$row["trend"]."</td></tr>"; } 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -