Creating A Question and Answers page.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Creating A Question and Answers page.

Post by chris98 »

Hello, I would like to create a Question and Answers page on my site, where people can ask questions, and other members can respond.Does anyone know how to?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Creating A Question and Answers page.

Post by Christopher »

It sounds the same as a blog post with comments. You could use something like Wordpress for that. Do you want people to be able to vote for the answers like on StackOverflow?
(#10850)
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Creating A Question and Answers page.

Post by chris98 »

It doesn't really matter, as long as anyone at all, (even guests) can leave a question or answer that an Administrator can remove if necessary
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Creating A Question and Answers page.

Post by Christopher »

Do you want to write it from scratch or use something like Wordpress to achieve the same thing?
(#10850)
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Creating A Question and Answers page.

Post by chris98 »

I just want something that is easy to moderate, and fairly easy to change styles.I think that it would be easier to use a template, but as long as you don't have to login specifically to just view it, then i'm fine.Also, preferably if possible I would like a theme similar to the one below.

http://www.cheatmasters.com/qa/19349/St ... _2_qa.html
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Creating A Question and Answers page.

Post by Christopher »

Submitting a question or answer looks like a popup that is a PHP script that adds it to the database. The page seems to load maybe 50 questions plus their answers. Then it uses CSS and Javascript to show/hide the answers.
(#10850)
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Creating A Question and Answers page.

Post by chris98 »

How could I start with creating something like this, or do you know any templates similar, as I'm fairly new to PHP?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Creating A Question and Answers page.

Post by Christopher »

It's mostly done with CSS and Javascript, I would start there. All the PHP does is provide the question and answer records from the database.
(#10850)
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Creating A Question and Answers page.

Post by chris98 »

Do you know any free templates I could use to set me off?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Creating A Question and Answers page.

Post by Christopher »

No, I don't know any templates. You could take a look at something like the jQuery hide()/show() methods. It should be pretty easy to first hide all the answers by making them all the same CSS class. Then show the ones that go with a question on a click event.
(#10850)
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Creating A Question and Answers page.

Post by chris98 »

What PHP and MySQL would I need?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Creating A Question and Answers page.

Post by Christopher »

I would first write the HTML/CSS/Javascript to show a page with one question with answers. Then add PHP to fetch questions and their answers. Put a PHP loop around the one question with answers and fill in the text, etc. with the values from the database. Once you have that working, then you can add pagination -- only fetching say 25 questions.

Use the PDO or mysqli extension for database access.
(#10850)
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Creating A Question and Answers page.

Post by chris98 »

So far, i've come up with this, but could you tell me what is wrong here, as it's not hiding/showing the text?

The text on the button changes, but the actual text down the bottom doesn't.

Code: Select all

<input type="button" id="hide" value="Hide" onclick="return change(this);" />

<script type="text/javascript">
function change( el )
{
    if ( el.value === "Show" )
        el.value = "Hide";
    else
        el.value = "Show";
}
$(document).ready(function(){
  $("#hide").click(function(){
    $("p").hide();
  });
  $("#show").click(function(){
    $("p").show();
  });
});
</script>
<p>test</p>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Creating A Question and Answers page.

Post by Christopher »

Remember to link to JQuery:

Code: Select all

<input type="button" id="mybutton" value="Hide" />

<script type="text/javascript">
function change( el )
{
    if ( el.value === "Show" )
        el.value = "Hide";
    else
        el.value = "Show";
}
$(document).ready(function(){
  $("#mybutton").click(function(){
	change(this);
	$("#mytext").toggle();
  });
});
</script>
<p id="mytext">test</p>
(#10850)
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Creating A Question and Answers page.

Post by chris98 »

How could I make it hidden by default?
Post Reply