Use CSS for that. You can give the text <p> a class (which would make sense for your answers) and set it to hidden in the style sheet.chris98 wrote:How could I make it hidden by default?
Creating A Question and Answers page.
Moderator: General Moderators
- 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.
(#10850)
- chris98
- Forum Contributor
- Posts: 103
- Joined: Tue Jun 11, 2013 10:47 am
- Location: England, United Kingdom
Re: Creating A Question and Answers page.
How could I use all this with question id's?
E.G. id=1, id=2.etc
And also, now I've added the Class Hidden attribute, it won't let me make them visible.How could I do that?
E.G. id=1, id=2.etc
And also, now I've added the Class Hidden attribute, it won't let me make them visible.How could I do that?
- chris98
- Forum Contributor
- Posts: 103
- Joined: Tue Jun 11, 2013 10:47 am
- Location: England, United Kingdom
Re: Creating A Question and Answers page.
I've added the Class Hidden attribute, but now it won't let them become visible again, so what would fix that?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
- chris98
- Forum Contributor
- Posts: 103
- Joined: Tue Jun 11, 2013 10:47 am
- Location: England, United Kingdom
Re: Creating A Question and Answers page.
Code: Select all
<div id="text">
<?php echo nl2br($row['text']); ?>
<br><br>
<input type="button" id="q1" value="Show" class="ask">
<script type="text/javascript">
$(".ans").hide();
</p><p id="a1">A1: test</p><br>
<p id="a2">A2: test</p><br>
<p id="a3">A3: test</p><br>
<p id="a4">A4: test</p><br>
<p id="a5">A5: test</p><br><br>
<a href="answer.php" id="ans" class="ask">Answer this Question</a>
</script>
<br><br>
</div>
</div>- 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.
Please format your code and put it in PHP Code tags.
Your </script> tag is in the wrong place. And you answers all need to be class="ans".
Your </script> tag is in the wrong place. And you answers all need to be class="ans".
(#10850)
- chris98
- Forum Contributor
- Posts: 103
- Joined: Tue Jun 11, 2013 10:47 am
- Location: England, United Kingdom
Re: Creating A Question and Answers page.
When I change the class attribute, should I rename all the A1, A2.etc, or just keep one?
And also where should the script tag go?
And also where should the script tag go?
- 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.
You use the class to hide them all and the ID to show individual answers. However, I think you only need a <div> around all the answers to a question -- not each answer. Like this:
<div id="question_1">
So "question_1", "button_1" and "answers_1" are all related, when you get "button_1" clicked, remove the "button_" part to get the "1" and then append it to "answer_" to get the ID of the element to hide.
<div id="question_1">
Code: Select all
<?php echo nl2br($row['text']); ?>
<br><br>
<input type="button" id="button_1" value="Show" class="buttons">
</div>
<div id="answers_1" class="answers">
<p id="a1">A1: test</p><br>
<p id="a2">A2: test</p><br>
<p id="a3">A3: test</p><br>
<p id="a4">A4: test</p><br>
<p id="a5">A5: test</p><br><br>
<a href="answer.php" id="ans" class="ask">Answer this Question</a>
<br><br>
</div>
<script type="text/javascript">
$(document).ready(function(){
$(".answers").hide();
$(".buttons").click(function(event){
alert("button="+event.target.id);
});
});
</script>
(#10850)
- chris98
- Forum Contributor
- Posts: 103
- Joined: Tue Jun 11, 2013 10:47 am
- Location: England, United Kingdom
Re: Creating A Question and Answers page.
This doesn't do anything.It only makes a message box appear saying "button=button_1" 3 times.
- 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.
I assumed that given the ID you could do the actual coding yourself.
(#10850)
- chris98
- Forum Contributor
- Posts: 103
- Joined: Tue Jun 11, 2013 10:47 am
- Location: England, United Kingdom
Re: Creating A Question and Answers page.
I possibly could, but I would have to be told quite a lot about how to add it in, and make it work, because I would quite likely get confused.(As I've said before, I'm new to PHP).What would the first thing to do be?
- chris98
- Forum Contributor
- Posts: 103
- Joined: Tue Jun 11, 2013 10:47 am
- Location: England, United Kingdom
Re: Creating A Question and Answers page.
How could I fix this code.Every time I never get the result I want, because it always comes up with two things.The first:
Notice: Undefined variable: query in C:\wamp\www\qanda\index.php on line 95
And the second:
Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\wamp\www\qanda\index.php on line 95
Here is the Code I'm using, so how could I fix this so it just outputs the "No answers yet!", rather than the mysql error?
<?php
require_once('general.php');
require_once('db/db_connection.php');
if (mysql_num_rows($query) < 0)
{
$sql = "SELECT * FROM answers where id=1";
$result = $MyDb->f_ExecuteSql($sql);
$recordcount = $MyDb->f_GetSelectedRows();
}
else
echo "No answers yet!"
?>
Notice: Undefined variable: query in C:\wamp\www\qanda\index.php on line 95
And the second:
Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\wamp\www\qanda\index.php on line 95
Here is the Code I'm using, so how could I fix this so it just outputs the "No answers yet!", rather than the mysql error?
<?php
require_once('general.php');
require_once('db/db_connection.php');
if (mysql_num_rows($query) < 0)
{
$sql = "SELECT * FROM answers where id=1";
$result = $MyDb->f_ExecuteSql($sql);
$recordcount = $MyDb->f_GetSelectedRows();
}
else
echo "No answers yet!"
?>
Last edited by chris98 on Thu Aug 29, 2013 8:17 am, edited 1 time in total.
Re: Creating A Question and Answers page.
One of the most useful things you can learn about PHP or any computer programming language that provides you with error messages, is how to read and understand the error messages. Error messages are your friends! If you understand them, you often won't need to ask others to help you, they will tell you exactly what's wrong.
"Undefined variable: query ... on line 95" tells you that your code is trying to use a variable ($query) that you have not previously defined or given a value to. Obviously that won't work. It tells you what file and the line number where this problem was found. Now it's up to you to figure out what's missing.
"mysql_num_rows() expects parameter 1 to be resource, null given ... on line 95" tells you that instead of a data resource as the first parameter, the function is being given a null value (nothing). This is virtually always the result of the query failing to produce any data, thus the variable you probably used to hold the data resource has a null value.
Take a look at your code. After including the db_connection.php file (which I'm sure contains the code to connect to your database), you immediately begin a conditional (if) statement that depends on a function that counts how many rows of data were returned by a query--but you haven't executed a query yet! Thus, PHP tells you that the variable $query has not been defined, and MySQL tells you that the row counting function has not been given a valid data resource to count. Both PHP and MySQL are trying their best to show you what's wrong.
If I may make an observation, while there is nothing wrong with being a beginner (all of us had to start from the beginning, obviously), you shouldn't have unrealistic expectations that you can learn programming by consulting a forum every time you receive an error message. That just doesn't work. There are far too many error messages and errors that you can make. If you are in a hurry to get your program into production, you need to either use a template or find an experienced developer to write it for you, probably for pay. If you have the time and want to learn web development, you won't do it the way you are going. You need to study all the different technologies involved (HTML, CSS, Javascript, PHP, MySQL, just for starters) and create simple web pages, then more complex ones, until you have built the skills necessary to do 90% of it without outside help. That's when a forum like this is very useful. That could take anywhere from a few months to a few years, depending on your previous experience and natural abilities.
"Undefined variable: query ... on line 95" tells you that your code is trying to use a variable ($query) that you have not previously defined or given a value to. Obviously that won't work. It tells you what file and the line number where this problem was found. Now it's up to you to figure out what's missing.
"mysql_num_rows() expects parameter 1 to be resource, null given ... on line 95" tells you that instead of a data resource as the first parameter, the function is being given a null value (nothing). This is virtually always the result of the query failing to produce any data, thus the variable you probably used to hold the data resource has a null value.
Take a look at your code. After including the db_connection.php file (which I'm sure contains the code to connect to your database), you immediately begin a conditional (if) statement that depends on a function that counts how many rows of data were returned by a query--but you haven't executed a query yet! Thus, PHP tells you that the variable $query has not been defined, and MySQL tells you that the row counting function has not been given a valid data resource to count. Both PHP and MySQL are trying their best to show you what's wrong.
If I may make an observation, while there is nothing wrong with being a beginner (all of us had to start from the beginning, obviously), you shouldn't have unrealistic expectations that you can learn programming by consulting a forum every time you receive an error message. That just doesn't work. There are far too many error messages and errors that you can make. If you are in a hurry to get your program into production, you need to either use a template or find an experienced developer to write it for you, probably for pay. If you have the time and want to learn web development, you won't do it the way you are going. You need to study all the different technologies involved (HTML, CSS, Javascript, PHP, MySQL, just for starters) and create simple web pages, then more complex ones, until you have built the skills necessary to do 90% of it without outside help. That's when a forum like this is very useful. That could take anywhere from a few months to a few years, depending on your previous experience and natural abilities.
- chris98
- Forum Contributor
- Posts: 103
- Joined: Tue Jun 11, 2013 10:47 am
- Location: England, United Kingdom
Re: Creating A Question and Answers page.
How would you set up the SQL? Would you have a table for questions, and a seperate table for answers, or would you have a table for each question's answers?
I'll take your advice I think, and I'll try not to consult you all the time, and try to learn most of it myself.
I'll take your advice I think, and I'll try not to consult you all the time, and try to learn most of it myself.
Re: Creating A Question and Answers page.
I would not create a table for every question's answers. Database design (not "SQL" design, by the way, that's the name of the language you use, not the database) is a rather complicated and strict process. It's perhaps the last area that you will need to learn about, but you should at least read some tutorials to begin with. In Google or Bing, search for something like relational database design tutorial.chris98 wrote:How would you set up the SQL? Would you have a table for questions, and a seperate table for answers, or would you have a table for each question's answers?
Please understand, we all want to help you learn how to develop websites and I am not suggesting that you shouldn't use this forum for that purpose. What I am trying to say is that web development is a mix of several rather complicated skills and tools, and you will not succeed in developing your website by tackling each tiny piece of it, one at a time. You need to learn certain fundamentals about HTML, CSS, Javascript, PHP and MySQL before you even BEGIN to think about writing your scripts and markups. The way to do that is to start with a very simple web page and make sure that you can get it to display what you want. Then you can style it with CSS to see how that works. Then you can begin adding some Javascript to manipulate your display dynamically. Then you can begin learning how PHP can be used on the server, before the page is sent to the browser. Then you can begin learning how a database on the server can be used with all the preceding things. It's only by going through all these steps that you will begin to understand how more complicated websites are created.chris98 wrote:I'll take your advice I think, and I'll try not to consult you all the time, and try to learn most of it myself.