Current location: Hot Scripts Forums » Programming Languages » PHP » CSV problem


CSV problem

Reply
  #1 (permalink)  
Old 06-21-07, 11:41 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Hi, I have another question.

It's about validating a csv file when uploaded. I tried this code, but doesn't work perfectly, so wanna check out where did I gone wrong or is there a better method.

PHP Code:

foreach($temp as $n){
        
$temp1explode("\r\n",$n);//
        
$temp2explode(",",$temp1[0]);
        
// check there is no NULL values in the first three column for every row.
        
for ($i=0;$i<=$temp2[2];$i++){
            if (
$temp2[$i] == NULL) { // if any of the first three column is NULL
                
echo $message3// error message
                
echo "<meta http-equiv='refresh' content='0;URL=../'>";
            }
        } 
The above is my current validation code. There is still some more to it, those seem to be alright. My main concern is the above codes. What I need is to check every row in the csv file and on each row, the first 3 column to see if there is any NULL value. If there is, then either reject the whole file OR upload only those that are without NULL values. (Not sure which is better).

If possible, I would also like to check exactly which ROW and COLUMN is missing and prompt the user about the particular missing ROW and COLUMN.
TIA.
Reply With Quote
  #2 (permalink)  
Old 06-21-07, 11:47 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
i created a new thread of this post, as it has nothing to do with your old thread
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 06-21-07, 12:04 PM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Sure, thanks for it.
Reply With Quote
  #4 (permalink)  
Old 06-21-07, 01:59 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
If $temp contains the entire contents of the CSV file, and all records were seperated by \r\n,
then the foreach loop would put one record into $n each time. So you wouldn't need to explode
$n on \r\n because it only contains one record.
PHP Code:

<?php
foreach($temp as $n)
{
 
$temp1explode(",",$n);
 
// check there is no NULL values in the first three column for every row.
 
for ($i=0;$i<3;$i++)
 {
  if (
$temp1[$i] == NULL)
  {
   
// if any of the first three column is NULL
   
echo $message3// error message
   
break;
   
// I'm not sure what you are trying to do here //
   //echo "<meta http-equiv='refresh' content='0;URL=../'>";
   /////////////////////////////////////////////////
   
}
  }
 }
?>
__________________
Jerry Broughton
Reply With Quote
  #5 (permalink)  
Old 06-21-07, 08:20 PM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Thanks for the reply.

Yes, $temp contains the entire contents of the CSV file. Here's the code.

PHP Code:

$temp file($path.$MyFile_name); 

Actually, I don't really understand what is \r, can you explain to me?

After echoing message3, I'm doing some other validation. Should it fall under the for loop ? or outside the for loop better?

PHP Code:

if (strlen($temp2[1]) == '11'){ // if mobile no length is 11
            
if ($temp2[3]){                                            //¸¸×é´æÔÚ, if parent department
                
$key array_search($temp2[3],$g_type); // search uploaded parent department name with DB g_type (department name)
                
if(isset($key)){                                    //¸¸×éµÄID²¢ÇÒ´æÔÚ, if exist
                    //È¡µÃÌí¼Ó×éµÄ id ºÅ
                    
if($t_id $g[$g_id[$key]][$temp2[2]]){        //×Ó×é´æÔÚ
                        
$p_id $g_id[$key];
                        
$isTrue true;
                    }
                }
            } else{
                
$key array_search($temp2[2],$g_type);
                if(isset(
$key)){
                    
$p_id = ($g_uid[$key] == '0')?$g_id[$key]:$g_uid[$key];
                    
$t_id $g_id[$key];
                    
$isTrue true;
                }else{
                    
$isTrue false;
                }
            } 
Reply With Quote
  #6 (permalink)  
Old 06-21-07, 11:45 PM
Sxooter Sxooter is offline
Newbie Coder
 
Join Date: Jun 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
why not use fgetcsv() ?
__________________
Post a clear description of your problem, your code in code tags, your errors, and wait. Because nobody here's a mind reader...
Reply With Quote
  #7 (permalink)  
Old 06-22-07, 02:30 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Isn't fgetcsv() also getting all the content in the csv file?

Right now, I need to validate the data, NOT getting the data.
Reply With Quote
  #8 (permalink)  
Old 06-22-07, 07:31 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Actually, I don't really understand what is \r, can you explain to me?
\r is the symbol for a carriage return and the \n is the symbol for new line.
Some word processors use the \r and some use the \n for a new line.
So sometimes we use both \r\n just in case.




Here, check this out. What do you think?

I stored the contents of the file into a multi-dimensional array for later use.
Now you won't have to go back to the file. Everything is in the array $temp1.
I also tagged the end of each record with a 0 or 1 depending on the
value of $isTrue. You don't have to use it but it might come in handy later.

PHP Code:

<?php
$rc 
0;
$handle fopen($path.$MyFile_name"r");
while((
$temp fgetcsv($handle)) !== FALSE)
{
 
$temp1[$rc] = explode(",",$temp);
 
// check there is no NULL values in the first three column for every row.
 
for($i 0;$i 3;$i++)
 {
  if(
$temp1[$rc][$i] == NULL)
  {
   
// if any of the first three column is NULL
   
echo $message3// error message
   
break;
   }
  }
 if(
strlen($temp1[$rc][1]) == '11')  // if mobile # length is 11
 
{
  if(
$temp1[$rc][3])   //  if parent department
  
{
   
$key array_search($temp1[$rc][3],$g_type); // search uploaded parent department name with DB g_type (department name)
   
if(isset($key))
   {
    if(
$t_id $g[$g_id[$key]][$temp1[$rc][2]])
    {
     
$p_id $g_id[$key];
     
$isTrue true;
     
$temp1[$rc][count($temp1[$rc])] = 1;
     }
    }
   }
  else
  {
   
$key array_search($temp1[$rc][2],$g_type);
   if(isset(
$key))
   {
    
$p_id = ($g_uid[$key] == '0')?$g_id[$key]:$g_uid[$key];
    
$t_id $g_id[$key];
    
$isTrue true;
    
$temp1[$rc][count($temp1[$rc])] = 1;
    }
   else
   {
    
$isTrue false;
    
$temp1[$rc][count($temp1[$rc])] = 0;
    }
   }
  }
 
$rc++;
 }
fclose($handle);
?>
Or maybe like this. I'm not sure. You pick.
PHP Code:

<?php
$rc 
0;
$handle fopen($path.$MyFile_name"r");
while ((
$temp fgetcsv($handle)) !== FALSE)
{
 
$temp1[$rc] = explode(",",$temp);
 
// check there is no NULL values in the first three column for every row.
 
for ($i 0;$i 3;$i++)
 {
  if (
$temp1[$rc][$i] == NULL)
  {
   
// if any of the first three column is NULL
   
echo $message3// error message
   
break;
   }
  if(
strlen($temp1[$rc][1]) == '11')  // if mobile # length is 11
  
{
   if(
$temp1[$rc][3])   //  if parent department
   
{
    
$key array_search($temp1[$rc][3],$g_type); // search uploaded parent department name with DB g_type (department name)
    
if(isset($key))
    {
     if(
$t_id $g[$g_id[$key]][$temp1[$rc][2]])
     {
      
$p_id $g_id[$key];
      
$isTrue true;
      
$temp1[$rc][count($temp1[$rc])] = 1;
      }
     }
    }
   else
   {
    
$key array_search($temp1[$rc][2],$g_type);
    if(isset(
$key))
    {
     
$p_id = ($g_uid[$key] == '0')?$g_id[$key]:$g_uid[$key];
     
$t_id $g_id[$key];
     
$isTrue true;
     
$temp1[$rc][count($temp1[$rc])] = 1;
     }
    else
    {
     
$isTrue false;
     
$temp1[$rc][count($temp1[$rc])] = 0;
     }
    }
   }
  }
 
$rc++;
 }
fclose($handle);
?>
__________________
Jerry Broughton

Last edited by job0107; 06-22-07 at 07:56 PM.
Reply With Quote
  #9 (permalink)  
Old 06-24-07, 10:56 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Thank You 0107, just saw your reply as I was out of town this weekend.

Will try out your suggestion tomorrow, getting late here now.
Reply With Quote
  #10 (permalink)  
Old 06-24-07, 01:59 PM
Sxooter Sxooter is offline
Newbie Coder
 
Join Date: Jun 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Nikas View Post
Isn't fgetcsv() also getting all the content in the csv file?

Right now, I need to validate the data, NOT getting the data.
Not exactly. fgetcsv gets a single line at a time from a file and decodes it from csv to an array.

If your csv file should have 7 fields in it, you can do something like:

PHP Code:

$fp fopen("file.csv","r");
while (!
feof($fp)){
    
$row fgetcsv($fp,10000);
    if (
count($row)!=7) {
       die (
"Bad file");
    } else {
        do 
something
    
}

__________________
Post a clear description of your problem, your code in code tags, your errors, and wait. Because nobody here's a mind reader...

Last edited by Nico; 06-24-07 at 02:26 PM. Reason: Please use [php] wrappers.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CSV problem Andy128 PHP 2 01-05-07 05:49 AM
login, roles problem dbrook007 ASP.NET 10 11-10-06 03:42 PM
Form Display Problem neevrap02 Visual Basic 1 09-05-06 05:18 AM
Count problem kasic ASP.NET 1 10-20-04 12:23 AM
Asp and Microsoft Access 2002 problem gop373 ASP 2 10-06-04 09:13 AM


All times are GMT -5. The time now is 06:03 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.