Page 1 of 1

script for FAQ page

Posted: Mon Dec 20, 2010 5:10 am
by quickLearner
Hi everyone,

i am trying to write a script for an faq page, presently all the questions are stored in a database. i want the page to function as the one in this url http://www.magentocommerce.com/product/faq.

here is what i have been able to do. presently it fetches the question and answer from the database but now i will like for it to work this way: like if i click on a link it automatically jumps to the area on the same page. so how do i associate each links with content from the database???

function main()
{
$rs= mysql_query("SELECT * FROM faq WHERE status='1' ORDER BY faqorder");
echo "<H1>Frequently Asked Questions and my answers</H1>";
echo "If you have a question that has not been addressed below, please ask it in the text box at the bottom of this page
and I will answer it online and if appropriate put it on the website anonymously for others to benefit from as soon as possible.<br/>";
?>
<div id="faq_list">
<ul>
<li> <a href="#<?php echo $value['id']?>"> Is treatment Painful? </a></li>
<li> <a href=""> Will treatment take a long time? </a></li>
<li> <a href=""> How much will it cost? </a></li>
</ul>
</div>

<div class="table faq"> </div>
<?php

//var_dump(mysql_fetch_array($rs));exit;
while($value=mysql_fetch_array($rs))
{
//var_dump($value);
$question=stripslashes($value['question']);
$answer=stripslashes($value['answer']);
$ques=str_replace("?","",$question);
$ques=str_replace("!","",$question);
$ques=str_replace(" ","_",$question);
?>

<p id="faq">
<strong>Q. <?php echo $question;?></strong><br/>
<strong>A.</strong> <?php echo $answer;?>
</p>
<br/>
<?php

thanks.

Re: script for FAQ page

Posted: Mon Dec 20, 2010 6:20 am
by social_experiment
quickLearner wrote:so how do i associate each links with content from the database???
You already have part of the question half-answered. To go to a specific area on a page you have to use anchors (html).

Code: Select all

<a href="#top">Top</a>
// and the anchor is
<a name="top"></a>
I always use the primary key of the table for this purpose.

Code: Select all

<?php
 // assume the PK is 'id'
 $select = mysql_query("SELECT * FROM table");
 
 while ($array = mysql_fetch_array($select)) {
  // links would have the following format
  echo '<a href="page.php?#'. $array['id'] .'">Link</a>';
 }
?>
If the link is clicked a person goes to page.php#5 (assuming your id for the link is 5). On your FAQ page your topic will have the anchor called <a name="5"></a> near the related question.