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
bytte
Forum Commoner
Posts: 75 Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium
Post
by bytte » Tue Apr 13, 2004 5:43 pm
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])?
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Tue Apr 13, 2004 5:51 pm
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 » Tue Apr 13, 2004 5:54 pm
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 » Tue Apr 13, 2004 5:59 pm
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 » Tue Apr 13, 2004 6:09 pm
thanks for the quick and excellent replies.
andre_c
Forum Contributor
Posts: 412 Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah
Post
by andre_c » Tue Apr 13, 2004 6:23 pm
... 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 » Wed Apr 14, 2004 1:48 am
mishal, looks like we posted at the same time
bytte
Forum Commoner
Posts: 75 Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium
Post
by bytte » Wed Apr 14, 2004 9:08 am
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.