Jump to content

problems with loops


mattennant

Recommended Posts

I have two tables one contains a row of questions, the other contains users answers to those questions.

 

question table rows are roughly like this

id  -  question1  -  question2 -  question3 - etc etc

 

answer table is

id  -  username - answer1  -  answer2  -  answer3 - etc etc

 

i'm trying to display the results as follows

 

question 1

answer user 1

answer user 2

 

question 2

answer user 1

answer user 2

 

the following code gets me this far

 

question 1

answer user 1

answer user 2

 

question 2

 

but i am unable to display the answers after question 1 - hers's the code

$number = $row_task_question['qnum'];
				for( $y=1; $y<=$number; $y++ ){
                    
                    print "\t\n";
                    print "\t\t<strong>";
                    
				echo  $row_task_question['question'.($y)] .  '</strong><p>';

					do {
					echo $row_task_answer['answer'.($y)] . '<br>';
					echo $row_task_answer['username'].'<P></td>';
					} while ($row_task_answer = mysql_fetch_assoc($task_answer));

                    print "\n";
                   }

 

i'm hoping i'm missing something simple and obvious, but it's driving me crazy, any help or pointers in the right direction would be much appreciated

thanks

mat

 

 

Link to comment
Share on other sites

the query is a bit embarrassing, it's spewed out from dreamweaver,

but here goes, here's the answer query

$colname2_task_answer = "-1";
if (isset($_GET['step_ref'])) {
  $colname2_task_answer = (get_magic_quotes_gpc()) ? $_GET['step_ref'] : addslashes($_GET['step_ref']);
}
$colname_task_answer = "-1";
if (isset($_GET['task_ref'])) {
  $colname_task_answer = (get_magic_quotes_gpc()) ? $_GET['task_ref'] : addslashes($_GET['task_ref']);
}
mysql_select_db($database_mattyboy, $mattyboy);
$query_task_answer = sprintf("SELECT * FROM answer_text WHERE task_ref = %s AND step_ref = %s", $colname_task_answer,$colname2_task_answer);
$task_answer = mysql_query($query_task_answer, $mattyboy) or die(mysql_error());
$row_task_answer = mysql_fetch_assoc($task_answer);
$totalRows_task_answer = mysql_num_rows($task_answer); 

and here's the  here's the question query

$colname2_task_question = "-1";
if (isset($_GET['step_ref'])) {
  $colname2_task_question = (get_magic_quotes_gpc()) ? $_GET['step_ref'] : addslashes($_GET['step_ref']);
}
$colname_task_question = "-1";
if (isset($_GET['task_ref'])) {
  $colname_task_question = (get_magic_quotes_gpc()) ? $_GET['task_ref'] : addslashes($_GET['task_ref']);
}
mysql_select_db($database_mattyboy, $mattyboy);
$query_task_question = sprintf("SELECT * FROM question_text WHERE task_ref = '%s' AND step_ref = '%s'", $colname_task_question,$colname2_task_question);
$task_question = mysql_query($query_task_question, $mattyboy) or die(mysql_error());
$row_task_question = mysql_fetch_assoc($task_question);
$totalRows_task_question = mysql_num_rows($task_question);

I Know

Link to comment
Share on other sites

just a suggestion its dont wanna read the code lol

 

do a select statement like this..

select t1.question,t2.answer form table1 t1
left joint table2 t2 on t1.id=t2.id
where condition here...
[code]

running this kind of query will give you what you need this is allot shorter and easier i guess?


[/code]

Link to comment
Share on other sites

<?php

$question_1 = mysql_query("SELECT question1 FROM questions");

while($row_1=mysql_fetch_array($question_1)){

$answer = mysql_fetch_array(mysql_query("SELECT * FROM answers WHERE step_ref = '$row_1[question1]'"));

// Question 1
echo $row['question1'];
//Answer 1
echo $answer['answer_1'];


}
}

$question_2 = mysql_query("SELECT question2 FROM questions");

while($row_2=mysql_fetch_array($question_1)){

$answer = mysql_fetch_array(mysql_query("SELECT * FROM answers WHERE step_ref = '$row_2[question2]'"));

// Question 2
echo $row['question2'];
//Answer 2
echo $answer['answer_2'];


}
}

?>

 

and keep going for question 3.

 

I am sorry, this isnt the right way of doing it but...

Link to comment
Share on other sites

 

@phpSensei

I dont understand you .. look if you will query tables and theres a relationship with them theres no other way to do it but to join them

this step_ref  is only part of the condition and has nothing to do with the relationship of table

hmm .. im confuzed with your idea

Link to comment
Share on other sites

 

@phpSensei

I dont understand you .. look if you will query tables and theres a relationship with them theres no other way to do it but to join them

this step_ref  is only part of the condition and has nothing to do with the relationship of table

hmm .. im confuzed with your idea

 

Well he put a ID there, meaning there must be like

 

id = 1

question1

question2

question3

 

then

 

id

step_ref

answer1

answer2

answer3

 

 

Now lets say there was id 1 and 2 for the questions meaning

 

id = 1

question1

question2

question3

 

id = 2

question1

question2

question3

 

 

so step_ref can be 1 or 2 depending on the ID of the question.

 

Know what i mean?

 

 

My query won't work, because I am a little confused with the way he has setup his database tables.

Link to comment
Share on other sites

does this look right to you, getting a mysql error

 

select question_text.question1, answer_text.answer1

from question_text

left joint answer_text.foreign  on question_text  question_text.number=answer_text.foreign

FROM question_text

WHERE task_ref = 'colname' AND step_ref = 'colname2'

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.