Topic: FAQ Mod?

I really like PunBB but was srprised that there was no FAQ for users.  It would be nice to have an admin-able FAQ with a link in the header.  The admin should be able to add custom Q & A along with standard PunBB questions.

I would do this if I had a clue how...  right now I'm limited cut&paste programming. sad

Thanks

Find what you want...  Where you want it... www.truelocal.com

2 (edited by Cailean 2004-03-17 19:17)

Re: FAQ Mod?

So I've been workin gon this FAQ thing...  I have the DB stuff done... I created a table called 'faq' with the following fields: id, question, answer, disp_position.
The FAQ Admin panel allows add/delete/edit (almost bug-free) so the DB side works.  I'm having trouble, though, with faq.php.
The code for faq.php is below...  i get an error on the '$result' line...  I really have no idea how this is supposed to work... if anyone has the time to take a look, I'd appreciate it!

$pun_root = './';
require $pun_root.'include/common.php';

//$id = intval($_GET['id']);
//if (empty($id) || $id < 0)
//    message($lang_common['Bad request']);


// Load the faq.php language file
require $pun_root.'lang/'.$language.'/'.$language.'_faq.php';





$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_faq['faq'];
require $pun_root.'header.php';

?>
<table class="punspacer" cellspacing="1" cellpadding="4"><tr><td><b><a href="index.php"><?php echo pun_htmlspecialchars($pun_config['o_board_title']) ?></a> / <?php echo $lang_faq['long_faq'] ?></b></td></tr></table>

<table class="punmain" cellspacing="1" cellpadding="4">
    <tr class="punhead">
        <td class="punhead" colspan="2"><b>Question not answered here? Post your question to the <a href="viewtopic.php?pid=112#112">FAQ Topic.</a></b></td>
    </tr>
<?php

// Fetch some info from the faq
$result = $db->query('SELECT faq_name, question, answer, disp_position FROM '.$db->prefix.'faq ORDER BY disp_position') or error('Unable to fetch faq info', __FILE__, __LINE__, $db->error());
if (!$db->num_rows($result))
    message($lang_common['Bad request'], true);

list($faq_name, $question, $answer, $disp_position) = $db->fetch_row($result);


if ($db->num_rows($result))
{
    while ($id = $db->fetch_assoc($result))
    {
    list($faq_name, $question, $answer, $disp_position) = $db->fetch_row($result);
?>    

    <tr>
        <td class="puncon1" style="width: 240px; vertical-align: top" ><p style="margin-bottom: 0"><b><?php echo $lang_faq['question'].$disp_position.'</p><p style="margin-left: 10px; margin-top: 5px; margin-bottom: 5px"></b>'.$question ?></p></td>
        <td class="puncon2" style="vertical-align: top"><p style="margin-bottom: 0"><b><?php echo $lang_faq['answer'].'</b></p><p style="margin-left: 10px; margin-top: 5px; margin-bottom: 5px">'.$answer ?></p></td>
    </tr>

<?php    
    }
}
else    
    echo "\t".'<tr><td class="puncon1" colspan="6">'.$lang_faq['Empty FAQ'].'</td></tr>'."\n";

?>       
</table>

<table class="punspacer" cellspacing="1" cellpadding="4"><tr><td> </td></tr></table>
<?php

require $pun_root.'footer.php';

edit: updated code

Find what you want...  Where you want it... www.truelocal.com

Re: FAQ Mod?

PHP has no idea what $id is. You have to fetch the variable from $_GET and make sure it's an integer. Have a look at the other scripts (e.g. viewforum.php) to see how PunBB does it.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

Re: FAQ Mod?

Thanks Rickard!!  I added that snippet and now I get a 'Bad Request' error which I've determined originates from the if statement immediately following the $result query.  Any ideas?

Find what you want...  Where you want it... www.truelocal.com

Re: FAQ Mod?

updated the code again... now I don't get the error!

Unfortunately, now I get the first row (record) of the db table three times...! and nothing else.  There are three other rows in the db table.

Hmmm.  I really don't get how the loop works, etc. so I'm kinda taking shots in the dark.  As it is now, the loop part is transplanted from viewforum.php. while the tables themselves are from help.php

Find what you want...  Where you want it... www.truelocal.com

6

Re: FAQ Mod?

Yeah, sure, you've got to add the line:
list($faq_name, $question, $answer, $disp_position) = $db->fetch_row($result);
inside the while loop. Otherwise, the variables don't get updated.

Re: FAQ Mod?

Thanks MarcB, that helped...
{updated above code, again}

but if it ain't one thing.... it's something else!!

now I only get the last record (as determined by dips_position) if a change the order in the admin_faq, the displayed record changes appropriately...  I suspect I need to reset the array or something like that?  I'm going to poke around the other files and see what I find. (It's kinda like a scavenger hunt!)

Find what you want...  Where you want it... www.truelocal.com

8 (edited by Cailean 2004-03-17 19:22)

Re: FAQ Mod?

did some more messing around with the code (you're all cringing, aren't you!)
Anyway, the updated code is above and I'm only getting the first record which means that it's not looping, right?  well, that probably has to do with the fact that $id is undefined (as Rickard noted). You'll see that I've commented out Rickard's suggestion because it was causing the Bad Request because there is no value for 'Get' to get.

edit:
actually, I get the last record...

Find what you want...  Where you want it... www.truelocal.com

9

Re: FAQ Mod?

Try this:

// Fetch some info from the faq
$result = $db->query('SELECT id,faq_name, question, answer, disp_position FROM '.$db->prefix.'faq ORDER BY disp_position') or error('Unable to fetch faq info', __FILE__, __LINE__, $db->error());
if (!$db->num_rows($result))
    message($lang_common['Bad request'], true);

if ($db->num_rows($result))
{
    while (list($id, $faq_name, $question, $answer, $disp_position) = $db->fetch_row($result);
)
    {
    list($faq_name, $question, $answer, $disp_position) = $db->fetch_row($result);
?>    

    <tr>
        <td class="puncon1" style="width: 240px; vertical-align: top" ><p style="margin-bottom: 0"><b><?php echo $lang_faq['question'].$disp_position.'</p><p style="margin-left: 10px; margin-top: 5px; margin-bottom: 5px"></b>'.$question ?></p></td>
        <td class="puncon2" style="vertical-align: top"><p style="margin-bottom: 0"><b><?php echo $lang_faq['answer'].'</b></p><p style="margin-left: 10px; margin-top: 5px; margin-bottom: 5px">'.$answer ?></p></td>
    </tr>

Please note two things:
1) I assumed there's an id field on the table. Might not be so.
2) Since you're not using the $id variable you could take it off, but if you want to make links to these faqs or something, you might need it. Can't say.

I just erased the $id=... from the while and substituted it from the real one. You should as well erase the first one, or you'll always miss the first record. I think the recordcount should work exactly the same, but if it doesn't (if you get an empty recordset) you'll have to change the while bucle to a do while or erase the recordcount if and use a trafficlight variable inside the bucle, so as to know if the recordset was really empty or not (that might be the most speed effective solution, although not the most elegant) that way, I'd do something like:
$light=true;
while {... $light=false;}
if ($light) then echo('empty');

Hope that solves your problem.

Re: FAQ Mod?

Well, progress, I think...

As you can see in the code below...
I changed the while statement as you suggested (correcting for the round bracket ending up on the next line...)
I also commented out the first list statement since it was not in your code...

// Fetch some info from the faq
$result = $db->query('SELECT faq_name, question, answer, disp_position FROM '.$db->prefix.'faq ORDER BY disp_position') or error('Unable to fetch faq info', __FILE__, __LINE__, $db->error());
if (!$db->num_rows($result))
    message($lang_common['Bad request'], true);

//list($faq_name, $question, $answer, $disp_position) = $db->fetch_row($result);


if ($db->num_rows($result))
{
    while (list($id, $faq_name, $question, $answer, $disp_position) = $db->fetch_row($result))
    {
    list($faq_name, $question, $answer, $disp_position) = $db->fetch_row($result);
?>    

    <tr>
        <td class="puncon1" style="width: 25%; vertical-align: top" ><p style="margin-bottom: 0"><b><?php echo $lang_faq['question'].$disp_position.'</p><p style="margin-left: 10px; margin-top: 5px; margin-bottom: 5px"></b>'.$question ?></p></td>
        <td class="puncon2" style="vertical-align: top"><p style="margin-bottom: 0"><b><?php echo $lang_faq['answer'].'</b></p><p style="margin-left: 10px; margin-top: 5px; margin-bottom: 5px">'.$answer ?></p></td>
    </tr>

<?php    
        }
}
else    
    echo "\t".'<tr><td class="puncon1" colspan="6">'.$lang_faq['Empty FAQ'].'</td></tr>'."\n";

?>

I now get record #2 only and a blank record. (there should be 3 records.)
I didn't really understand what you said about this in your last post, could you clarify it for me?

I really appreciate your help on this!!!

Find what you want...  Where you want it... www.truelocal.com

11

Re: FAQ Mod?

Ok, first thing, you didn't modify the query as I suggested, which as I also stated might not be well done unless you have an 'id' field in the table. In this case, simply take the $id out of the while condition, cause the fields won't match.
Second, that's my error (sorry) you need to comment out the list command immediately after the while condition.

That should solve your problems.

Re: FAQ Mod?

SUCCESS!!!!!!

sorry 'bout the confusion but it's working now as expected!!

Thanks a ton for your help, MarcB!!!
Stay tuned for a MOD release....

Find what you want...  Where you want it... www.truelocal.com

13

Re: FAQ Mod?

No way! It's 4:47 here, I should go to bed... if I can still find it wink

Have pun smile

14 (edited by Cailean 2004-03-18 16:54)

Re: FAQ Mod?

FAQ Mod v1.0 released!!!

Find what you want...  Where you want it... www.truelocal.com