2

I am new to use the csv file. I want to use CSV file so that updating the data of my table will be easier.

The EXPECTED TABLE should be like this: https://www.dropbox.com/s/e6sg9d3fsh8n1v9/table.png?dl=0

I have generated .csv file for above table as below:

Americas Center,XX%,xxxx,xxxxx,Irving,Norcross,San Carlos
Asheville,XX%,xxxx,xxxxx,Louisville,Northfield,San Diego
Austin,XX%,xxxx,xxxxx,Manchester,Overland Park,Telecommuters
Buffalo Grove,XX%,xxxx,xxxxx,Marlborough,Pennsylvania,Tempe
Charlotte,etc,,,Memphis,Plano,Valencia
Columbia,,,,Milpitas,Raleigh,
Coopersville,,,,Morgan Hill,RTS,

I have read .csv file in PHP as below:

<table class="local table-fill">
<?php

$file_handle = fopen("flextronics.csv", "r");

while (!feof($file_handle) ) {

$line_of_text = fgetcsv($file_handle, 1024);

?>
<tr>
<td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[0]; ?></td>
</tr>
<tr>
<td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[1]; ?></td>
</tr>
<tr>
<td class="text-left" style="color:#FFF;background:#9ea7af;"><strong>Eligible:</strong> <?php print $line_of_text[2]; ?></td>
</tr>
<tr>
<td class="text-left" style="color:#FFF;background:#9ea7af;"><strong>Registered:</strong> <?php print $line_of_text[3]; ?></td>
</tr>
<tr>
<td class="text-left"><?php print $line_of_text[4]; ?></td>
</tr>
<tr>
<td class="text-left"><?php print $line_of_text[5]; ?></td>
</tr>
<tr>
<td class="text-left"><?php print $line_of_text[6]; ?></td>
</tr>
<?php   
}
fclose($file_handle);
?>   
</table>

I am getting RESULT like this: https://www.dropbox.com/s/fjqoj1400nqkgnx/result.png?dl=0

RESULT is NOT like the above EXPECTED TABLE. I tried to get table by changing PHP code, but not able to get data at correct place of table. I could not understand what's going wrong either arrangement of .csv file data or reading that file in PHP. There is another problem like in EXPECTED TABLE, rows css are different. So thinking that, I can not use single <tr> and <td> to fetch data. How can I do this?

2 Answers 2

1
<?php

$file_handle = fopen("flextronics.csv", "r");

while (!feof($file_handle)) {

$lines_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
?>

<table class="local table-fill" >
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[0]; ?></td>
<? endforeach; ?>
</tr>
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[1]; ?></td>
<? endforeach; ?>
</tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left" style="color:#FFF;background:#9ea7af;">
        <strong>Eligible:</strong> <?php print $line_of_text[2]; ?></td>
<? endforeach; ?>
</tr>
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left" style="color:#FFF;background:#9ea7af;">
        <strong>Registered:</strong> <?php print $line_of_text[3]; ?></td>
<? endforeach; ?>
</tr>
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left">
           <?php if ( ! empty($line_of_text[4]) ) print $line_of_text[4]; ?>
    </td>
<? endforeach; ?>
</tr>
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left">
        <?php if ( ! empty($line_of_text[5]) ) print $line_of_text[5]; ?>
    </td>
<? endforeach; ?>
</tr>
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left">
       <?php if ( ! empty($line_of_text[6]) ) print $line_of_text[6]; ?>
    </td>
<? endforeach; ?>
</tr>
</table>
Sign up to request clarification or add additional context in comments.

4 Comments

@ RST: The result is getting like this: dropbox.com/s/3srcfiaxha72smx/ans_result.png?dl=0 . There is a extra column coming. can the last three column be like EXPECTED TABLE(not having Eligible & Registered in heading)?
This actually means you have an empty row in your CSV file. Please refrain from using uppercase characters. It indicates you are shouting which is not the best attitude to get people to help you.
To be more precise. The empty last column indicates an empty row in your CSV file. You can add an if ( ! empty($line_of_text[6]) ) (for example) check for empty fields
@ RST: hey, thanks, that idea of using condition worked and yes, there was the empty line in the CSV file. I removed that and used your answer. Also made some changes in code that you suggested on my end like that condition should be for $line_of_text[1]; $line_of_text[2]; $line_of_text[3] to get expected table. And I used uppercase chars in my question and comment because I thought it will help people to find or relate the things easily, nothing else.
1

In your code you make a new line for each column of $line_of_text.

Try with the below mentioned code:

<table class="local table-fill" >
<?php

$file_handle = fopen("flextronics.csv", "r");

while (!feof($file_handle)) {

$line_of_text = fgetcsv($file_handle, 1024);

?>
<tr>
    <td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[0]; ?></td>

    <td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[1]; ?></td>
    <td class="text-left" style="color:#FFF;background:#9ea7af;">
        <strong>Eligible:</strong> <?php print $line_of_text[2]; ?></td>
    <td class="text-left" style="color:#FFF;background:#9ea7af;">
        <strong>Registered:</strong> <?php print $line_of_text[3]; ?></td>
    <td class="text-left"><?php print $line_of_text[4]; ?></td>
    <td class="text-left"><?php print $line_of_text[5]; ?></td>
    <td class="text-left"><?php print $line_of_text[6]; ?></td>
</tr>
<?php
}
fclose($file_handle);
?>
</table>

4 Comments

that would still be not like the expected table thoug, as the target has each country organized by column , not by row
Take a look at stackoverflow.com/questions/16071864/… this is a javascript code to inverse column and row. If you want to do without JS, you must use another csv struct or parse the csv file then draw each row of the table later.
@micka39: I have tried the code that you answered already. and got the result as Damien Pirsy said. Also tried by changing csv struct but no use. will try with another struct or js that you mentioned. but will prefer solution without JS. thanks.
I have down voted the answer because it doesn't supply the requested result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.