pleeeez!! so easy but??!

pleeeez!! so easy but??!

am 12.02.2006 00:01:22 von Beshoo

hi
I have this function to print each question and it`s answar / actually
from 2 tables answers and questions /

I am taking the "question id" to be the "answer parent_id",so that I
can know each question and its answers..... ok!!

the quey like this:

function display (){
$sql="SELECT * from answers,questions WHERE
answers.parent_id=questions.question_id";
$exe=mysql_query($sql);
while ($row=mysql_fetch_assoc($exe))
{
echo $row['answer']."
";
echo $row['quest']."
";

}
}
display();

the problem is.. in this way the question will be printed more than one
time , in another word the questios will be printed each time its
answer printed !!!

SO how I can print the question just one time???!!!
pleeeez !!!
thank you very much my frnds !!!

Re: pleeeez!! so easy but??!

am 12.02.2006 01:25:13 von Shion

Beshoo wrote:
> hi
> I have this function to print each question and it`s answar / actually
> from 2 tables answers and questions /
>
> I am taking the "question id" to be the "answer parent_id",so that I
> can know each question and its answers..... ok!!
>
> the quey like this:
>
> function display (){
> $sql="SELECT * from answers,questions WHERE
> answers.parent_id=questions.question_id";
> $exe=mysql_query($sql);
> while ($row=mysql_fetch_assoc($exe))
> {
> echo $row['answer']."
";
> echo $row['quest']."
";
>
> }
> }
> display();
>
> the problem is.. in this way the question will be printed more than one
> time , in another word the questios will be printed each time its
> answer printed !!!
>
> SO how I can print the question just one time???!!!
> pleeeez !!!
> thank you very much my frnds !!!
>
You can use the DISTINCT option when you fetch the data from the database,
this way you will get just one line even if the are doublets.

If you have more than one answer to the same question, then I would suggest
you keep track of the question.id and compare it with the current
question.id, if it's the same then don't print the answer, if it's different,
then print and set the storage variable to the new question.id.


//Aho

Re: pleeeez!! so easy but??!

am 14.02.2006 00:49:09 von Siv Hansen

J.O. Aho wrote:
> Beshoo wrote:
>
>> hi
>> I have this function to print each question and it`s answar / actually
>> from 2 tables answers and questions /
>>
>> I am taking the "question id" to be the "answer parent_id",so that I
>> can know each question and its answers..... ok!!
>>
>> the quey like this:
>>
>> function display (){
>> $sql="SELECT * from answers,questions WHERE
>> answers.parent_id=questions.question_id";
>> $exe=mysql_query($sql);
>> while ($row=mysql_fetch_assoc($exe))
>> {
>> echo $row['answer']."
";
>> echo $row['quest']."
";
>>
>> }
>> }
>> display();
>>
>> the problem is.. in this way the question will be printed more than one
>> time , in another word the questios will be printed each time its
>> answer printed !!!
>>
>> SO how I can print the question just one time???!!!
>> pleeeez !!!
>> thank you very much my frnds !!!

You could use a query like this:

$connection = mysql_connect('myHost', 'myUsername', 'myPassword') or
die("ConnectionError".mysql_error());
$database = mysql_select_db('myDatabase') or die ("DatabaseError ".
mysql_error());

$query = mysql_query("select id from table1") or die("Error in query1:
".mysql_error());

print ("

");


What this snippet does is to select all the rows from table1 (only the
id in this case), comparable to your questions.
Then it takes the result from the first query as input to a second...
(your answeres) and performes a join over the $result['id'] from the
outer query. And finally displays the result as a nicely formatted list ;o)
(most likely not valid html)

>>
> You can use the DISTINCT option when you fetch the data from the
> database, this way you will get just one line even if the are doublets.
>
> If you have more than one answer to the same question, then I would
> suggest you keep track of the question.id and compare it with the
> current question.id, if it's the same then don't print the answer, if
> it's different, then print and set the storage variable to the new
> question.id.
>
>
> //Aho

Re: pleeeez!! so easy but??!

am 23.02.2006 11:06:32 von Jim Michaels

"Beshoo" wrote in message
news:1139698882.214335.219900@g43g2000cwa.googlegroups.com.. .
> hi
> I have this function to print each question and it`s answar / actually
> from 2 tables answers and questions /
>
> I am taking the "question id" to be the "answer parent_id",so that I
> can know each question and its answers..... ok!!
>
> the quey like this:
>
> function display (){
> $sql="SELECT * from answers,questions WHERE
> answers.parent_id=questions.question_id";
> $exe=mysql_query($sql);
> while ($row=mysql_fetch_assoc($exe))
> {
> echo $row['answer']."
";
> echo $row['quest']."
";

I hear one area you are going to run into trouble is your dotted column
names. you will need to use AS to rename the columns before you can use
them like $row['answer'] and such. here's why:
C:\Documents and Settings\Jim>php test1.php
Array
(
[0] => 1
[cat_id] => 1
[1] => a
[category] => a
[2] => 1
)

C:\Documents and Settings\Jim>type test1.php
mysql_connect("localhost","username","password");
mysql_select_db("database");
$q=mysql_query("SELECT * FROM categories,categoryroots WHERE
categories.cat_id=categoryroots.cat_id") or die("oops1.");
$row=mysql_fetch_array($q);
print_r($row);
mysql_free_result($q);
?>

C:\Documents and Settings\Jim>

well, I got everything from the categories table (trhe first table mentioned
in the SELECT). Because I didn't specify the columns I wanted from both
tables.





>
> }
> }
> display();
>
> the problem is.. in this way the question will be printed more than one
> time , in another word the questios will be printed each time its
> answer printed !!!
>
> SO how I can print the question just one time???!!!
> pleeeez !!!
> thank you very much my frnds !!!
>