dongren5293 2015-02-13 15:59
浏览 100
已采纳

Javascript / PHP,使用文本文件作为数据库,并将内容组织到一个阵列表中

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.

  • 写回答

2条回答 默认 最新

  • douzhan2027 2015-02-13 19:10
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题