2

I'm trying to find the easiest way to do this. (PHP or Javascript) Basically, I have a text file that looks like this:

1
的
de/dí /dì 
(possessive particle)/of, really and truly, aim/clear
2
一
yī 
one/1/single/a(n)
3
是
shì 
is/are/am/yes/to be
4
不
bù /bú 
(negative prefix)/not/no
5
了
le/liǎo /liào 
(modal particle intensifying preceding clause)/(completed action marker), to know/to understand/to know, clear, look afar from a high place

I want to read the data and insert it into a table such as:

<table cellpadding="0" cellspacing="0" border="1">
    <tr>
      <td width="50" height="50" align="center">1</td>
      <td width="50" height="50" align="center" >的</td>
      <td width="50" height="50" align="center">de/dí /dì</td>
        <td width="50" height="50" align="center">(possessive particle)/of, really and truly, aim/clear</td>

    </tr>
        <tr>
      <td width="50" height="50" align="center">2</td>
      <td width="50" height="50" align="center">一</td>
      <td width="50" height="50" align="center">yī </td>
        <td width="50" height="50" align="center">one/1/single/a(n)</td>
    </tr>
</table>

I suppose there are two ways to do this. The first is organizing the content into variables (1a, 1b, 1c, 1d, 2a, 2b, 2c, 2d...) and then find some way to with minimal effort to insert the variables into a set number of tables.

The second way is to organize the data in an array, then array the data to generate a table. I think this may be the best way, but I need to be able to specify which portion of the table (like page 1 (entries 1-100), etc.

4
  • 1
    You'd be better off putting the text file data in a database, and pulling it from there. Commented Feb 13, 2015 at 18:44
  • Does the data file always contain 4 lines per table row? Commented Feb 13, 2015 at 18:45
  • For now they should all contain the same number of rows. Although I can think of future changes where that may not be the case (if I split context examples onto multiple lines). Commented Feb 13, 2015 at 20:49
  • You should at least turn this into a proper CSV file. Commented Feb 13, 2015 at 21:23

2 Answers 2

2

If each data set in the file always consists of 4 rows, this is how you can generate the table rows:

<?php
$rows=file('path/to/file.txt');
$start=1;
$max=100;
echo '<tr>';
for($length=count($rows); $start<=$length && $start<=$max; $start++){
    echo '<td>',$rows[$start-1],'</td>';
    if($start %4 == 0) echo '</tr><tr>';
}
echo '</tr>';
?>

Then you just have to build the rest of the table around it. Set the formatting (like heights) in a CSS file. To show only part of data, just set the $start and $max variables accordingly. I've kept it short for brevity. Obviously, add checks like if the file isn't actually empty and so on...

Sign up to request clarification or add additional context in comments.

4 Comments

Very enlightening bit of code. I'm playing with the values now and thinking how I can adapt this to a more complex table structure. Maybe if I include bits of code and blank spaces in the text file. I mocked something up in Photoshop to illustrate one possibility: i.imgur.com/fDyPzEl.jpg
That way lies madness. You should really store the data in a more structured way before making it any more complex.
I found one way to do it, but it's extremely convoluted and messy. Maybe I should ask a question about storing the data in a database, but I'm not exactly sure where to start on that.
@Bodhi1: You might try a SQLite database or, if it's not a lot of data, maybe even store it as XML or JSON. Anything standardized is better than what you have, really.
2

Assuming there are always 4 lines per entry, start by reading the file into an array where each value is a single line. Then create a loop that iterates every 4 lines (i = i + 4), and within the body of the loop, access each element relative to the current iteration (line[i], line[i+1], line[i+2], line[i+3]).

Alternatively, you could use a JSON array to store your data in a file, where each value is it's own object containing each entry as it's own key. Both PHP and JavaScript have native support for reading and writing JSON. Then you could just loop through the resulting array and point to the keys you need.

Comments

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.