Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/50203-fgetcsv-need-help/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/50203-fgetcsv-need-help/#findComment-247758
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/50203-fgetcsv-need-help/#findComment-247780
Share on other sites

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);
?> 

Link to comment
https://forums.phpfreaks.com/topic/50203-fgetcsv-need-help/#findComment-247792
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.