Note that there are some explanatory texts on larger screens.

plurals
  1. PORead Pairs of Q&A's from file?
    primarykey
    data
    text
    <p>I'm looking to create a simple text file full of question and single answers. The basic layout idea is :</p> <pre><code>[Q] Question 1 [A] Answer 1 [-] [Q] Question 2 [A] Answer 2 [-] </code></pre> <p>After each answer is a separater [-] to indicate that Q/A combination is complete. The answer could run over multiple lines and/or include some basic HTML. </p> <p>Eg:</p> <pre><code>[Q] What is PHP ? [A] &lt;b&gt;PHP&lt;/b&gt; is a server-side scripting language designed for web development but also used as a general-purpose programming language.&lt;br/&gt;PHP is now installed on more than 244 million websites and 2.1 million web servers.&lt;a href='#'&gt;[2]&lt;/a&gt;&lt;br/&gt;Originally created by Rasmus Lerdorf in 1995, the reference implementation of PHP is now produced by The PHP Group [-] [Q] Question 2 [A] Answer 2 [-] </code></pre> <p>The text file will be written by hand and stored on a local server. So the layout can be changed if needed. But I do want to be able to use some HTML in the formatting.</p> <p>What I want to try an do is read this file into a php page and loop through each Q / A pair and present them on screen.</p> <p>The Question and Answer will be outputted similar to :</p> <pre><code>&lt;a href="#" class="question"&gt;QUESTION&lt;/a&gt; &lt;div class="answer"&gt; &lt;strong&gt;[Q] QUESTION&lt;/strong&gt;&lt;br/&gt; [A]ANSWER &lt;/div&gt; </code></pre> <p>Can someone point me in the right direction for reading the pairs and then outputting them.</p> <p>Thanks :)</p> <p>note: the example answer to Q1 is taken from Wikipedia ;)</p> <p><strong>UPDATE:</strong> This is what I've got so far: Questions:</p> <pre><code>[Q] Question 1 [A] Answer 1 [-] [Q] Question 2 [A] Answer 2 [-] [Q] Question 3 [A] Answer 3 [-] [Q] Question 4 [A] &lt;b&gt;PHP&lt;/b&gt; is a server-side scripting language designed for web development but also used as a general-purpose programming language.&lt;br/&gt;PHP is now installed on more than 244 million websites and 2.1 million web servers.&lt;a href='#'&gt;[2]&lt;/a&gt;&lt;br/&gt;Originally created by Rasmus Lerdorf in 1995, the reference implementation of PHP is now produced by The PHP Group [-] [Q] Question 5 [A] Answer 5 [-] [Q] Question 6 [A] Answer 6 [-] </code></pre> <p>Script:</p> <pre><code>&lt;?php $handle = fopen("questions.txt","r"); if ($handle) { while (($line = fgets($handle)) !== false) { if (substr($line,0,3)==="[Q]") { echo "&lt;a href='#' class='question'&gt;$line&lt;/a&gt;"; $question = $line; } if (substr($line,0,3)==="[A]") { echo "&lt;div class='answer'&gt;"; echo "&lt;strong&gt;$question&lt;/strong&gt;&lt;br/&gt;"; echo "$line&lt;/div&gt;"; } if (substr($line,0,3)==="[-]") { echo "&lt;br/&gt;"; continue; } } } else { echo "Unable to open file"; } ?&gt; </code></pre> <p>This works but it requires each question to be a single line. Ideally I'd like to ensure they are matching Q/A Pairs and the answer can be split over multiple lines.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload