Jump to content

[SOLVED] display a choosen ROW from CSV flatfile and remove the double quotes


nielolee

Recommended Posts

I am using a CSV text file for displaying 75 records on a php page, I am not trying to import them to a database I am displaying the "full list on one page" and trying to create dynamic links to a "descriptive page - that shows all info (from a selected CSV Row) for selected vehicle" by passing a ID varible in the link url..

 

Now this code works, but my $Price column always has "$9,795" double quotes that I cant get rid of, and if I do the comma throws up errors..

 

I have tried both "\t" and "," seperated files but I cant figure out a better method to remove the double quotes from my CSV or TXT files.

 

I know fgetcsv() can solve this problem but I dont know how to rewrite my old code below to use the fgetcsv()

 

the passing variable link is like this:

/vehicle_details.php?vehicleID=901188

 

The CVS file is like this:

Stock No    Title    Odometer    Colour    Transmission    Registration    Year    Price    VIN    Contact No    Contact Email    Options    Notes    Images    Extra Images
9001188    1994 DAIHATSU CHARADE CE STRATAS MAN 5D HATCHBACK 3CYL     50555    White    (Info not available)    TGX123    1994    "$2,999"    JDAG2027777006393    (02) 1234 1234    [email protected]    "Power Windows, Power Steering, Leather Trim, Air Conditioning, Alloy Wheels, Dual Air Bags, ABS, central locking, Single CD Player"        h003775_main.jpg    "h003775_rear.jpg,h003775_left.jpg,h003775_right.jpg,h003775_interior.jpg,h003775_engine.jpg "

 

And vehicle_details.php looks like this:

<?php
// reads a csv file and returns an two-dimensional array of lines/fields
function select_csv($file,$delimiter,$field,$query)
{

  $data_array = file($file);
  for ( $i = 0; $i < count($data_array); $i++ )
  {
   $parts_array[$i] = explode($delimiter,$data_array[$i]);
   if(trim(strtolower($parts_array[$i][$field])) == trim(strtolower($query)))
   {
    $result_array[] = $parts_array[$i];
   }
  }
  return $result_array;
}


// this willl display all records where the value
// of the selected field matches the query
$data = select_csv("datafeed/vehicles.txt",'\t','0',$_GET['vehicleID']); // 0 means first column (like ID or Key)
    for ( $i = 0; $i < count($data); $i++ ) {
    $vehicleID       = $data[$i][0];
    $Title          = $data[$i][1];
    $Odometer       = $data[$i][2];
    $Colour         = $data[$i][3];
    $Transmission   = $data[$i][4];
    $Registration   = $data[$i][5];
    $Year           = $data[$i][6];
    $Price_AUD      = $data[$i][7];
    $VIN            = $data[$i][8];
    $Contact_No     = $data[$i][9];
    $Contact_Email  = $data[$i][10];
    $Options        = $data[$i][11];
    $Notes          = $data[$i][12];
    $Images         = $data[$i][13];
    $Extra_Images   = $data[$i][14];
}
?>

Title: <?php echo $Title;?><br />
Image: <?php echo $Images;?><br />
Xtra Images: <?php echo $Extra_Images;?><br /><br />

Stock: <?php echo $vehicleID;?>     <br />
Odometer: <?php echo $Odometer;?>     <br />
Colour: <?php echo $Colour;?>       <br />
Transmission: <?php echo $Transmission;?> <br />
Registration: <?php echo $Registration;?> <br />
Year: <?php echo $Year;?>         <br />
Price (AUD): <?php echo $Price_AUD;?>    <br />
VIN: <?php echo $VIN;?>          <br />
Contact No: <?php echo $Contact_No;?>   <br />
Contact Email: <?php echo $Contact_Email;?><br /><br />

Options:<br />
<?php echo $Options;?><br /><br />

Notes:<br />
<?php echo $Notes;?><br /><br />

 

Could anyone tell my how I can sort this issue out,

 

Now this code works, but my $Price column always has "$9,795" double quotes that I cant get rid of, and if I do the comma throws up errors..

 

I have tried both "\t" and "," seperated files but I cant figure out a better method to remove the double quotes from my CSV or TXT files.

 

Could anyone tell my how I can sort this issue out,

 

Is the issue just trying to remove the double quotes?

 

str_replace

 

<?php
// reads a csv file and returns an two-dimensional array of lines/fields
function select_csv($file,$delimiter,$field,$query)
{

  $data_array = file($file);
  for ( $i = 0; $i < count($data_array); $i++ )
  {
   $parts_array[$i] = explode($delimiter,$data_array[$i]);
   if(trim(strtolower($parts_array[$i][$field])) == trim(strtolower($query)))
   {
    $result_array[] = $parts_array[$i];
   }
  }
  return $result_array;
}


// this willl display all records where the value
// of the selected field matches the query
$data = select_csv("datafeed/vehicles.txt",'\t','0',$_GET['vehicleID']); // 0 means first column (like ID or Key)
    for ( $i = 0; $i < count($data); $i++ ) {
    $vehicleID       = $data[$i][0];
    $Title          = $data[$i][1];
    $Odometer       = $data[$i][2];
    $Colour         = $data[$i][3];
    $Transmission   = $data[$i][4];
    $Registration   = $data[$i][5];
    $Year           = $data[$i][6];
    $Price_AUD      = $data[$i][7];
    $VIN            = $data[$i][8];
    $Contact_No     = $data[$i][9];
    $Contact_Email  = $data[$i][10];
    $Options        = str_replace('"', "", $data[$i][11]);
    $Notes          = $data[$i][12];
    $Images         = $data[$i][13];
    $Extra_Images   = $data[$i][14];
}
?>

Title: <?php echo $Title;?><br />
Image: <?php echo $Images;?><br />
Xtra Images: <?php echo $Extra_Images;?><br /><br />

Stock: <?php echo $vehicleID;?>     <br />
Odometer: <?php echo $Odometer;?>     <br />
Colour: <?php echo $Colour;?>       <br />
Transmission: <?php echo $Transmission;?> <br />
Registration: <?php echo $Registration;?> <br />
Year: <?php echo $Year;?>         <br />
Price (AUD): <?php echo $Price_AUD;?>    <br />
VIN: <?php echo $VIN;?>          <br />
Contact No: <?php echo $Contact_No;?>   <br />
Contact Email: <?php echo $Contact_Email;?><br /><br />

Options:<br />
<?php echo $Options;?><br /><br />

Notes:<br />
<?php echo $Notes;?><br /><br />

 

If that is not the issue, please state what you want a bit more clearly.

by using str_replace() on double quotes I run into another problem, specifically fields that contain a required comma such as "price" because it looks like this "$20,999" and anything after the comma in the price becomes a new line... and shows up like $20 and then on the next line 999"

 

I also have a field that has car options like this "Power Windows, Air Cond, Elec Mirrors" see it will happen here to all the comma's will cause newline's.

 

If I change the cvs source file and the code Delimited to a "Tab Delimited TXT File" it works fine, but the client will be using both types of file - depends how the Desktop Automotive Software spits out the exported cvs file - I will assume for now its "Comma seperated".

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.