Re: Cannot Redeclare Function

From: Date: Thu, 19 Sep 2002 17:58:23 +0000
Subject: Re: Cannot Redeclare Function
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-116897@lists.php.net to get a copy of this message
Looks like you may not redeclare functions generally. Because the function tbl2array can't be any kind of scope, the functions declared inside it will be globally declared. I think the solution is to take the functions like compname() out of tbl2array and declare them "really global". On Thu, 19 Sep 2002 13:28:09 -0230 RMILLER@mail.gov.nf.ca (Robert Miller) wrote: > Hello, > > My function to read a comma separated text file and return a sorted multi-dimensional array > will not work if used more than once per page. > > I know why I'm having this problem. But, I don't know how to solve it. :-( > > > > > > Calling it Twice: > $services = tbl2array ("../locations/service.txt", "default", > $divdetail['id'], "0"); > $collections = tbl2array ("../download/collection.txt", "name", > $divdetail['id'], "4"); > > > The Error: > Fatal error: Cannot redeclare compname() in ../tbl2array.inc on line 35 > > > Function Table 2 Array: > <?php > function tbl2array ($table, $sortby = "default", $keyword = "all", > $keyfield = "0") { > $fpointer = fopen ($table,"r"); > $row = 1; > $leaf = array(); > $heading = array(); > while ($data = fgetcsv ($fpointer,1000)) { > if ($row == 1) { > $heading = $data; > } > else { > $count = 0; > if ($keyword == "all") { > foreach ($data as $value) { > $leaf[$heading[$count]] = $value; > $count++; > } > $result[] = $leaf; > } > elseif (($keyword != "all") && ($data[$keyfield] == $keyword)) { > foreach ($data as $value) { > $leaf[$heading[$count]] = $value; > $count++; > } > $result[] = $leaf; > } > else { > $count++; > } > } > $row++; > } > fclose ($fpointer); > > function compname ($a, $b) { > return strcmp ($a["name"], $b["name"]); > } > > function comptitle ($a, $b) { > return strcmp ($a["title"], $b["title"]); > } > > function compcity ($a, $b) { > return strcmp ($a["city"], $b["city"]); > } > > function compdesc ($a, $b) { > return strcmp ($a["description"], $b["description"]); > } > > function compfile ($a, $b) { > return strcmp ($a["filename"], $b["filename"]); > } > > if ($sortby == "name") { > usort ($result, "compname"); > } > elseif ($sortby == "title") { > usort ($result, "comptitle"); > } > elseif ($sortby == "city") { > usort ($result, "compcity"); > } > elseif ($sortby == "description") { > usort ($result, "compdesc"); > } > elseif ($sortby == "filename") { > usort ($result, "compfile"); > } > > return $result; > } > ?> > > > > ---------------------------------------------------------------------- > Robert J. Miller > Internet Support Specialist > Department of Government Services and Lands > P.O. Box 8700, St. John's, NF., A1B-4J6 > (709) 729-4520 (phone) > (709) 729-4151 (facsimile) > (709) 778-8746 (pager) > > http://www.gov.nf.ca/gsl/ > mailto:rmiller@gov.nf.ca > ---------------------------------------------------------------------- > Simple things should be simple and hard things > should be possible. > ---------------------------------------------------------------------- >

« previous php.general (#116897) next »