0

I have a graph that auto fills based on a few values. The values are the goal (total) the current (where it is at now) and in the total height of the graph. I am trying to pull the goal and current out of the database format it into money and it should fill the graph to the point it needs to be.

I had it working with a URL get which was ?current=584559096&goal=1000000000 and I just used $_GET. For easier maintenance for others around I want to pull it from a database which can be updated a post.

The code I have so far is:

    <? php function formatMoney($number, $fractional=false) {
        if ($fractional) {
            $number = sprintf('%.0f', $number);
        }
        while (true) {
            $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
            if ($replaced != $number) {
                $number = $replaced;
            } else {
                break;
            }
        }
        return $number;
    }

        $goal = $row['goal'];
        $current = $row['current'];
        $percent = round($current / $goal * 100);
        $totalheight = 216;
        $ourheight = $totalheight * $percent / 100;

        $halfofarrowheight = 12;
            $arrowheight = $totalheight-$ourheight-$halfofarrowheight; 
            if($arrowheight < 0)
                $arrowheight = 0;
    mysql_close($con);
?>

Here is my query

mysql_select_db("databasename", $con);

$result = mysql_query("select * from `dbname`.`tablename");

This is my error Warning: Division by zero in E:\inetpub\wwwroot\test.website.net\web\includes\globalfoot.php on line 36

Line 36 is the $percent = round($current / $goal * 100);

So, I been fooling with this thing for 4 days now trying to just get the numbers out of the goal column and current column format them to money and have the chart fill. For test sake lets say the goal is 1000000000000 and the current is 5000000000.

1
  • Where is $row getting set? Can you show that code? Commented Jul 29, 2011 at 16:23

3 Answers 3

2
$row['goal'] = $goal;
$row['current'] = $current;

Please forgive me if I haven't understood your code, but shouldn't the above be:

$goal=$row['goal'];
$current=$row['current'];
Sign up to request clarification or add additional context in comments.

8 Comments

Yeah, I switched them around to see if it would do anything and forgot to switch it back. No change on the error.
again, not sure if i'm teaching my granny to suck eggs but have you tried just echoing the values of those two vars prior to the calculation? a division by zero warning suggests that $goal is zero or empty, you should verify that it gets set
Yes I did, I did they values come up on with an while ($row = mysql_fetch_array($result)) echo $row['goal'] and $row['current '].
but what about just doing echo $row['goal']?
@ Dmitriy I don't understand, through out the rest it is just used as an <?php echo ($arrowheight)); ?>, <?php echo($totalheight-$ourheight);?>, <?php echo($ourheight);?>, <?php echo(formatMoney($current)); ?>, in the css and on top of the arrow. But the error is saying that it is not reading it from the table.... even though it will echo it on test.
|
0

Your code should look like this:

mysql_select_db("databasename", $con);
$result = mysql_query("select * from `dbname`.`tablename");
if (!$result || !($row = mysql_fetch_assoc($result))) {
    echo "DB error: ".mysql_error();
    exit;
}

$goal = $row['goal'];
$current = $row['current'];
// ...

4 Comments

So, maybe there is something with your query? See updated code
@ Dmitriy yeah it worked, I had missed a ; which is always a pain with PHP miss one little thing and it ruins your day.
Wish I could check both of the answers because you both helped.
Thank you very much for your help! I like the feeling when stuff actually works.
0

Always, always double-check your variables when doing division.

$percent = ($goal > 0 || $goal < 0) ? round($current / $goal * 100) : 0;

2 Comments

Thank you Matt that got rid of the error. For some reason though my graph is not working still. Does not even echo the row column on the arrow. It is like it does not even read the db, even though the test echo shows it will pull the numbers out. Do I need to close the connection at the bottom of the page?
Wish I could check both of the answers because you both helped.

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.