LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php undefined offset (https://www.linuxquestions.org/questions/programming-9/php-undefined-offset-849148/)

eoc92866 12-08-2010 05:18 PM

php undefined offset
 
hi... i'm getting an undefined offset for the associative arrays [28-46] in this format.

PHP Code:

$row 1;

$i 0;

        while (
$i<10000)
                {
                
$data fgetcsv($handle1000",");
$sql="INSERT INTO tableName (name1, name2, name3, name4... this goes all the way to name46) VALUES ('".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."' ...this goes all the way to name46)";

$sqlData mysql_query($sql); 



i have read that i can prevent the notices by doing the following, but it's not working for me

PHP Code:

$row 1;

$i 0;

        while (
$i<10000)
                {
                
$data fgetcsv($handle1000",");
$sql="INSERT INTO tableName
                (
                'name1' => "
isset(.$data[0].) ? $data[0] : 'default value'",
                'name2' => "
isset(.$data[1].) ? $data[1] : 'default value'",
                'name3' => "
isset(.$data[2].) ? $data[2] : 'default value'",
                'name4' => "
isset(.$data[3].) ? $data[3] : 'default value'",
#this goes all the way to name46
                )"
;

$sqlData mysql_query($sql); 

can anyone please assist me?

j-ray 12-09-2010 11:31 AM

You could do like that:

$data = fgetcsv($handle, 1000, ",");
foreach ($data as &$d) {
if (empty($d)) {
$d = 'default value';
}
}
Then INSERT...

mattca 12-09-2010 02:42 PM

Quote:

Originally Posted by eoc92866 (Post 4185217)
hi... i'm getting an undefined offset for the associative arrays [28-46] in this format.

PHP Code:

$row 1;

$i 0;

        while (
$i<10000)
                {
                
$data fgetcsv($handle1000",");
$sql="INSERT INTO tableName (name1, name2, name3, name4... this goes all the way to name46) VALUES ('".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."' ...this goes all the way to name46)";

$sqlData mysql_query($sql); 


This code doesn't make a lot of sense.. why are you hardcoding your $data array indexes? You can do all that using a loop. You're also missing a closing curly brace. Is the call to mysql_query() inside the loop, or outside? Also, you're not incrementing $i at all, which means $i will ALWAYS be 0, and the loop will never terminate. Actually, that's probably your problem right there. You run out of data in the file, which means $data is null, and then you try to use it like an array. You can't can't check if $data[0] exists if $data itself doesn't exist.

Judging by what it appears you are trying to do, something like this might work better:

PHP Code:

while ($data fgetcsv($handle1000",")) {
    
$sql "insert into tableName (";

    foreach (
$data as $i => $item) {
        
$sql .= "name$i";

         if (
$i count($data) - 1) {
             
$sql .= ", ";
         }
    }

    
$sql .= ") values (";

    foreach (
$data as $i => $item) {
        
$sql .= "'$item'";

         if (
$i count($data) - 1) {
             
$sql .= ", ";
         }
    }

    
$sql .= ");";
}

$sqlData mysql_query($sql); 

[untested, may not work exactly as is]

eoc92866 12-09-2010 05:15 PM

i know this is a stupid question... but the name.$i... will i require to make an array for this area to place all of the different field names from the csv file into it?

mattca 12-10-2010 10:22 AM

Quote:

Originally Posted by eoc92866 (Post 4186467)
i know this is a stupid question... but the name.$i... will i require to make an array for this area to place all of the different field names from the csv file into it?

No such thing as stupid questions, just stupid answers ;)

That said though, I'm not completely sure that I understand your question. Are you asking if you'll need to create an array for the "name$i" line? If so, no, you do not. You are using the same $data array to create both parts of the query.

Actually, you can probably use implode() instead of a loop in both places....

something like:
PHP Code:

$sql .= "values ('" implode("', '"$data) . "')"

for the values, and a combination of array_keys() and implode() for the field names:
PHP Code:

$sql .= "(name" implode(", name"array_keys($data)) . ")"

[again, untested, may not work exactly as is]

j-ray 12-10-2010 03:19 PM

As far as I understand that you have a table with 46 columns and want to read in a csv file completely?

// foreach ($data as $i => $item) { $i is 0 at the start
// $sql .= "name$i"; so the column names are not matched ??

while ($data = fgetcsv($handle, 1000, ",")) {

//kick out empty
foreach ($data as &$d) {
if (empty($d)) {
$d = 'default value';
}
}
//create sql
$sql = "insert into tableName (";
for ($i = 1; $i <47 ; $i++) {
$sql .= "name$i";

if ($i < 46) {
$sql .= ", ";
}
}
$sql .= " ) ";
$sql .= "values ('" . implode("', '", $data) . "')";

$sqlData = mysql_query($sql);

}


All times are GMT -5. The time now is 09:21 AM.