Hi everyone

I'm currently working on a survey project,now my problem is that my while loop isn't giving the correct results. i have a mysql table that looks like this.

sectionID SurveyID sectionTitle sectionInstructions
1 2 section1 answer the following questions
2 2 section2 answer the following questions

Ive already queried the questions of the survey from the database to display on a pdf file, now what i want is that for each section of questions the section title should appear.
my current query is

[$query1]=["SELECT * FROM section where SurveyID =".$_GET['survey'];] 

[ $result1]=[ mysql_query( $query1,$link );] 

 [while ( $a_row1 = mysql_fetch_row( $result1 ) )]
{

    [$pdf->ezText($a_row1[2]."\n",10,array('justification'=>'left'));]
[echo "$a_row1[2]";]
}

and displays all the section titles before the questions which is not what i want.

Your help will be appreciated!!!!!

i am not sure but maybe its because its like an array start from 0

hi, i know perfectweb but $a_row1[2] is for the sectionTitle column, so please would you be more specific.

Thanks

try with giving name of columns in query, do not use *,

select sectionid, SurveyID, sectionTitle from section where  where SurveyID =".$_GET['survey'];

or if you are using two tables, then post code and table structure of that table

QuestionID sectionID SurveyID questionText questionType
1 1 2 what is your gender 1
2 1 2 are you black or white 1
3 2 2 what do you know about computers? 3
4 2 2 have you used a computer before? 1

this is the structure of table questions this is where i'm querying the questions to display on the pdf file. and heres the rest of the code

//calls all sectionTitle's from table sections
$query1 = "SELECT * FROM section where SurveyID =".$_GET['survey'];

 $result1 = mysql_query( $query1,$link );
 while ( $a_row1 = mysql_fetch_row( $result1 ) )
{
        $pdf->ezText($a_row1[2]."\n",10,array('justification'=>'left'));
echo "$a_row1[2]";
}


 $query = "SELECT * FROM question where SurveyID =".$_GET['survey'];
 print_r($_GET);

 $count=1;

 $result = mysql_query( $query,$link );
$inc =-1;


//calls all the questions in the survey from table questions
  while ( $a_row = mysql_fetch_row( $result ) )
 {
     $name= $count;

    $pdf->ezText($count.'. '.$a_row[3]."\n",10,array('justification'=>'left'));
 $count++;

 $total = 0;

the table structure for the section table is given on the first problem section I posted.

I think you need loop into loop

a) section query
secion loop
{
    display section title
    question query where section =section_loop_variable
    question loop
    {
        display question title
    }
}

thanks utrivedi i'l try it out.

would you please try writing the code down utrivedi i keep getting errors!!!

thanks in advance.

Eureka i found it thanks people for all your help what i did was i ran two loops at the same time using this code.

$query1 = "SELECT * FROM section where SurveyID =".$_GET['survey'];
$query = "SELECT * FROM question where SurveyID =".$_GET['survey']; 

if(($result1 = mysql_query( $query1,$link )) &&($result = mysql_query( $query,$link )))
{
 while ( ($a_row1 = mysql_fetch_row( $result1 ) )&&( $a_row = mysql_fetch_row( $result ) ))
{

    $pdf->ezText($a_row1[2]."\n",10,array('justification'=>'left'));
    $pdf->ezText($count.'. '.$a_row[3]."\n",10,array('justification'=>'left'));

}
$query1 = "SELECT * FROM section where SurveyID =".$_GET['survey'];
$query = "SELECT * FROM question where SurveyID =".$_GET['survey']; 

if(($result1 = mysql_query( $query1,$link )) &&($result = mysql_query( $query,$link )))
{
 while ( ($a_row1 = mysql_fetch_row( $result1 ) )&&( $a_row = mysql_fetch_row( $result ) ))
{

    $pdf->ezText($a_row1[2]."\n",10,array('justification'=>'left'));
    $pdf->ezText($count.'. '.$a_row[3]."\n",10,array('justification'=>'left'));

}

and it worked.

guyz i thought this problem was solved but what it does is, it displays the sectionTitle's in a single set of questions from 1 section i.e


like this

Section1
1.what is your gender?
Section2
2.How old are you?

whereby both questions are from the same section.

Any suggestions, please help, It'll be highly appreciated.

Two separte loops executing one by one will always give you unexpected results, in your case. Thats why follow my steps, given in my previous post above.
You need loop in loop (nested loop).

ok utrivedi I'm trying it out now I'll show you my code later on.

Utrivedi if it wasn't for the pdf file I'm sure the query would've worked according to specifications if i use the format that you gave me it doesnt show the questions it only shows the sections
let me show you my code

$query1 = "SELECT * FROM section WHERE SurveyID =".$_GET['survey'];
$result1 = mysql_query( $query1,$link );
 while ( $a_row1 = mysql_fetch_row( $result1 ) )
 {
    $query = "SELECT * FROM question WHERE SurveyID =".$_GET['survey']." AND  sectionTitle=".$a_row1[2]."";
    $pdf->ezText($a_row1[2]."\n",15,array('justification'=>'left'));



$result = mysql_query( $query,$link );

//calls all the questions in the survey

$count=1; 
while ( $a_row = mysql_fetch_row( $result ))
 {

      $name= $count; 

  $pdf->ezText($count.'. '.$a_row[3]."\n",10,array('justification'=>'left'));
       $count++;

but when I remove this AND sectionTitle=".$a_row1[2].""; from $query it displays section1 with all the questions from section1 and 2 and then repeats itself for section2 doing the same thing so I really do not know now.

utrivedi your a genius my man !!!!!!!!!!!!!!!!!!!!!!!! i kept forgetting this statement query where section =section_loop_variable I kept putting the surveyID in there. thanks alot my man.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.