Help with record in csv file.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rayc
Forum Newbie
Posts: 1
Joined: Fri Sep 03, 2004 4:41 pm

Help with record in csv file.

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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;
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

McGruff wrote:timvw: Is there a $data['equipment'] key, or is that $data[10]?
:oops: I'm a hash addict :)
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post 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...
Post Reply