SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    London, United Kingdom
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    question on using foreach loops when retrieving data

    I was just recently introduced to the foreach loop, when I was having trouble showing questions and their realted answers appropriately when the data was being returned.

    At the moment it is only handling 2 fields coming back from the db, and now I need greater flexibility I need to bring back a whole lot more. So the question is how do I structure this array, to be able to bring back and be able to manipulate the variables related to the fields, instead of throwing everything in the array into one var?


    Code:
    $unhidefirst = @mysql_query("SELECT DISTINCT QuestionID, ButtonClass, Question, Answer, ButtonNumberArr FROM answers, questions WHERE QuestionID = QuestionNum");
    
    if (!$unhidefirst) {
    exit('<p>answers query failed: '.mysql_error().'</p>');	
    }
    else {}
    
    $groups2 = array();
    
    while ($row = mysql_fetch_array($unhidefirst)) {
    	
    $groups2[$row['QuestionID']] [] = $row['Answer']; 
    		  
    }
    
    foreach ($groups2 as $QuestionID => $buttonClasses) {
    
    	echo "<p>$QuestionID</p>";
    	echo "<br/>";
    	
       
       foreach ($buttonClasses as $unhidefirst) {
        
           echo "<p>$unhidefirst</p>";
    	   echo "<br/>";
       }   
    
    }

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not really sure what you're asking for. Maybe this?
    PHP Code:
    //$groups2[$row['QuestionID']] [] = $row['Answer']; 
    $groups2[$row['QuestionID']][] = $row

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    London, United Kingdom
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well what I meant was how do you bring back more information from the db in such a way that you can echo it in the proper order?

    for example, I need to bring back the question id as well as question and put it next to question. I also need to bring back the answer arrangement and put it next to answer, in the output. These will all become either select or input boxes.

    So I just need a way to be able to bring back more information and output it to html nicely.

    The way that it is doing it now for just questions and answers is perfect, structurally, but is now missing additional information.

    I tried this:

    Code:
    $groups2[$row['Question']] [] = $row['Answer'];
    $groups2[$row['QuestionNum']] [] = $row['AnswerArrangement'];
              
    }
    
    foreach ($groups2 as $QuestionID => $buttonClasses) {
    
        echo "<p>$QuestionID</p>";
        echo "<br/>";
    where I added more fields to the array, but that won't allow me to seperate the field data into individual variables which I can echo into their relevant div containers.

    I hope this helps for you to understand my problem.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using what I did in my previous post

    I'm playing on the fact that each row in $rows will have the same value for Question. So I just choose the first row, because we know there will be at least one row.
    (I assume, a specific QuestionID has only one value for Question)
    PHP Code:
    foreach ($groups2 as $QuestionID => $rows) {

        echo 
    "<p>$QuestionID $rows[0][question]</p>";
        echo 
    "<br/>";

        foreach (
    $rows as $row) {
            echo 
    "$row[Answer] $row[AnswerArrangement] \n";
        }


    The way you structure the array, and then later use it for output will really vary depending on your needs. Sometimes, stuff like this is best served by providing us with well chosen samples of a database result, and then maybe some minimal desired html.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    Your going to need the question and answer primary key to order the result in that hierarchical manor. What is the primary key for the Answers table – AnswerID? I assume The primary key for the Questions table is QuestionID, correct?

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    What you would be looking at is something like this hypothetically speaking:

    PHP Code:
    $questions = array();

    $questionPK 'QuestionID';
    $answerPK 'AnswerID';

    while(
    $row=mysql_fetch_assoc($result)) {
        
        
    $current null;
        
        foreach(
    $questions as &$question) {
                
            if(
    $question[$questionPK]==$row[$questionPK]) {
            
                
    $current =& $question;
                break;
                
            }
            
        }
        
        if(
    is_null($current)) {
            
            
    $current = array(
                
    $questionPK=>$row[$questionPK]
                ,
    'answers'=>array()
            );
            
            
    $questions[] =& $current;
            
        }
        
        
        if(
    is_null($row[$answerPK])) {
            
            continue;
        
        }
        
        
        foreach(
    $questions['answers'] as $answer) {
        
            if(
    $answer[$answerPK]==$row[$answerPK]) {
            
                continue;
                
            }        
        
        }
        
        
    $current['answers'][] = array(
               
    $answerPK=>$row[$answerPK]
           );
        


    I would prefer to use objects, but I'm not sure how familiar you are with object-oriented programming and rather not make it any more difficult to understand.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •