phpmysqlmoney-format

Money format in PHP table taking the price from a Database


In this Image you can see how my table currently displays, with the price of the items being collected from a mysql database, and displays just as '500' or how ever much the item is I want it to display '£500' I have tried number format and I have tried with and without a locale but still get the same error Currently have it with $ just to test*

Thanks for any help ! :D

    include 'db_connection.php';

    $conn = OpenCon();

    echo "Connected Successfully";
 setlocale(LC_MONETARY, 'en_US');

        $sql = "SELECT * FROM Product, Manufacturer Where Product_ID = Manufacturer_ID";
        $result = $conn->query($sql);
        if ($result->num_rows > 0){
            echo "<table>
                <tr>
                    <th>Product ID</th>
                    <th>Name</th>
                    <th>Product Description</th>
                    <th>Price</th>
                    <th>Image</th>
                    <th>Manufacturer Name</th>
                    </tr>";
            while($row = $result->fetch_assoc()){
                echo "<tr>
                    <td>".$row["Product_ID"]."</td>
                    <td>".$row["Name"]."</td>
                    <td>".$row["Product_Description"]."</td>
                    <td>" '$'.number_format(floatval($row["ProductPrice"]));."</td>
                    <td><img src='images/".$row['ProductImage']."'></td>
                    <td>".$row["ManufacturerName"]."</td>
                </tr>"; 
            }
            echo "</table>";
        } else {
            echo "0 results";
            }

        $conn->close();

Solution

  • You're getting the syntax error because you're missing a . between the two strings (the double-quoted string containing the <td> and the single-quoted string containing the $).

    You don't need to create a new string for your currency symbol, just add it to the string you're already echo'ing:

    echo "<tr>
        [...]
        <td>&pound;" . $row["ProductPrice"] . "</td>
        [...]
    </tr>";