PDA

View Full Version : [PHP] Maths + MySQL?



Calon
05-10-2008, 07:37 PM
id amount time date miscdesc
1 £7.64 20:32 05/10/2008 Initial start-off amount

There will be more columns, but what code would I use to get the total of all the digits added together (I know I will remove the £)

Joe!
05-10-2008, 08:18 PM
use mysql_fetch_array, add them using array_sum(). There might be an easier way, i'm not brilliant at php ;p

Hypertext
05-10-2008, 08:18 PM
Your not making yourself clear, can you give an example?

I presume you want 764203205102008 if your taking all the digits out.

Use str_replace after concatenating each of the strings in a variable.

@above:

array_sum needs integers or floats (php.net).

So. heres the code to do that.



$str = '';
$row = mysql_fetch_array($query);
foreach($row as $key => $val) {
$str .= $val; // TESTME
}
$unwantedSymbols = array('£', ':', '/'); // Change to unicode values
foreach($unwantedSymbols as $unwantedSymbol) {
$str = str_replace($unwantedSymbol, '', $str);
}



Test this.

Joe!
05-10-2008, 08:25 PM
Ah okay, thanks :)
Maybe he could strip all the symbols out using some function? and then add them?

Hypertext
05-10-2008, 08:28 PM
Ah okay, thanks :)
Maybe he could strip all the symbols out using some function? and then add them?
My code above should work.

Calon
05-10-2008, 08:37 PM
well say i have 8 rows which are different numbers, what code would I create/or functions to sum them all up (+)

Hypertext
05-10-2008, 08:39 PM
well say i have 8 rows which are different numbers, what code would I create/or functions to sum them all up (+)

Your still not making yourself clear, could you give us an example table, and an example output of what you'd like?

Calon
05-10-2008, 08:41 PM
Your still not making yourself clear, could you give us an example table, and an example output of what you'd like?
Table: money
Columns: amount, id, miscdesc

1: 7.50
2: 7.50
3: 7.50

Well, how ever many columns I end up with..

Something to add the columns together giving me a total number of the amount field.

Hypertext
05-10-2008, 08:43 PM
:rolleyes:

I'll say it the third time. Example output?

Calon
05-10-2008, 08:45 PM
:rolleyes:

I'll say it the third time. Example output?
:S I don't understand what you want.

Output: 22.50 i think

Hypertext
05-10-2008, 08:47 PM
OK Show me a row like you said:

Columns: amount, id, miscdesc

1: 7.50
2: 7.50
3: 7.50

With three columns? Yet you have 2? Then an example output as in what do you want the code to return to you...

Calon
05-10-2008, 08:50 PM
OK Show me a row like you said:

Columns: amount, id, miscdesc

1: 7.50
2: 7.50
3: 7.50

With three columns? Yet you have 2? Then an example output as in what do you want the code to return to you...
http://www.tehupload.com/uploads/59641f28adce48bmysql.png
I want the code to return me the data of the amount column in a total.

Example output:

£7.70

Hypertext
05-10-2008, 08:54 PM
Oh.

Wow. Thats basic.


$amount = array();
$i = 0;
while($row = mysql_fetch_array($query) {
$amount[$i] = str_replace('£, '', $row['amount']); // get the unicode for sterling sign
++$i;
}


This will create an array of those values.

Calon
05-10-2008, 08:56 PM
Oh.

Wow. Thats basic.


$amount = array();
$i = 0;
while($row = mysql_fetch_array($query) {
$amount[$i] = str_replace('£, '', $row['amount']); // get the unicode for sterling sign
++$i;
}


This will create an array of those values.



<?php
//mysql login code was here lol

$query = mysql_query("SELECT * FROM `money`")
$amount = array();
$i = 0;
while($row = mysql_fetch_array($query) {
$amount[$i] = str_replace('£, '', $row['amount']); // get the unicode for sterling sign
}
?>

Excellent2
05-10-2008, 09:00 PM
Hypertext forgot to indentate!!

Calon
05-10-2008, 09:04 PM
Parse error: syntax error, unexpected T_VARIABLE in /home/calonuk/public_html/money.php on line 6

*Awaits help.*

Hypertext
05-10-2008, 09:04 PM
<?php
//mysql login code was here lol

$query = mysql_query("SELECT * FROM `money`")
$amount = array();
$i = 0;
while($row = mysql_fetch_array($query) {
$amount[$i] = str_replace('£, '', $row['amount']); // get the unicode for sterling sign
}
?>



<?php
//mysql login code was here lol

$query = mysql_query("SELECT * FROM `money`")
$amount = array();
$i = 0;
while($row = mysql_fetch_array($query) {
$amount[$i] = str_replace('£', '', $row['amount']); // get the unicode for sterling sign
}
?>
I missed a closing quote. That should work now.

Hypertext forgot to indentate!!

GTFO.

Calon
05-10-2008, 09:10 PM
<?php
//mysql login code was here lol

$query = mysql_query("SELECT * FROM `money`")
$amount = array();
$i = 0;
while($row = mysql_fetch_array($query) {
$amount[$i] = str_replace('£', '', $row['amount']); // get the unicode for sterling sign
}
?>
I missed a closing quote. That should work now.


GTFO.



<?php
mysql_connect( 'localhost', 'calonuk_trains', '*******' ) or die(mysql_error());
mysql_select_db( 'calonuk_website' ) or die(mysql_error());

$query = mysql_query("SELECT * FROM `money`")
$amount = array();
$i = 0;
while($row = mysql_fetch_array($query) {
$amount[$i] = str_replace('£', '', $row['amount']); // get the unicode for sterling sign
}

?>
Doesn't seem to be working.

http://www.tehupload.com/uploads/59641f28adce48bmysql.png any table stuff or something? I'm really tired and cant think

Excellent2
05-10-2008, 09:12 PM
GTFO.Yeah, you don't like it when people hit back at you do you?

Calon
05-10-2008, 09:14 PM
Yeah, you don't like it when people hit back at you do you?
Please don't flame.. I really need help and you're not helping.

Excellent2
05-10-2008, 09:20 PM
So you're wanting to add the contents of your table?

Calon
05-10-2008, 09:25 PM
So you're wanting to add the contents of your table?
Of the row "Amount", yes.

Calon
05-10-2008, 09:39 PM
<?php
mysql_connect( 'localhost', 'calonuk_trains', '*****' ) or die(mysql_error());
mysql_select_db( 'calonuk_website' ) or die(mysql_error());

$handle = mysql_query("SELECT SUM(amount) AS total FROM `money`");
$row = mysql_fetch_assoc( $handle );
echo '£'.$row['total'] .' out of £300';
?>


Sorted - thanks, Source.

Want to hide these adverts? Register an account for free!