Quote from: Soreng at Dec 22, 2007, 10:12 AM
Hi,
The FAQ is a custom solution, which can easily be made into a snippet. I’l do this, and make it available in the repository.
Cheers,
Soren
Hi Soren,
I didn’t mean to get ahead of you, but I had some time and started playing around and I have the guts of a working FAQ snippet. I got the script you used from the DynamicDrive.com and it works really well.
It was your idea so you definitely deserve to build the snippet if you want, but I can do it if you don’t have the time or inclination. Either way, I thought you’d like to see the code I have.
It assumes that the FAQ content is in a standard MODx document (published or unpublished) with the following form:
Q:Question one.
A:Answer one.
Q:Question two.
A:Answer two.
etc.
The snippet is insensitive to the spacing between fields and the Q: and A: are stripped out.
The snippet is called as
[[FaqSnippet? &docID=`n`]]
where n is the document ID of the document containing the content.
Here’s the snippet code. I haven’t had time to put in the tags but it definitely outputs the questions and answers as it should:
$doc = $modx->getDocument((string)$docID, '*', 1); // Search published first.
if (empty($doc)) {
$doc = $modx->getDocument((string)$docID, '*', 0); // Un-published next?
}
$output = ""; // initialize $output
$docString = $doc['content']; //get the document's content
$docArray = explode("Q:",$docString); // split it at each Q:
//$i = count($docArray);
//echo "count: ".$i.'<br>';
foreach($docArray as $value) {
$items = explode("A:",$value); // split at the A:
if ( ($items[0] != "") && ($items[1] != "") ) { // don't use any items with empty strings
// tags before question go here
$output .= $items[0]; // here's the question
// tags after question go here
// tags before answer go here
$output .= $items[1]; // here's the answer
// tags after answer go here
//$output .= '<br>***<br>';
}
}
return $output;
Cheers,
Bob