PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hello All,
First time poster
I am having a looping issue that I cannot seem to find an answer to.
I have a db with two tables in it.
table 1: Questions
table 2: Question Responses
I would like to loop through these and pull back the following example:
Gender?
Male
Female
Favorite Color?
Red
Green
Blue
However what I am getting back is not exaclty correct:
Gender?
Male
Female
Red
Green
Blue
Favorite Color?
Male
Female
Red
Green
Blue
This is my nightmare in a nutshell:
I am trying to display the data in the following application =
http://www.gerd-tentler.de/tools/phpgraphs/example.php
as you can see it takes its data in a Coma seperated vlaue type arrangement.. I am doing this like such
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by Faded19 on Thu May 03, 2007 5:44 pm, edited 1 time in total.
Can you var_dump both result sets so we can see how they relate? You may be able to do with with one loop inside of another, but there needs to be a relationship somewhere.
Survey_Responses does not need a field for survey id since the survey question id is related to survey id and the survey response id is related to the survey question id. Now, on to how you get your information set up...
<?php
$survey = 12; // Or however you want to set this value, it is up to you
// Start by getting the questions
$sql = "SELECT * FROM Survey_Question WHERE `SID` = $survey";
if (! $result = mysql_query($sql))
{
die('Could not execute the query: ' . $sql . '<br />' . mysql_error());
}
$questions = array();
if (mysql_num_rows($result) < 1)
{
echo 'There were no questions found for this survey.';
}
else
{
while ($row = mysql_fetch_array($result))
{
$questions[] = $row;
}
}
$questions_count = count($questions);
// Now lets get some responses
// We are going to fetch all of them but only show those we need
$sql = "SELECT * FROM Question_Responses";
if (! $result = mysql_query($sql))
{
die('Could not execute the query: ' . $sql . '<br />' . mysql_error());
}
$responses = array();
if (mysql_num_rows($result) < 1)
{
echo 'There were no responses found.';
}
else
{
while ($row = mysql_fetch_array($result))
{
$responses[] = $row;
}
}
$responses_count = count($responses);
// Now we loop the questions, as long as we have data
if ($questions_count && $responses_count)
{
for ($i = 0; $i < $questions_count; $i++)
{
$qid = $questions[$i]['SQID'];
echo '<h2>' . $questions[$i]['Question_Text'] . '</h2>';
// Now, while we are in the questions loop, we loop the responses
for ($j = 0; $j < $responses_count; $j++)
{
if ($qid == $responses[$j]['SQID'])
{
echo $response[$j]['Response_Text'] . '<br />';
}
}
}
}
?>
There are a few different ways to do what you want. I like the idea of running two queries only, then looping and matching. Of course, you can do whatever works for you. Oh yeah, you are going to have to handle your own formatting. I threw this out there only as a way to show you how the data could be accessed and displayed.