Crazy Csv and Array insert into table

Hi,

I am trying to get my head around how to do this but to do it i will need to break it down so that you guys understand what i am trying to do… :wink:

Ok, so what i want to do is upload a csv file and insert the contents into a table, including an “array” of authors. Take a look at this page:

http://www.prima.cse.salford.ac.uk:8080/~ibrarhussain/admin/testing.php

If you click on “add author” you can add however many textboxes necessary. (This is just to show you…)

Anyway, i use the following method to insert the array of authors, i do have other columns in the table you will notice.


public function insertAuthor($authArray, $PCorder=0)
    {
        $query = sprintf('SELECT Pid, Pname FROM People WHERE Pname IN(\\'%s\\') ORDER BY Pid ASC', implode('\\',\\'', $authArray));
        $result = mysql_query($query);

        $maxquery = "SELECT MAX(CPid) as max FROM ConfPaper WHERE CPRid = ".$_GET['CPRid'];
        $maxresult = mysql_query($maxquery);
        $max = mysql_fetch_array($maxresult);
        $CPid = $max['max'];

        if($result && mysql_num_rows($result) > 0)
        {
            $sqlValues = array();
            while(list($PId, $PName) = mysql_fetch_row($result))
            {
                if(in_array($PName, $authArray))
                {
                    $sqlValues[] = sprintf("(%d, 1, ".$CPid.", %d, now(), 0)", $PId
                                                               , $PCorder++ );
                    // Author already exists within the Pname table
                    // remove user from $authArray
                    $key = array_search($PName, $authArray);
                    unset($authArray[$key]);
                }
            }
        $sql  = "INSERT INTO PeopleCon(Person_id, PCHid, Pid, PCorder, PCdateadded, PCdeleted) VALUES \
";
        $sql .= implode(",\
", $sqlValues);
        $result = mysql_query($sql);
        }
        // If there are Authors left within the $authArray
        // Add them to the Pname table
        if(count($authArray) > 0)
        {
            People::insertPersons($authArray); // call insertPersons method for remaining authors
            $this->insertAuthor($authArray, $PCorder); // insert the remaining auhtors into PeopleCon
        }
    }

I have got this working successfully…

BUT the problem starts when i want to apply this method when inserting a csv file, i currently have the following code which simply inserts the normal columns:


public function InsertCSVFileToDB(){

	$has_title_row = true;
	$not_done = array();

        if(is_uploaded_file($_FILES['csvfile']['tmp_name'])){
            $filename = basename($_FILES['csvfile']['name']);

            if(substr($filename, -3) == 'csv'){
                $tmpfile = $_FILES['csvfile']['tmp_name'];
                if (($fh = fopen($tmpfile, "r")) !== FALSE) {
                    $i = 0;
                    while (($items = fgetcsv($fh, 10000, ",")) !== FALSE) {

                        if($has_title_row === true && $i == 0){ // skip the first row if there is a tile row in CSV file
                            $i++;
                            continue;
                        }

                        $sql = "INSERT INTO ConfPaper SET
                                CPRid = ".$items[0].",
                                Pid = ".$items[1].",
                                CPtitle = '".mysql_real_escape_string($items[2])."',
                                CPabstract = '".mysql_real_escape_string($items[3])."',
                                CPspage = ".mysql_real_escape_string($items[4]).",
                                CPepage = ".mysql_real_escape_string($items[5]).",
                                CPfile = '".mysql_real_escape_string($items[6])."',
                                CPlastedited = now(),
                                CPUid = ".$_SESSION['Uid']."";
                        //die($sql);
                        if(!mysql_query($sql)){
                            $not_done[] = $items;
                        }
                        $i++;
                    }
                }
                // if there are any not done records found:
                if(!empty($not_done)){
                    echo "<strong>There are some records could not be inserted</strong><br />";
                    print_r($not_done);
                }
            }
            else{
                die('Invalid file format uploaded. Please upload CSV.');
            }
        }
        else{
            die('Please upload a CSV file.');
        }
    }

My csv file looks like this:

http://www.prima.cse.salford.ac.uk:8080/~ibrarhussain/ConfPaper.csv

Notice i have “Author” columns at the end… So my question is how can i apply the insertAuthor() method to the csv method?

I appreciate that i have posted a lot of code but really hope you guys can help me out or point me in the right direction?

Thanks again