1

I have successfully done the php script that connect to my database and return desired data. But i need to extend this script to insert a variable that will show the price with some calculations (tax,no-tax,margin etc.).

This script show me only price value of the first database row 0.00 in all fetched rows and its not correct - for the first database row is ok because product have 0.0000 price, but the other rows are filled with correct values. It seems like the while loop dont like my $styledprice variable . I can't figure how to show correct field values in all lines. Any ideas much apppreciated? I'm a PHP beginner!

$pricequery = "SELECT rrp FROM my_products";
$b2bprice = mysql_query($pricequery);
$rrps = mysql_fetch_array($b2bprice); 
$price = $rrps['rrp'];
$styledprice = number_format($price, 2, '.', '');

$query = mysql_query("
SELECT 
CONCAT(' <td> ',p.id,' </td><td> ',p.manufacturer,' </td><td> ',p.reference,' </td><td> ',p.name,' </td><td> ',p.quantity,' <td> ','".$styledprice."',' </td> ') AS row
FROM my_products p
");

echo "
<table>
    <tr>
        <td><h5>ID</h5></td>
        <td><h5>Manufacturer</h5></td>
        <td><h5>PN</h5></td>
        <td><h5>Name</h5></td>
        <td><h5>Quantity</h5></td>
        <td><h5>Price</h5></td>
    </tr>";

while($row=mysql_fetch_array($query))
    {
        echo "<tr>".$row['row']."</tr>";
    }

 echo "
 </table>";

Yes i know about mysql_ functions that are deprecated.

6
  • 1
    it's not common to see html being mixed into the sql statement itself Commented Aug 12, 2016 at 8:17
  • 1
    I'm surprised to see such query being included with html tags. Commented Aug 12, 2016 at 8:18
  • @RamRaider but it works ;) Commented Aug 12, 2016 at 8:18
  • Yes, don't use HTML in your SQL query. Just retrieve the column values with SQL and then add the HTML in PHP. You need to use the number_format() inside the while loop on the price retrieved from MySQL. Commented Aug 12, 2016 at 8:19
  • it works? really - how many results does it fetch / should it fetch? You could do that in one sql command rather than two Commented Aug 12, 2016 at 8:21

2 Answers 2

2

Something like this should work better. It seperates the resultset from the database and the output via HTML.

// Get the Result Set
$result =  mysql_query("SELECT p.id, p.manufacturer, p.reference, p.name, p.quantity FROM my_products p");

// Convert the rows and columns from the Result Set to a PHP Array
$data = array(); // empty array
while ($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
}

// Now you have access any row or column
echo "<table>";
foreach($data as $row){

    // prepare the data
    $formatttedQuantity = number_format($row['quantity'], 2, '.', '');

    // show each Table Row

    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['manufacturer'] . "</td>";
    echo "<td>" . $row['reference'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $formatttedQuantity . "</td>";
    echo "</tr>";
}
echo "</table>";
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for Your effort @Cagy79 ! I will check it within a minutes!
Top notch! This is exactly what i need and with this one i can make a lot of flexible modifications! +1 and Answer
1

It looks like you can do this all in one sql command like:

<?php

    $sql = "SELECT * FROM my_products";
    $result = mysql_query( $sql );

    echo "
    <table>
        <tr>
            <td><h5>ID</h5></td>
            <td><h5>Manufacturer</h5></td>
            <td><h5>PN</h5></td>
            <td><h5>Name</h5></td>
            <td><h5>Quantity</h5></td>
            <td><h5>Price</h5></td>
        </tr>";

    while( $row=mysql_fetch_array( $result ) ){

        $price=$row['rrp'];
        $styledprice = number_format( $price, 2, '.', '' );

        echo "
            <tr>
                <td>{$row['id']}</td>
                <td>{$row['manufacturer']}</td>
                <td>{$row['reference']}</td>
                <td>{$row['name']}</td>
                <td>{$row['quantity']}</td>
                <td>{$styledprice}</td>
            </tr>";
    }

     echo "
     </table>";

 ?>

1 Comment

Thanks @RamRaider! Looks very simple! I will check it! :)

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.