Question

So I have this table filled with a row (we call it $numbers for now) containing numbers greater than 100 000. For example 581 249

I want to echo these values on-screen (using PHP) with a MySql query. Only do I want to divide the returned value by 1000...

The idea is this:

I have made a horizontal bar which is the length of $numbers. But the width of this bar is the value of $numbers in pixels... Basically, it creates a bar with a with of (for example) 581 249 pixels :') ...

This is obviously not the goal. So I have chosen to divide this value by 1k. In this way, the bar fits better on-screen.

How do I implement this in my MySql query?

The echo code for the query is this:

if(isset($_POST['submit'])){
$jaar = $_POST['year'];
echo "<CENTER><h1>".$_POST['year']."</h1></CENTER>";
$result2 = mysql_query("SELECT week, SUM(numbers), SUM(orders) FROM productie WHERE year =" . $year . " GROUP BY week"); 
while($row=mysql_fetch_array($result2) or die(mysql_error())){
    echo "<div class='BarTable'>
<table>
    <tr>
        <td><div class='BarLabel''>".$row['week']."</div></td>
        <td class='BarFull'>
            <img src='images/bargraph/month_bar.png' height='12' alt='320' width=".$row['SUM(numbers)']." />
            <p>".$row['SUM(numbers)']."</p>
        </td>
    </tr>
</table>

This while loop goes trough the database, and creates a new bar for every new week found in the database. In this way my result is a nice horizontal bar graph with all the weeks (with the corresponding $numbers) under eachother.

The only problem I'm still facing (as said before), isi that the bars are way too wide. Now I want to decrease the size, but still keep the entered value (since the value of $numbers is displayed behind the bar).

How to divide this value by 1000?

Was it helpful?

Solution

Without mentioning that you should be using PDO or mysqli for new projects, I think what you're looking for is this:

SELECT week, ROUND(SUM(numbers)/1000) as thousandths, SUM(orders) ...

And then access it in your array with $row['thousandths']

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top