Page 1 of 1

Help with record in csv file.

Posted: Fri Sep 03, 2004 4:43 pm
by rayc
Hello,

I hope someone can help me here, as I cannot figure out for the life of me on how to do this.

I have a csv file with data in it like so:


Stock#,Year,Make,Model,Engine,Transmission,Color,Mileage,VIN,Asking,Equipment,,,,,,,,,,,,,,,,,,,,,,,,,

401883,2003,Mazda Truck,MPV LX Minivan 4D,V6 3.0 Liter,Automatic,Lt. Blue,19886,JM3LW28J330353753,20795,Front Wheel Drive,Power Sliding Doors,7 Passenger,Air Conditioning,Rear Air,Power Steering,Power Windows,Power Door Locks,Tilt Wheel,Cruise Control,AM/FM Stereo,Single Compact Disc,Dual Front Air Bags,ABS (4-Wheel),Quad Seating,Privacy Glass,Alloy Wheels,,,,,,,,,

401881,2001,Ford Truck,Expedition Sport Utility 4D,V8 5.4 Liter,Automatic,Blue,54742,1FMRU17L71LA17357,22995,2 Wheel Drive,Eddie Bauer,Air Conditioning,Rear Air,Power Steering,Power Windows,Power Door Locks,Tilt Wheel,Cruise Control,AM/FM Stereo,Cassette,Multi Compact Disc,Dual Front Air Bags,ABS (4-Wheel),Leather,Dual Power Seats,Third Seat,Roof Rack,Privacy Glass,Running Boards,Towing Pkg,Two-Tone Paint,Premium Wheels,,,


On each line in position 10 I have the equiptment and so on util the end of the line. What I am trying to do is get all the equiptment for each line in the variable $equiptment. Here is what I have so far:

Code: Select all

// Get file
$lines = file ('./data/inventorynew.csv');

// Loop through file
foreach ($lines as $line_num => $line) {
	$rows = list($stock,$year,$make,$model,$engine,$trans,$color,$miles,$vin,$price,$equipt) = split (",", $line);
	$numrows = count($rows)."<br>";  // Get number of columns in line
		
}
There is where I get stuck how do I make it loop through to add the equiptment to one variable?

I tried this and it does not work right:

Code: Select all

$lines = file ('./data/inventorynew.csv');

foreach ($lines as $line_num => $line) {
	$rows = list($stock,$year,$make,$model,$engine,$trans,$color,$miles,$vin,$price,$equipt) = split (",", $line);
	$numrows = count($rows);
	
	$i = 10;
	while ($i <= $numrows) {
   		$equiptment .= $equipt;
   		$i++;
	}
}
echo $equiptment;
Any help would be greatly appreciated.

Thanks in advance!
Ray

Posted: Fri Sep 03, 2004 6:36 pm
by timvw
[php_man]fgetcsv[/php_man]

Code: Select all

$equipment = '';

$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  $equipment .= $data['equipment'];
}
fclose($handle);
 
echo $equipment;

Posted: Fri Sep 03, 2004 9:15 pm
by McGruff
timvw: Is there a $data['equipment'] key, or is that $data[10]?

I'd definitely go with fgetcsv though. It's generally best to read a file one-line-at-time since this avoids having to copy the whole thing into an array (it's also copied a second time in the foreach loop). These sorts of optimisations don't always matter but I guess it's better to know about them.

Posted: Fri Sep 03, 2004 9:40 pm
by timvw
McGruff wrote:timvw: Is there a $data['equipment'] key, or is that $data[10]?
:oops: I'm a hash addict :)

Posted: Sun Sep 05, 2004 6:11 pm
by litebearer
Sorry for being a couple days late...

Code: Select all

$lines = file ('./data/inventorynew.csv'); 
$total_lines = count($lines);
$i = 0;
for ($i = 0; $i < $total_lines; $i ++) {
  $myarray1 = explode(",",$lines[$i]);
  $total_elements = count($myarray1);
  $start = 10;
  for ($start=10; $start < $total_elements; $start ++) {
    $myarray2[] = $myarray1[$start];
  }
  $equipment[$i] = implode(",", $myarray2);
  echo $equipment[$i];
}
Lite...