View Full Version : PHP Navigation ?id=x


disconnect277
04-09-2007, 12:19 PM
For a site I'm making, I'm making a FAQ, but I want the link to be

ex. /faq.php?id=q1

Then it would display a question and answer.

Unless I missed it, but how do you do it?

I know it has something to do with databases, but what?

Ges
04-10-2007, 12:50 PM
Hi disconnect277,
A lot of FAQ's are based on databases such as MySql. In MySql it is very easy to set up using the Administrator, however, if you are not familiar with the interface between PHP and MySql then you can use a 'flat' database which simply uses PHP and HTML code.
Here are 2 such basic files that you can easily adapt to your application ( both using PHP and HTML ). The first one is the Table of Contents ( FAQ.PHP ), whereby clicking on a question the 'id' is passed to the Q and A's ( QANDA.PHP );

FAQ.PHP;

<?php
echo "

<html>
<head>
<title>Frequently Asked Questions</title>
</head>
<body>

<h2>FREQUENTLY ASKED QUESTIONS</h2>
<h2>Please select a question!</h2>
<ol>
<li><a href='qanda?id=q1'>Question #1</a></li>
<li><a href='qanda?id=q2'>Question #2</a></li>
<li><a href='qanda?id=q3'>Question #3</a></li>
<li><a href='qanda?id=q4'>Question #4</a></li>
<li><a href='qanda?id=q5'>Question #5</a></li>
<li><a href='qanda?id=q6'>Question #6</a></li>
<li><a href='qanda?id=q7'>Question #7</a></li>
<li><a href='qanda?id=q8'>Question #8</a></li>
<li><a href='qanda?id=q9'>Question #9</a></li>
<li><a href='qanda?id=q10'>Question #10</a></li>
</ol>


</body>
</html>

";
?>


and QANDA.PHP;

<?php
$new_id = $_GET['id'];

switch ( $new_id ) {
case "q1":
echo "<b><a id='q1'>Question #1</a></b><br><br>Answer #1<br>";
break;
case "q2":
echo "<b><a id='q2'>Question #2</a></b><br><br>Answer #2<br>";
break;
case "q3":
echo "<b><a id='q3'>Question #3</a></b><br><br>Answer #3<br>";
break;
case "q4":
echo "<b><a id='q4'>Question #4</a></b><br><br>Answer #4<br>";
break;
case "q5":
echo "<b><a id='q5'>Question #5</a></b><br><br>Answer #5<br>";
break;
case "q6":
echo "<b><a id='q6'>Question #6</a></b><br><br>Answer #6<br>";
break;
case "q7":
echo "<b><a id='q7'>Question #7</a></b><br><br>Answer #7<br>";
break;
case "q8":
echo "<b><a id='q8'>Question #8</a></b><br><br>Answer #8<br>";
break;
case "q9":
echo "<b><a id='q9'>Question #9</a></b><br><br>Answer #9<br>";
break;
case "q10":
echo "<b><a id='q10'>Question #10</a></b><br><br>Answer #10<br>";
break;
default:
print "No such question";


}

echo " <br><br><a href='faq.php'>Back to contents....</a> ";

?>



Just copy them exactly as they are and give them the correct names. I have simply used 10 q and a's.
QANDA.PHP uses a rather cumbersome 'switch' statement of which it would be better to call an appropriate function to do the handling ( much more structured and managable ).

Well I hope this helps to get you started.
Regards,
Ges.

iGeek
04-12-2007, 04:22 AM
Check http://www.hotscripts.com/ to see if they have any FAQ scripts that will work.

I would then learn PHP and try it yourself. :)

Ges
04-12-2007, 11:31 AM
Yes there are some good ones at;

http://www.hotscripts.com/search/14921079.html

and some do employ MySql.

The one I posted is a very simple 'get up and running' script - dependant, as iGeek points out - PHP experience.

Regards,
Ges

disconnect277
04-12-2007, 10:45 PM
Thank you SO much Ges.

Thank you, If I have any questions, I'll be sure to ask you.

Idiotic Creation
04-14-2007, 02:03 PM
Just as a side note you can also use the require() function in Ges's switch statment if you wanted to have you pages load diferent content based on the id.

iGeek
04-15-2007, 11:14 PM
Here is a better example of PHP navigation:

http://xentrik.net/php/querystring.php

Ges
04-16-2007, 05:42 AM
Hi all,
Referring to my comment;
cumbersome 'switch' statement of which it would be better to call an appropriate function to do the handling ( much more structured and managable ).
Let's not forget the 'tone' of the original post from disconnect277 implying a simple solution.

However, Idiotic Creation has made a good point. The require() is a good way and ties in closely with include() - the main difference being that require() stops processing if it encouters an error whereas include() does not, ( but flags a warning ).
Which brings us niceley to iGeeks example which employs the include() technique very nicely;
Quote from that site;
If you have heaps and heaps of links and include files, the code needed to do all this can get quite bulky and messy, not to mention difficult to maintain. In this case, there is a more efficient code you can use, which utilizes switch and case

I'm always on the lookout ( are'nt we all ), for good, sound and practical advice and sites for others to use so if you have any more gems then please let me know.

Regards,
Ges.