За последние 24 часа нас посетили 18246 программистов и 1596 роботов. Сейчас ищет 941 программист ...

при значение 0 в csv не заносит в таблицу

Тема в разделе "PHP и базы данных", создана пользователем hitr, 20 дек 2010.

  1. hitr

    hitr Активный пользователь

    С нами с:
    19 дек 2010
    Сообщения:
    5
    Симпатии:
    0
    Собственно есть в csv файле значения равные нулю, и записываться они должны в mt_cfvalues, всё кроме нуля записывается нормально, а вот с нулем даже не создаются в чем может быть проблема?

    Код (Text):
    1. function import_csv() {
    2.     global $mainframe;
    3.    
    4.     $database =& JFactory::getDBO();
    5.    
    6.     $files = JRequest::get( 'files' );
    7.     $file_csv = $files['file_csv'];
    8.    
    9.     if( isset($file_csv['tmp_name']) && file_exists($file_csv['tmp_name']) )
    10.     {
    11.         # Find user_id for administrator
    12.         $database->setQuery( "SELECT id FROM #__users WHERE username = 'admin' OR usertype = 'Super Administrator' LIMIT 1" );
    13.         $admin_user_id = $database->loadResult();
    14.  
    15.         # Now, start reading the file
    16.         $row = 0;
    17.         $index_catid = -1;
    18.         $index_linkname = -1;
    19.         $handle = fopen($file_csv['tmp_name'], "r");
    20.    
    21.         // Test if the csv file is using /r as the line ending. If it is, use our custom csv parser.
    22.         $data = fgets($handle, 100000);
    23.         $type = 0;
    24.         // if(strpos($data,"\r") > 0) {
    25.         //  $type = 1;
    26.         // }
    27.         rewind($handle);
    28.    
    29.         while (($data = mtgetcsv($handle,$type,$row)) !== FALSE) {
    30.             $row++;
    31.  
    32.             # Set the field name first
    33.             if ( $row == 1 ) {
    34.                 $fields = array();
    35.                 for ($f=0; $f < count($data); $f++) {
    36.                     if ( $data[$f] == 'cat_id' ) {
    37.                         $index_catid = $f;
    38.                     }
    39.                     $fields[] = $data[$f];
    40.                     if( $data[$f] == 'link_name' ) {
    41.                         $index_linkname = $f;
    42.                     }
    43.                 }
    44.                 // echo "Fields list: <b>" .implode("|",$fields) . "</b><br />";
    45.             } else {
    46.            
    47.                 # Make sure the listing has at least a link_name. Everything else is optional.
    48.                 if ( !empty($data[$index_linkname]) ) {
    49.                     $num = count($data);
    50.                     $sql_cf_ids = array();
    51.                     $sql_cf_insertvalues = array();
    52.                     $sql_insertfields = array('alias','link_published','link_approved','link_created','user_id');
    53.                     $sql_insertvalues = array(JFilterOutput::stringURLSafe($data[$index_linkname]),1,1,date('Y-m-d H:i:s'),$admin_user_id);
    54.                     for ($c=0; $c < $num; $c++) {
    55.                         if ( !empty($data[$c]) && !empty($fields[$c]) && $c != $index_catid ) {
    56.                             switch($fields[$c]) {
    57.                                 case 'alias':
    58.                                     $sql_insertvalues[0] = $database->getEscaped($data[$c]);
    59.                                     break;
    60.                                 case 'link_published':
    61.                                     $sql_insertvalues[1] = $database->getEscaped($data[$c]);
    62.                                     break;
    63.                                 case 'link_approved':
    64.                                     $sql_insertvalues[2] = $database->getEscaped($data[$c]);
    65.                                     break;
    66.                                 case 'link_created':
    67.                                     $sql_insertvalues[3] = $database->getEscaped($data[$c]);
    68.                                     break;
    69.                                 case 'user_id':
    70.                                     $sql_insertvalues[4] = $database->getEscaped($data[$c]);
    71.                                     break;
    72.                                 default:
    73.                                     if(is_numeric($fields[$c])) {
    74.                                         if($fields[$c] > 22) {
    75.                                             $sql_cf_ids[] = $fields[$c];
    76.                                             $sql_cf_insertvalues[] = $database->getEscaped($data[$c]);
    77.                                         }
    78.                                     } else {
    79.                                         $sql_insertfields[] = $fields[$c];
    80.                                         $sql_insertvalues[] = $database->getEscaped($data[$c]);
    81.                                     }
    82.                                     break;
    83.                             }
    84.                             // echo "<br /><b>".$fields[$c].": </b>".$database->getEscaped($data[$c]);
    85.                         }
    86.                     }
    87.                
    88.                     if ( count($sql_insertfields) == count($sql_insertvalues) && count($sql_insertvalues) > 0 ) {
    89.                         # Insert core data
    90.                         $sql = "INSERT INTO #__mt_links (".implode(",",$sql_insertfields).") VALUES ('".implode("','",$sql_insertvalues)."')";
    91.                         $database->setQuery($sql);
    92.                         $database->query();
    93.                         $link_id = $database->insertid();
    94.                         // echo '<br />' . $sql;
    95.                    
    96.                         # Insert Custom Field's data
    97.                         $values = array();
    98.                         if(count($sql_cf_ids)>0 && count($sql_cf_insertvalues)>0) {
    99.                             $sql = "INSERT INTO #__mt_cfvalues (cf_id,link_id,value) VALUES";
    100.                             for($i=0;$i<count($sql_cf_ids);$i++) {
    101.                                 $values[] = "('" . $sql_cf_ids[$i] . "', '" . $link_id . "', '" . $sql_cf_insertvalues[$i] . "')";
    102.                             }
    103.                             $sql .= implode(',',$values);
    104.                             $database->setQuery($sql);
    105.                             $database->query();
    106.                             // echo '<br />' . $sql;
    107.                         }
    108.                    
    109.                         # Assign listing to categories
    110.                         if(stristr($data[$index_catid],',') === false) {
    111.                             $sql = "INSERT INTO #__mt_cl (link_id, cat_id, main) VALUES (".$link_id.", ".( ($index_catid == -1 || empty($data[$index_catid])) ? 0:$data[$index_catid] ).",1)";
    112.                             $database->setQuery($sql);
    113.                             $database->query();
    114.                             // echo '<br />' . $sql;
    115.                         # This record is assigning to more than one category at once.
    116.                         } else {
    117.                             $cat_ids = explode(',',$data[$index_catid]);
    118.                             $j = 0;
    119.                             foreach($cat_ids AS $cat_id) {
    120.                                 if( !empty($cat_id) )
    121.                                 {
    122.                                     $sqlvalue = '('.$link_id.','.$cat_id.',';
    123.                                     if($j==0) {
    124.                                         $sqlvalue .= '1';
    125.                                     } else {
    126.                                         $sqlvalue .= '0';
    127.                                     }
    128.                                     $sqlvalue .= ')';
    129.                                     $sqlvalues[] = $sqlvalue;
    130.                                     ++$j;
    131.                                 }
    132.                             }
    133.                             if( !empty($sqlvalues) ) {
    134.                                 $sql = 'INSERT INTO #__mt_cl (link_id, cat_id, main) VALUES ' . implode(', ',$sqlvalues);
    135.                                 $database->setQuery($sql);
    136.                                 $database->query();
    137.                                 // echo '<br />' . $sql;
    138.                             }
    139.                             unset($sqlvalues);
    140.                         }
    141.                     }
    142.                 }
    143.  
    144.             }
    145.             echo '<hr />';
    146.         }
    147.  
    148.         fclose($handle);
    149.  
    150.         $mainframe->redirect( 'index.php?option=com_mtree', JText::_( 'Import process Complete!' ) );
    151.     } else {
    152.         $mainframe->redirect( 'index.php?option=com_mtimporter&task=check_csv', JText::_( 'No file specified' ) );
    153.     }
    154. }
    155.  
    156. function mtgetcsv($handle,$type=0,$line=0) {
    157.     switch($type) {
    158.         case 1:
    159.             rewind($handle);
    160.             $data = fgets($handle);
    161.             $newlinedData = explode("\r",$data);
    162.             if(($line+1)>count($newlinedData)) {
    163.                 return false;
    164.             } else {
    165.                 $expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
    166.                 $results=preg_split($expr,trim($newlinedData[$line]));
    167.                 return preg_replace("/^\"(.*)\"$/","$1",$results);
    168.             }
    169.             break;
    170.         case 0:
    171.         default:
    172.             return fgetcsv($handle, 100000, ",");
    173.     }
    174.    
    175. }
    Вот думаю что виновата данная часть кода
    Код (Text):
    1.                         # Insert Custom Field's data
    2.                         $values = array();
    3.                         if(count($sql_cf_ids)>0 && count($sql_cf_insertvalues)>0) {
    4.                             $sql = "INSERT INTO #__mt_cfvalues (cf_id,link_id,value) VALUES";
    5.                             for($i=0;$i<count($sql_cf_ids);$i++) {
    6.                                 $values[] = "('" . $sql_cf_ids[$i] . "', '" . $link_id . "', '" . $sql_cf_insertvalues[$i] . "')";
    7.                             }
    8.                             $sql .= implode(',',$values);
    9.                             $database->setQuery($sql);
    10.                             $database->query();
    11.                             // echo '<br />' . $sql;
    12.                         }
     
  2. Kreker

    Kreker Старожил

    С нами с:
    8 апр 2007
    Сообщения:
    5.433
    Симпатии:
    0
    Что-то много когда не относящегося к вопросу.
    На вскидку - из-за 0 == false == null