Help with a PHP script

Discussion in 'PHP' started by virtualmisc, Mar 3, 2010.

  1. #1
    Hello Friends


    I'm trying to build a PHP function that gets the values of the first and second rows of a table and subtract them. Then the function gets the second and third values and subtract them, then the third and forth and so on until the end of the table. (The table will be sorted ASC and the value of the subtraction will be use for posterior use) Is this possible?


    Thanks
     
    virtualmisc, Mar 3, 2010 IP
  2. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #2
    I think this is what you trying to do
    
     $sql="SELECT value FROM table";
     $query=mysql_query($sql);
     $numRows=mysql_num_rows($query);
     $count=0;
    	while($r=mysql_fetch_array($query)){ //build an array with the values
    		$array[$count]=$r['value'];
    		$count++;
    	}// end while
    	for($i=1; $i<$numRows; $i++){
    		$totalArray[$i-1]=$array[$i]-$array[$i-1]; //subtract the rows
    	}//end for
    
    PHP:
    $totalArray as the results of the subtractions
     
    Narrator, Mar 3, 2010 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    if your going to do that in sql your are referring to stored procedures.. though theres a way in querying but it will be slower than a regular query..
    use php for the computation as what Narrator posted.
     
    bartolay13, Mar 4, 2010 IP