glend58 Posted May 6, 2007 Share Posted May 6, 2007 Hello, I'm accessing a file of 4,800 hexadecimal colors with each line separated by a carriage return. It looks like this: E8E3E3 EBE0E0 EDDEDE F0DBDB F2D9D9 F5D6D6 F7D4D4 FAD1D1 Etc. I'm using this code to get it: [pre]$fh = fopen("includes/colors_4800.txt", "r"); for($row = 0; $line_array = fgetcsv($fh, 80000, "\r"); $row++) { $nr_elements = count($line_array); //echo 'NUMBER OF ELEMENTS: ' . $nr_elements; echo '<table align="center">'; for($i = 0; $i < $nr_elements; $i++) { if(($i % 10) == 0) { echo '<tr>'; } ?> <td style="width:15px;height:15px;background-color:#<?php echo $line_array[$i]; ?>" title="<?php echo $line_array[$i]; ?>" onclick="enter_color('<?php echo $line_array[$i]; ?>');"> </td> <?php if(($i % 10) == 9) { echo '</tr>'; } } echo '</table>'; }[/pre] This works perfectly on my server. I get one Table with 480 rows and 10 tds in each row. On the hosting server, however, I get 4,800 individual tables with one row and one td. I can't figure out what the problem is or what php ini setting might be giving me the problem or if my code is actually flawed and for some unknown reason it works on my Apache server on my iMac. Any help would be appreciated. Beginner programmer here. Thanks, Glen Quote Link to comment https://forums.phpfreaks.com/topic/50203-fgetcsv-need-help/ Share on other sites More sharing options...
Barand Posted May 6, 2007 Share Posted May 6, 2007 I think your problem is using fgetcsv on a non-csv file. You choose \r as a field terminator but it is also a line terminator on Mac Mac \r Win \r\n Nix \n Quote Link to comment https://forums.phpfreaks.com/topic/50203-fgetcsv-need-help/#findComment-246523 Share on other sites More sharing options...
glend58 Posted May 8, 2007 Author Share Posted May 8, 2007 Thanks for the reply. I'm confused as to what a "non-csv" file is. I assumed a csv file was basically any file that you extracted information from with the information separated by some unique character, the standard delimiter being a comma (,), though I was under the impression the delimiter could be anything, i.e. "|" or "\\" or whatever. What I was trying to say in my code with the "\r" was "after every carriage return (line end, etc) start over." What you're saying is that the "\r" worked on my iMac because macs see this as a carriage return? But on the hosting server it must, obviously it must, see it as something else? Is there some other way to denote a "carriage return" in the delimiter field that is more universally recognized? Thanks, Glen Quote Link to comment https://forums.phpfreaks.com/topic/50203-fgetcsv-need-help/#findComment-247758 Share on other sites More sharing options...
kenrbnsn Posted May 8, 2007 Share Posted May 8, 2007 Just use the file() function to read the file into an array, one line per element. <?php $line_array = file('includes/colors_4800.txt'); echo '<table align="center">'; for($i=0;$i<count($line_array);$i++) { if(($i % 10) == 0) echo '<tr>'; ?> <td style="width:15px;height:15px;background-color:#<?php echo trim($line_array[$i]); ?>" title="<?php echo trim($line_array[$i]); ?>" onclick="enter_color('<?php echo trim($line_array[$i]); ?>');"> </td> <?php if(($i % 10) == 9) echo '</tr>'; } echo '</table>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/50203-fgetcsv-need-help/#findComment-247780 Share on other sites More sharing options...
jitesh Posted May 8, 2007 Share Posted May 8, 2007 You have a txt file not a csv. better to use file(). By the way the code to fetch data from csv <?php $row = 1; $handle = fopen("test.csv", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/50203-fgetcsv-need-help/#findComment-247792 Share on other sites More sharing options...
glend58 Posted May 10, 2007 Author Share Posted May 10, 2007 Thanks! This really clears things up for me. I appreciate it! Glen Quote Link to comment https://forums.phpfreaks.com/topic/50203-fgetcsv-need-help/#findComment-249548 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.