Miguel-php Posted January 8, 2009 Share Posted January 8, 2009 Hello, I have just found this forum and it looks like a goldmine of information! I am hoping someone might have the answer to this; How do you read a CSV file with PHP? For example, if I was selling carpets online, the customer would enter a width and length eg. 39 x 6 This would then call on a CSV file which would look like the below; 10 20 30 40 50 5 1 2 3 4 5 10 6 7 8 9 10 How would I get the PHP script to return the price of 7? Many thanks!! Link to comment https://forums.phpfreaks.com/topic/139975-how-to-read-a-csv-file/ Share on other sites More sharing options...
Mark Baker Posted January 8, 2009 Share Posted January 8, 2009 How do you read a CSV file with PHP? using the fgetcsv() function Link to comment https://forums.phpfreaks.com/topic/139975-how-to-read-a-csv-file/#findComment-732352 Share on other sites More sharing options...
Miguel-php Posted January 8, 2009 Author Share Posted January 8, 2009 Thanks for your reply. I've had a look at the examples but can't figure out how to get the function go to the correct column and row? Can you elaborate further. How would I get the PHP script to return the price of 7? Should read 9 - my mistake!! Many thanks Link to comment https://forums.phpfreaks.com/topic/139975-how-to-read-a-csv-file/#findComment-732357 Share on other sites More sharing options...
Mark Baker Posted January 8, 2009 Share Posted January 8, 2009 To read the file into a nicely structured array: $carpetFile = fopen('data.csv','r'); $carpetData = array(); $carpetDataKeys = array(); $i = 0; while (($carpetDataRecord = fgetcsv($carpetFile,1000,',')) !== False) { if ($i++ == 0) { $carpetDataKeys = $carpetDataRecord; $discard = array_shift($carpetDataKeys); } else { $iKey = array_shift($carpetDataRecord); foreach($carpetDataRecord as $key => $carpetValue) { $carpetData[$iKey][$carpetDataKeys[$key]] = $carpetValue; } } } fclose($carpetFile); echo '<pre>'; print_r($carpetData); echo '</pre>'; To retrieve the appropriate value from the array: $length = 39; $width = 6; foreach ($carpetData as $widthKey => $carpetRow) { if ($widthKey >= $width) { foreach ($carpetRow as $lengthKey => $carpetCost) { if ($lengthKey >= $length) { break 2; } } } } echo 'Array Value is '.$carpetCost.'<br />'; Link to comment https://forums.phpfreaks.com/topic/139975-how-to-read-a-csv-file/#findComment-732361 Share on other sites More sharing options...
Miguel-php Posted January 8, 2009 Author Share Posted January 8, 2009 Just when I think I'm starting understand PHP So I can understand the code and not just copy and paste, is there any chance you can put some explanations next to each line Link to comment https://forums.phpfreaks.com/topic/139975-how-to-read-a-csv-file/#findComment-732366 Share on other sites More sharing options...
Miguel-php Posted January 9, 2009 Author Share Posted January 9, 2009 Anyone??? :-\ Link to comment https://forums.phpfreaks.com/topic/139975-how-to-read-a-csv-file/#findComment-733284 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.