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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

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

Post 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])?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
RadixDev
Forum Commoner
Posts: 66
Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.

Post 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)
mishal
Forum Newbie
Posts: 12
Joined: Sat Apr 10, 2004 4:23 pm

Post 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);
Last edited by mishal on Tue Apr 13, 2004 11:12 pm, edited 1 time in total.
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

Post by bytte »

thanks for the quick and excellent replies.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

... also you don't have to unset each variable since their scope stays within the function.
RadixDev
Forum Commoner
Posts: 66
Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.

Post by RadixDev »

mishal, looks like we posted at the same time :lol:
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

Post 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.
Post Reply