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. He lists the question-answer list, taking data from a text file (base.txt). However, it is very suitable for me to create an address and telephone directory. There is only one drawback, I would like the data on organizations to be displayed not only by a common list, but also for each org-tion in a separate page, that is, as is customary in modern Internet directories (yell.ru, for example).

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

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

where the question is displayed, would also be a link to the file, where in a separate window the corresponding question-answer from one database would be displayed. It will be very convenient. I hope I clearly explained what I want. I would greatly appreciate such help.

    3 answers 3

    The simplest option if you do not use the 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.

    • Well, or something like that. //well well. - Kenpachi
    • Thanks for the help, now I will disassemble)) - Aviko
    • Thanks again, realized your version, it turned out! Only the first item does not follow the link, why? - Aviko
    • Because the infection empty () considered int = 0 as an empty value. empty ($ _ REQUEST ['node']) We'll have to check for isset () and> = 0 - knes
    • knes, something I do not understand how to contact you? - Aviko

    God .. go to the database.

    but in general

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

     if($_GET['org']) { echo 'то что надо'; } 
    • go on using the database. // Dadada! =) But TK is TZ. Maybe there is an ancient hosting, which not all supported the database. - knes
    • I thought this only happens in fairy tales = (and here it turns out - Kenpachi
    • Alas, this happens not only in fairy tales, alas. = / - knes
    • Thanks friends! I'll try to figure it out. As for the text files and the database, the specifics of this work, I go to the enterprises of the city, collect information and immediately add it to the smartphone and transfer it to the server. Everything is simple and clear. How can I switch to using the database without being a programmer, I can’t even imagine. Yes, I heard there is such a SQLite, but what kind of animal and what they eat, and even less how to use it with this script, alas, I don’t know)) If there are any recommendations, I will be very grateful. - Aviko
    • I want to add about the database. The database is a universal repository, convenient and structured. In each operating system, different standards about files and folders (there may be errors). And about the ancient hosting, use xml files and their structure is more convenient. - Andrey Arshinov

    Why not put all the text in an array? Then it is possible through serialize () to write it to a file, and then when reading a file, make unserialize (). Then you can create for each organization a separate associative array inside the main one, and they will be processed in a loop.