View Single Post
Old 11-30-2012, 02:56 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You use joins and let the software structure accordingly.
If you do a query within a loop, it would execute the query for the number of loops. If you had 60 questions, that means you would loop the inner query 60 times which is very expensive.
Pedant's first post there shows the join in question (with the ambiguity of the columns on the join mentioned).

So if I had a questions, and options table (so I could create a radio group for example), that would be similar to so using PHP to format it:
PHP Code:
$sQry 'SELECT q.questionID, q.questionDescription, qo.optionID, qo.option
            FROM question q
            INNER JOIN questionoption qo ON qo.questionID = q.questionID
            ORDER BY q.questionID ASC'
;
if (
$qry mysql_query($sQry))
{
    
$iLast = -1;
    if (
mysql_num_rows($qry) > 0)
    {
        while (
$row mysql_fetch_assoc($qry))
        {
            if (
$row['questionID'] != $iLast)
            {
                
// question has changed
                
if ($iLast > -1)
                {
                    print 
'</select>';
                }
                
$iLast $row['questionID'];
                
printf('<label for="question_%d">%s</label>'$iLast$row['questionDescription']);
                
printf('<select name="question_%d" id="question_%d">'$iLast$iLast);
            }
            
printf('<option value="%s">%s</option>'$row['optionID'], $row['option']);
        }
        print(
'</select>');
    }

I think I biffed the HTML in there, but that should give you the idea.
Fou-Lu is offline   Reply With Quote