HELP...Nested Looping issue

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!

Moderator: General Moderators

Post Reply
Faded19
Forum Newbie
Posts: 3
Joined: Thu May 03, 2007 5:11 pm

HELP...Nested Looping issue

Post by Faded19 »

feyd | Please use

Code: Select all

,

Code: Select all

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

Code: Select all

while ($Rows = mysql_fetch_array($NewQuestionsList))
   {	
      $RelatedAnswers =& ReportsListingDAO::AnswerOptionsPerQuestion($Rows[SQID]);
      $newGraphValues = $graphValues.$Rows[SQID];
      $newGraphLabels = $graphLabels.$Rows[SQID];

      while ($AnswerRows = mysql_fetch_array($RelatedAnswers))
         {
	 $Answers .= $AnswerRows[Response_Text].',';
         }
							 
       $NewAnswers = substr($Answers, 0, -1);        
       $newGraphLabels = $NewAnswers;
    }
I believe the problem is the concantenation of the $Answers string as it just spits out one long string of all the answers...

The quesiton is? how do I break this up? so that it displays incrementally.

Thanks a million
Bryan


feyd | Please use

Code: Select all

,

Code: Select all

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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Descriptive subjects

Post by feyd »

Can you make the title of this thread a bit more specific and descriptive?
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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.
Faded19
Forum Newbie
Posts: 3
Joined: Thu May 03, 2007 5:11 pm

Post by Faded19 »

Thank-You all for your help
I thought this might help it is the table set up I am using and sample data:

Survey
----------------------------
| SID | Survey_Descr |
----------------------------
| 1 | Test Survey |
----------------------------


Survey_Question
-------------------------------------------------------------
| SQID | SID | Question_Text | Question_Order |
-------------------------------------------------------------
| 1 | 1 | Gender? | 1 |
-------------------------------------------------------------
| 2 | 1 | Your Fav Color| 2 |
-------------------------------------------------------------


Survey_Responses
----------------------------------------------------------------------------------------------
| SRID | SID | SQID | Response_Text | Response_Type | Response_Order |
----------------------------------------------------------------------------------------------
| 1 | 1 | 1 | Male | Checkbox | 1 |
----------------------------------------------------------------------------------------------
| 2 | 1 | 1 | Female | Checkbox | 2 |
----------------------------------------------------------------------------------------------
| 3 | 1 | 2 | Red | Checkbox | 1 |
----------------------------------------------------------------------------------------------
| 4 | 1 | 2 | Orange | Checkbox | 2 |
----------------------------------------------------------------------------------------------
| 5 | 1 | 2 | Yellow | Checkbox | 3 |
----------------------------------------------------------------------------------------------
| 6 | 1 | 2 | Green | Checkbox | 4 |
----------------------------------------------------------------------------------------------
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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...

Code: Select all

<?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.
Faded19
Forum Newbie
Posts: 3
Joined: Thu May 03, 2007 5:11 pm

Post by Faded19 »

Everah

Dear God..Thank-You so much...I can't tell you how much this has helped me...

Bryan
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I wasn't able to test, but it sounds like it is working. Yeah?
Post Reply