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:[EMAIL PROTECTED]
----------------------------------------------------------------------
Simple things should be simple and hard things
should be possible.
----------------------------------------------------------------------


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to