Welcome Guest, Not a member yet? Register   Sign In
build query for active record insert
#1

[eluser]ocergyNohtna[/eluser]
ok, i'm still new to CI and a bit new to php. but i've made one post so far on this forum, and while it hasn't gotten me a solution yet, those that helped had good advice. i'm trying to figure out how to format an array for an active record insert. according to the docs, this is the general idea:
Code:
$data = array(
               'title' => 'My title' ,
               'name' => 'My Name' ,
               'date' => 'My date'
            );

$this->db->insert('mytable', $data);

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
so, i'm reading an Excel file and creating an array of data that i'll be using in my sql insert. here's the function that reads the excel file and creates the array, only reason for showing this is so you can see how the array is built that is being passed to the next function (the one that will create my array for the active record insert); the return of this function ($items) is passed to the next function as $excelData
Code:
function read_excel_file($fileName) {
    set_time_limit(120);
    $pathToExcelFile = './listings/'.$fileName.'.xls';
    if(!file_exists($pathToExcelFile)) {
        return 'Excel file does not exist.<br />';
    } else {
        $data = new Spreadsheet_Excel_Reader();
        $data->setOutputEncoding('CP1251');
        $data->read($pathToExcelFile);
        error_reporting(E_ALL ^ E_NOTICE);
        $k = 0;
        //$i = 2 because i don't want the top row of data since it's "headers" for the columns
        for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
            for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
                $item = $data->sheets[0]['cells'][$i][$j];
                $item = str_replace('$', '', $item);
                $item = str_replace('#', '', $item);
                $items[$k][$j] .= $item;
            }
            if ($i == $data->sheets[0]['numRows']) {
                break;
            } else {
                $k++;
            }
        }
    }
    return $items;
}
then here's where i'm trying to build my array to pass through the active record insert:
Code:
function insert_data($excelData, $listingDate, $active) {
    for ($i = 0; $i <= count($excelData)-1; $i++) {
        $sql[$i] = array(
                        'uniqueId' => 'NULL',
                        'lotNum' => $excelData[$i][1],
                        'title' => $excelData[$i][2],
                        'description' => $excelData[$i][3],
                        'serialNum' => $excelData[$i][4],
                        'askPrice' => $excelData[$i][5],
                        'increment' => $excelData[$i][6],
                        'reserve' => $excelData[$i][7],
                        'image1' => $excelData[$i][8],
                        'image2' => $excelData[$i][9],
                        'image3' => $excelData[$i][10],
                        'image4' => $excelData[$i][11],
                        'image5' => $excelData[$i][12],
                        'image6' => $excelData[$i][13],
                        'ebayCatId' => $excelData[$i][14],
                        'lowEstimate' => $excelData[$i][15],
                        'highEstimate' => $excelData[$i][16],
                        'listingDate' => $listingDate,
                        'active' => $active
                    );
    }
    $result = $this->db->insert('items', $sql);
    if ($result != FALSE) {
        return TRUE;
    } else {
        return FALSE;
    }
}

does this logic make any sense to anyone?? i just can't seem to to figure out how to correctly build the array. any ideas? suggestions? slaps in the face? (feels like one of those that's right under my nose....yet ever so elusive)


Messages In This Thread
build query for active record insert - by El Forum - 06-26-2007, 07:13 PM
build query for active record insert - by El Forum - 06-27-2007, 05:18 AM
build query for active record insert - by El Forum - 06-27-2007, 12:20 PM
build query for active record insert - by El Forum - 06-27-2007, 12:43 PM
build query for active record insert - by El Forum - 06-27-2007, 03:02 PM
build query for active record insert - by El Forum - 06-28-2007, 12:18 AM
build query for active record insert - by El Forum - 06-28-2007, 01:12 AM
build query for active record insert - by El Forum - 06-28-2007, 10:30 PM
build query for active record insert - by El Forum - 06-29-2007, 08:47 AM
build query for active record insert - by El Forum - 06-29-2007, 09:58 AM



Theme © iAndrew 2016 - Forum software by © MyBB