duangou1551 2015-08-20 19:01
浏览 10
已采纳

使用php列出文本文件中的数据

I am using a text file as a database. This code only shows the row by the id I select. I want it to show everything from the text file.

<?php
$lines = file('database.txt');
$data = array();
$id = 1;

for($i = 0; $i < count($lines); $i++){
if($lines[$i] == $id){
    $data[0] = $lines[$i];
    $data[1] = $lines[$i+1];
    $data[2] = $lines[$i+2];
    $data[3] = $lines[$i+3];
}
}
?>



<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
<title>Test</title>
</head>
<body>



<div class="box1">
   <h1><?= $data[1]; ?></h1>   
   <img src="<?= $data[2]; ?>">
    <p>
    <?= $data[3]; ?>
   </p> 
   <br />
   <a href="<?= $data[4]; ?>">Click  here!</a>
</div>

<div class="box1">
   <h1><?= $data[1]; ?></h1>   
   <img src="<?= $data[2]; ?>">
    <p>
    <?= $data[3]; ?>
   </p> 
   <br />
   <a href="<?= $data[4]; ?>">Click  here!</a>
</div>

</body>
</html>

and here is my database file

    1
    Name
    Image
    My text
    Link
    -------
    2
    Name1
    Image1
    Text1
    Link1
    -------
    3
    Name3
    Image3
    Text3
    Link3
    -------
  • 写回答

3条回答 默认 最新

  • dongren1986 2015-08-20 19:39
    关注

    First to answer your question
    I don't know if you want to display everything at once, or just a table of contents type of deal, so here goes the former and I will edit my post if you need the latter

    EDITED: added trim in the if clause to build the array properly and in the print function to eliminate new-lines.

    a. show everything

    <?php
    $lines = file('database.txt');
    
    $i=0;
    foreach ($lines as $line){
        if (trim($line)!="-------") $data[$i][]=$line;
        else $i++;
    }
    
    <!DOCTYPE HTML>
    <html lang="en-US">
    <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="style.css" media="all" />
    <title>Test</title>
    </head>
    <body>
    
    <?php
    foreach ($data as $key=>$val) {
        print '<div class="box'.$key.'">
        <h1>'.trim($val[1]).'</h1>
        <img src="'.trim($val[2]).'">
        <p>'.trim($val[3]).'</p> 
        <br />
        <a href="'.trim($val[4]).'">Click  here!</a>
        </div>';
    }
    
    ?>
    
    </body>
    </html>
    

    Next I suggest you store your information in CSV format, like this:

    Name,Image,Text,Link
    Bob,/home/images/bob.jpg,"some text",http://bob.example.com
    Alice,/home/images/alice.png,"alice is wonderfull",http://alice.org
    

    Then you can easily parse the csv file using fgetcsv http://php.net/manual/en/function.fgetcsv.php

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

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?