Solved

PHP Adding/Subtracting

Posted on 2011-03-09
10
237 Views
Last Modified: 2012-05-11
Hi experts,

I have a database table that looks like the following.

Customer Jan        Feb     March    April     May   June July Aug Sep etc
Jan          10.12    11.00    15.00    12.56    -36.25   ETCC

As you can see there is a minus under may of -36.25...is there away of setting a negative value to the database?
0
Comment
Question by:NeoAshura
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35083092
What do you mean by this "is there away of setting a negative value to the database?". Are you trying to say that you cannot store a negative number in the database?

Make sure the DB column is SIGNED or at least that it is not UNSIGNED
0
 
LVL 27

Expert Comment

by:yodercm
ID: 35083113
Just give your database field the characteristic of SIGNED, and you can put negative numbers into it.
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35083146
sorry bad english they are set as negative, and ive eneted the value as -36 so should recongise it, i just dont know how to get the result (sum) of the columns at the end in php
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35083194
"...they are set as negative.."

What are set as negative? I'm sorry but I still do not understand what the problem is. I can see that the sample table above has -36.25 in it, but I'm not clear what you are trying to do with it.

0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35083205
im trying to add the posotives in php with the data from the database

e.g

10+10+10 but then need to -minus the negatives for example

10+10+10-36= £-6
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35083327
Can you post the code that's not doing the adding correctly? It should just be simple arithmetic operations which is why I'm struggling to see where the problem would lie.
0
 
LVL 27

Expert Comment

by:yodercm
ID: 35083774
If the negative value is already in the database, you can just use SUM in MySQL or simple arithmetic in php.

$a = 3 + 2 - 4;

will give you $a = 1
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35083920
here it is sorry for late reply i keep getting unidentified index maybe im not defineing the variables?

if thats the case how would i add the columns jan and feb?

code is attached thanks again
<?php
session_start();
ob_start();
include 'myphp.php';
?>
<?php
$val = $_GET['val'];
$sql = "SELECT * FROM bill_detail2009  WHERE mobile_number = '" . $val . "'";

$first_number = $_GET['Jan09'];
$second_number = $_GET['Feb09'];
$sum_total = $first_number + $second_number;

$direct_text = 'The two variables added together = ';

print ($direct_text . $sum_total);

?>

Open in new window

0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35084059
You're not processing the database values or even accessing the database. I don't even know if you have establised a database connection at this point.

The following UNTESTED code should be closer to what you need but without knowing you database table structure I can only guess....

<?php
session_start();
ob_start();
include 'myphp.php';
?>
<?php
$val = $_GET['val'];
$sql = "SELECT * FROM bill_detail2009  WHERE mobile_number = '" . $val . "'";



$rs = mysql_query( $sql );

if ( $rs ) {

     if ( mysql_num_rows($rs) == 0 )
          echo "NO data to process";
     else {
          $rw = mysql_fetch_assoc( $rs );

          $sum_total = $rw['Jan09'] + $rw['Feb09'];
     }
}
else
     echo "Query failed " . mysql_error();
     



$direct_text = 'The two variables added together = ';

print ($direct_text . $sum_total);

?>

Open in new window

0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35084250
myphp was my connection file and thanks it worked ok
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AJAX Wordpress Not Reading Variable 2 56
MySQL-Design Help 12 39
Why is my $_POST not going to results page 10 35
How do I remove "" from json_encode 5 19
Creating and Managing Databases with phpMyAdmin in cPanel.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question