Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP
I have 2 tables
question(q_id,question,dept)
answer(a_id,q_id,answer)
 
I want that when i click on particular dept than it should show me question then its answer(can be more than one ) then next question tthen its answer like in forums but its not doing it is printing question then all anwers related to that dept not q_id
<?php
    $q=$_GET["q"];
    $host="localhost";
    $db_name="portal";
    $tb_name="question";
    $con=mysql_connect("$host","root","")or die("cannot connect");
    mysql_select_db("$db_name",$con)or die("cannot connect");
    $sql="SELECT question FROM question WHERE dept='".$q."'";
    //$sql2="SELECT q_id FROM question WHERE dept='".$q."'";
    $sql1="SELECT answer FROM answer WHERE q_id in (SELECT q_id FROM question WHERE dept='".$q."') " ;
 
    $result= mysql_query($sql);
    $result1= mysql_query($sql1);
    while($row = mysql_fetch_array($result) )
    {
        echo "<br>";
        echo "Q. ";
        echo $row[0];
        while($row1 = mysql_fetch_row($result1))
        {
 
        echo "<br>";
        echo "Ans.  ";
 
        echo $row1[0];
        }
    }
Posted 28-Sep-12 8:08am
YoGiV262
Edited 29-Sep-12 0:06am
Joan Murt81.3K
v2
Comments
YoGiV at 28-Sep-12 14:11pm
   
I have 2 tables
question(q_id,question,dept)
answer(a_id,q_id,answer)
 
so i want that when i click on particular dept than it should show me question then its answer(can be more than one ) then next question tthen its answer like in forums but its not doing it is printing question then all anwers related to that dept not q_id
ryanb31 at 28-Sep-12 14:25pm
   
I don't do PHP but you appear to have a loop within a loop. Just a guess, but that is likely why you are seeing everything.
Marcus Kramer at 28-Sep-12 20:57pm
   
That part is ok I think because result1 is his question and result2 is the answers to the question.
Sergey Alexandrovich Kryukov at 28-Sep-12 14:57pm
   
What do you want to achieve? What happened instead? Any other relevant information? Why this code dump?
--SA
Marcus Kramer at 28-Sep-12 15:45pm
   
You need to tell us what "proper result" means first. We cannot magically see into your brain for that information.
macika123 at 28-Sep-12 16:05pm
   
I think this is the guy who previously wrote, that he have to write some question/answer system. For one question several answers can be.
 
I can't edit any foreign articles, this is why I wrote a comment. Maybe the guy describe it better.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hmmm, I think you have some design problems. The question ID should describe the question and this value could be passed in the link, similarly to how you did with the dept-value ( it's a quite freaky thing to call 'q' something that describes 'dept', you can loose control very easy )
 
When you pass the ID to request a question the SQL-queries became more simple. One for getting the question text according to ID (you won't even need a loop) and an another for getting the answers for the question.
 
Edit:
I think the problem is with the second query and where it's used. The result of first query contains all questions in dept.
The second query's result contains all of the answers for these questions.
 
I think, that the second query should be executed within the first loop:
 
    $sql="SELECT question,q_id FROM question WHERE dept='".$q."'";
    
    
 
    $result= mysql_query($sql);
    while($row = mysql_fetch_array($result) )
    {
        echo "<br>";
        echo "Q. ";
        echo $row[0];
        $q_id=$row[1];
        $sql1="SELECT answer FROM answer WHERE q_id='".$q_id."'" ;
        $result1= mysql_query($sql1);
        while($row1 = mysql_fetch_row($result1))
        {
 
        echo "<br>";
        echo "Ans.  ";
 
        echo $row1[0];
        }
    }
 
Another way could be using a join in the queries,so you can get all question-answer pairs as a result, but you must achieve, that each question should be displayed only once. So it's more simply to run multiple sql-queries.
  Permalink  
v2
Comments
Marcus Kramer at 28-Sep-12 20:55pm
   
My vote of 2. There is nothing wrong with his queries themselves, or the way he uses the IDs.
macika123 at 28-Sep-12 21:35pm
   
After your comment I revised my solution, and you are right I had to improve ( by changing the queries, maybe I'm addicted to criticising queries :) ).
 
BTW, I like your method, that you comment why you rated.
Marcus Kramer at 28-Sep-12 22:14pm
   
+5 now. I think your revision nailed it on the head. I didn't see that myself until reading your edit. Well done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 634
1 OriginalGriff 355
2 CPallini 205
3 RyanDev 161
4 Hard_Rockz 140
0 OriginalGriff 5,689
1 Sergey Alexandrovich Kryukov 4,803
2 Peter Leow 2,974
3 Maciej Los 2,819
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100