How to improve php script?

Hello!

There is the following script:

   <?php
    $handle = @fopen('base.txt', 'r') or die('   '); //  
    /*   */
    while(!feof($handle)){
        $read[] = fgets($handle);
    }
    /* 
    echo '<pre>';
    print_r($read);
    echo '</pre>';
    */
    /*   */
    $i = 0;
    foreach($read as $item){
        if(!empty($item)){
            $data[] = explode('//', $item);
            echo '<div class="question">' .$data[$i][0]. '</div>';
            echo '<div class="answer">' .$data[$i][1]. '</div>';
            $i++;
        }           
    }
    fclose($handle); //  
    ?>

The script is designed to organize the FAQ section on the site. It outputs a question-answer list, taking data from a text file (base.txt). However, it is very suitable for me to create an address-telephone directory. There is only one drawback, I would like the data on organizations to be displayed not only as a general list, but also for each organization on a separate page, that is, as is customary in modern online directories (yell.ru, for example).

Therefore, help to make sure that in this script, for example, the line

echo '<div class="question">'.$data[$i][0].'</div>';

The

where the question is displayed would also be a link to a file where the corresponding question and answer from the same database would be displayed in a separate window. It will be very convenient. I hope I made it clear what I want. I would be very grateful for such help.


Answer 1, authority 100%

The simplest option if you don’t use a database engine:

//     ?
//if(empty($_REQUEST['node']) || !isset($read[$_REQUEST['node']])){
  if(!isset($_REQUEST['node']) || !isset($read[$_REQUEST['node']])){   
//:   .
$i = 0;
    foreach($read as $item){
        if(!empty($item)){
            $data[] = explode('//', $item);
            echo '<div class="question"><a href="spisok.php?node='.$i.'">' .$data[$i][0]. '</a></div>';
            echo '<div class="answer"><a href="spisok.php?node='.$i.'">' .$data[$i][1]. '</a></div>';
            $i++;
        }           
    }
}else{
//,   
    echo '<a href="spisok.php">,  </a>';  
    $data = explode('//', $read[$_REQUEST['node']]);
    echo '<div class="question">' .$data[0]. '</div>';
    echo '<div class="answer">' .$data[1]. '</div>';
}

Well, or something like that.


Answer 2, authority 50%

God.. switch to using a database.

in general

echo '<a href="some_url.php?org=name_of_org">' .$data[$i][0]. '</a>';

if($_GET['org'])
{
echo '  ';
}

Answer 3

Why not put all the text in an array? Then you can serialize() write it to a file, and then do unserialize() when reading the file. Then you can create a separate associative array for each organization inside the main one, and process them in a loop.