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 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错