GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Sep-2003, 15:13
sujon sujon is offline
New Member
 
Join Date: Sep 2003
Location: Italy
Posts: 3
sujon is an unknown quantity at this point

new in this area


CAN ANY ONE HELP ME??
I'M PRETTY NEW IN PHP MYSQL I HAVE ONE TABLE AND IN THE TABLE
HAVE 5 COLUMN I WANT TO COUNT THE VALUE OF 4 COLUMN AND THAT WILL
SHOW IN ANOTHER COLUMN JAST LIKE THIS :

-----------------------------------------
-------ROW1 | R2| R3| R4 | R5-----------
-----------------------------------------
----------1 | 1 | 1 | 1 = 4 < COL1 TOTAL
-----------------------------
----------2 | 2 | 2 | 2 = 8 < COL2 TOTAL
-----------------------------------------
-----------------------------------------
----------3 | 3 | 3 | 3 | 12 < ROW TOTAL


PLEASE HELP ME
BEST REGURDS
  #2  
Old 13-Sep-2003, 23:03
conkermaniac conkermaniac is offline
Member
 
Join Date: Dec 2001
Location: China
Posts: 174
conkermaniac is on a distinguished road
Hi sujon,

Would you be looking for something like this?

PHP Code:

$result = mysql_query("SELECT r1,r2,r3,r4 FROM table1");
while ($row = mysql_fetch_array($result)) {
  $r5 = $r1 + $r2 + $r3 + $r4;
  mysql_query("INSERT INTO table1 (r5) VALUES ('$r5')");
} 



That should work, but I might have done something wrong there...perhaps someone better at this stuff can check it for me.
__________________
You're not supposed to be looking at this.
  #3  
Old 14-Sep-2003, 05:46
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Hello sujon,

There are 2 schools of thought on this; there are those that 'work-the-script (PHP)' and then there are those that 'work-the-db (MySQL)'.

I prefer the latter... i.e. I prefer to get MySQL to do the dirty work if it can and if it's appropriate for the rest of the script.

Conkermaniac's example is a sample of the 'work-the-script'-type solution.

Here's one that's 'working-the-db':

PHP Code:

<?php
// I assume the data is off a form?
$d1 = intval( $_POST['d1'] );
$d2 = intval( $_POST['d2'] );
$d3 = intval( $_POST['d3'] );
$d4 = intval( $_POST['d4'] );

// I assume you want to insert these 'data' ($d1..$d4) into the db table...
// `r5` is the field where the total of the other 4 cols are 

$sql = "INSERT INTO `table1`
       ( `r1`, `r2`, `r3`, `r4`, `r5` )
       VALUES
       ( $d1, $d2, $d3, $d4, `r1`+`r2`+`r3`+`r4` )";    
?>

  #4  
Old 03-Oct-2003, 06:09
misunderstood misunderstood is offline
Member
 
Join Date: Jun 2003
Posts: 121
misunderstood is on a distinguished road
Can further calculations be carried out by using this method ?

In the example
PHP Code:

<?php
Blah Blah...
$sql = "INSERT INTO `table1`
       ( `r1`, `r2`, `r3`, `r4`, `r5` )
       VALUES
       ( $d1, $d2, $d3, $d4, `r1`+`r2`+`r3`+`r4` )";    
?>


Can I add r1 + r2 + r3 then divide by r4 ?
I have tried a number of different ways to get that to work but to no avail. :-(
But as usual I will probably be missing a simple piece of code.
  #5  
Old 03-Oct-2003, 12:03
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Yes it's possible... but you know SQL, one typo in the statement and all hell breaks lose!
  #6  
Old 03-Oct-2003, 12:09
misunderstood misunderstood is offline
Member
 
Join Date: Jun 2003
Posts: 121
misunderstood is on a distinguished road
Would I bracket the seperate instructions ie
PHP Code:

( $d1, $d2, $d3, $d4, (`r1`+`r2`+`r3`)/(`r4`) )"; 



or
PHP Code:

( $d1, $d2, $d3, $d4, `(`r1`+`r2`+`r3`)`/`(`r4`)` )"; 


Or am I still in the wrong direction?
  #7  
Old 03-Oct-2003, 12:41
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
The INSERT statement I can suggest (considering everything is set up OK with your table) is:

PHP Code:

<?php

$sql = 'INSERT INTO `sometable`
       ( `r1`, `r2`, `r3`, `r4`, `r5` )
       VALUES
       ( '.$d1.', '.$d2.', '.$d3.', '.$d4.', ((`r1`+`r2`+`r3`)/`r4`) )';
?>

  #8  
Old 03-Oct-2003, 12:43
sujon sujon is offline
New Member
 
Join Date: Sep 2003
Location: Italy
Posts: 3
sujon is an unknown quantity at this point

10q every one


hi thanx every one its working fine
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What Search Engines are you listed in? NetWay Search Engine Optimization Forum 6 23-Sep-2003 09:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:27.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.