duanhe0817825 2015-07-08 21:26 采纳率: 100%
浏览 19
已采纳

使用str_replace在html中翻译MySQL表


I have a website which contains a pricelist of CS:GO items. Now, how I did is this:

MySQL DB w/ All prices --> SELECT FROM DB Where X ( Item name ) --> echo Table

( Works fine )


Now I want to do a translation of the item names. How I planned to do it:

MySQL DB --> $result= From Where ( signature character ) --> $translate = str_replace (signature character from $result)

However, there's an issue with the code or str_replace I think.

$translate returns $result with 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 FROM utf WHERE Name LIKE '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 that 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 FROM paid WHERE Name LIKE '%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();
?>
  • 写回答

1条回答 默认 最新

  • duanputian5341 2015-07-08 21:35
    关注

    You can use SQL's REPLACE() function:

    $sql = "SELECT REPLACE(Name, 'K', 'Karambit') AS Name, Price FROM utf WHERE Name LIKE 'K%'";
    

    If you want to do it in PHP, you have to do it after fetching the 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>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思