Page 1 of 1

a lot of functions... do i have to write em all?

Posted: Tue Apr 13, 2004 5:43 pm
by bytte
I have a database with a LOT of fields. I need to have a function for each field like this:

Code: Select all

function S_00_CP_J_ABVV ($zoekcriteria) {
			db();
			$query = "SELECT S_00_CP_J_ABVV FROM svbedrijf" . $searchcriteria;
			$queryexe = mysql_query ($query);
			$S_00_CP_J_ABVV = 0;
			while ($row = mysql_fetch_row($queryexe)) {
				$S_00_CP_J_ABVV = $S_00_CP_J_ABVV + $row[16];
				}
			echo $S_00_CP_J_ABVV;
			db_close();
			unset($query);
			unset($queryexe);
			unset($row);
			}
As you might have guessed the strange name (S_00_CP_J_ABVV) is the field name. As I need exactly the same function for every field, I wondered if there wasn't a quick way to code instead of typing the above function again and again with only some varieties (function name = name of field, $S_00_CP_J_ABVV, and the number of the row ($row[16])?

Posted: Tue Apr 13, 2004 5:51 pm
by markl999
As I need exactly the same function for every field
Can you provide some context for needing this as i can't imagine a situation where you'de need it.

Posted: Tue Apr 13, 2004 5:54 pm
by RadixDev
Goin' against your rule but...

Code: Select all

<?php
function whatever($searchcriteria, $field,$rownum) {
  db();
  $query = "SELECT ".$field." FROM svbedrijf".$searchcriteria;
  $queryexe = mysql_query ($query) or die(echo mysql_error());
  $search[$field] = 0; 
  while ($row = mysql_fetch_row($queryexe)) { 
    $search[$field] = $search[$field] + $row[$rownum]; 
  } 
  echo $search[$field]; 
  db_close(); 
  unset($query,$queryexe,$row);  // Neater this way!
}
?>
Now all you have to do is pass the search criteria and the name of the field and the row number (16)

Posted: Tue Apr 13, 2004 5:59 pm
by mishal
You don't need to define functions for each fields, the best way is to have a standard function like :

Code: Select all

function FuncName($field_name,$table_name, ... $other_vars){
         db(); 
         $query = "SELECT ".$field_name." FROM ".$table_name." " . $searchcriteria; 
         $queryexe = mysql_query ($query); 
         $variable = 0; 
         while ($row = mysql_fetch_row($queryexe)) { 
            $variable = $variable + $row[16]; 
            } 
         echo $variable; 
         db_close(); 
         unset($query); 
         unset($queryexe); 
         unset($row); 
         }
then call this fuction whenever you need to get the results :

Code: Select all

FuncName('FIELD NAME','TABLE_NAME', ... $other_vars);

Posted: Tue Apr 13, 2004 6:09 pm
by bytte
thanks for the quick and excellent replies.

Posted: Tue Apr 13, 2004 6:23 pm
by andre_c
... also you don't have to unset each variable since their scope stays within the function.

Posted: Wed Apr 14, 2004 1:48 am
by RadixDev
mishal, looks like we posted at the same time :lol:

Posted: Wed Apr 14, 2004 9:08 am
by bytte
andre_c wrote:... also you don't have to unset each variable since their scope stays within the function.
thanks, I doubted about that so i've unset them.
i'll remove it now.