Jump to content

loop help


EKsparky

Recommended Posts

As a complete php Mysql noob I am as pleased as punch that I managed program my own quiz for my website

Check it out here Clicky Clicky

I no that ive probably used thousands more lines than I needed to be repeating code that could have gone on functions and it could be done better for faster query's But I guess that will come in time.

My only problem is I get repeated questions some times now I don't wont to change to much code as ive made over 20 pages.

So I was wondering if I could use and if or while statement to loop my sql query if questions id match

my sql table has four columns id, categories, questions, and answers,

Here the code for one of my pages

 

<?php
include 'modules/quiz/includes/config.php';
include 'modules//quiz/includes/opendb.php';

echo '<script type="text/javascript">
function show(id){
  var ans = document.getElementById(id);
  ans.style.display = (ans.style.display=="none") ? "inline":"none";
  return false;
}
</script>';

OpenTable();
echo '<link href="modules/quiz/style/style.css" rel="stylesheet" type="text/css" />';
############ STRART TABLE ############
echo '<table width="1000" border="1" align="center" cellpadding="1" cellspacing="1" >
  <tr><td class="td_border">';


echo '<table width="800" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#000000">
  <tr>
    <td class="td_border" colspan="3"><div align="center">
      <h2>TAGB 10th Kup Theory Test </h2>
    </div></td>';
    
############ REFRESH BTN ############   

echo '<td class="td_border"><SCRIPT LANGUAGE="JavaScript">

document.write(\'<form><input type=button value="New Questions" onClick="history.go()"></form>\')

</script> </td>
  </tr>
  <tr>';
  
########## QUESTION ONE ##################
#select a query
$sql = "SELECT * from quiz_korean  WHERE cat = 'ten_kup_korean' ORDER BY RAND() LIMIT 1 ";

#excute query
$dbname = mysql_query ( $sql );


#write data
while ( $row = mysql_fetch_array ( $dbname ) )
{

	$Q = (  $row["question"] );
	$A = ( $row["answer"] ); 

     echo '<td class="td_border" width="250" >'.$Q.'</td>';
     echo ' <td class="td_border" width="60" align="center"><input value="Answer" onclick="return show(\'q1\');" type="button"></td>';
     echo '<td class="td_border" width="390"><span id=\'q1\' style="display: none">  '.$A.'</span></td>';
}     
########## IMAGE ################## 
   
     echo'<td class="td_border" width="100" rowspan="9"><img src="modules/quiz/images/tkd_korean.gif" width="100" height="368" /></td>
     </tr>
     <tr>';
     
  ########## QUESTION TWO ##################
#select a query
$sql = "SELECT * from quiz_korean  WHERE cat = 'ten_kup_korean' ORDER BY RAND() LIMIT 1 ";

#excute query
$dbname = mysql_query ( $sql );


#write data
while ( $row = mysql_fetch_array ( $dbname ) )
{

	$Q = (  $row["question"] );
	$A = ( $row["answer"] );
	     
     echo '<td class="td_border">'.$Q.'</td>';
     echo ' <td class="td_border" width="60" align="center"><input value="Answer" onclick="return show(\'q2\');" type="button"></td>';
     echo' <td class="td_border"><span id=\'q2\' style="display: none">  '.$A.'</span></td>
     </tr>
     <tr>';
}     
########## QUESTION THREE ##################
#select a query
$sql = "SELECT * from quiz_korean  WHERE cat = 'ten_kup_korean' ORDER BY RAND() LIMIT 1 ";

#excute query
$dbname = mysql_query ( $sql );


#write data
while ( $row = mysql_fetch_array ( $dbname ) )
{

	$Q = (  $row["question"] );
	$A = ( $row["answer"] ); 
	     
     echo' <td class="td_border">'.$Q.'</td>';
     echo ' <td class="td_border" width="60" align="center"><input value="Answer" onclick="return show(\'q3\');" type="button"></td>';
     echo'<td class="td_border"><span id=\'q3\' style="display: none">  '.$A.'</span></td>
     </tr>
     <tr>';
}     
########## QUESTION FOUR ##################
#select a query
$sql = "SELECT * from quiz_korean  WHERE cat = 'ten_kup_korean' ORDER BY RAND() LIMIT 1 ";

#excute query
$dbname = mysql_query ( $sql );


#write data
while ( $row = mysql_fetch_array ( $dbname ) )
{

	$Q = (  $row["question"] );
	$A = ( $row["answer"] ); 
	     
     echo' <td class="td_border">'.$Q.'</td>';
     echo ' <td class="td_border" width="60" align="center"><input value="Answer" onclick="return show(\'q4\');" type="button"></td>';
     echo' <td class="td_border"><span id=\'q4\' style="display: none">  '.$A.'</span></td>
     </tr>
     <tr>';
}     
  ########## QUESTION FIVE ##################
#select a query
$sql = "SELECT * from quiz_korean  WHERE cat = 'ten_kup_korean' ORDER BY RAND() LIMIT 1 ";

#excute query
$dbname = mysql_query ( $sql );


#write data
while ( $row = mysql_fetch_array ( $dbname ) )
{

	$Q = (  $row["question"] );
	$A = ( $row["answer"] );    
     
     echo'<td class="td_border">'.$Q.'</td>';
     echo ' <td class="td_border" width="60" align="center"><input value="Answer" onclick="return show(\'q5\');" type="button"></td>';
     echo' <td class="td_border"><span id=\'q5\' style="display: none">  '.$A.'</span></td>
     </tr>
     <tr>';
}     
########## QUESTION SIX ##################
#select a query
$sql = "SELECT * from quiz_korean  WHERE cat = 'ten_kup_korean' ORDER BY RAND() LIMIT 1 ";

#excute query
$dbname = mysql_query ( $sql );


#write data
while ( $row = mysql_fetch_array ( $dbname ) )
{

	$Q = (  $row["question"] );
	$A = ( $row["answer"] );      
     
     echo' <td class="td_border">'.$Q.'</td>';
     echo ' <td class="td_border" width="60" align="center"><input value="Answer" onclick="return show(\'q6\');" type="button"></td>';
     echo'<td class="td_border"><span id=\'q6\' style="display: none">  '.$A.'</span></td>
     </tr>
     <tr>';
}     
########## QUESTION SEVEN ################## 
#select a query
$sql = "SELECT * from quiz_term  WHERE cat = 'ten_kup_term ' ORDER BY RAND() LIMIT 1 ";

#excute query
$dbname = mysql_query ( $sql );

#write data
while ( $row = mysql_fetch_array ( $dbname ) )
{

	$Q = (  $row["question"] );
	$A = ( $row["answer"] );     
     
     echo' <td class="td_border" align="left" valign="top">'.$Q.'</td>';
     echo ' <td class="td_border" width="60" align="center"><input value="Answer" onclick="return show(\'q7\');" type="button"></td>';
     echo'<td class="td_border" height="80" align="left" valign="top"><span id=\'q7\' style="display: none">  '.$A.'</span></td>
     </tr>
     <tr>';
}     
########## QUESTION EIGHT ################## 
#select a query
$sql = "SELECT * from quiz_term  WHERE cat = 'ten_kup_term ' ORDER BY RAND() LIMIT 1 ";

#excute query
$dbname = mysql_query ( $sql );

#write data
while ( $row = mysql_fetch_array ( $dbname ) )
{

	$Q = (  $row["question"] );
	$A = ( $row["answer"] );     
     
    echo'  <td class="td_border" align="left" valign="top">'.$Q.'</td>';
    echo ' <td class="td_border" width="60" align="center"><input value="Answer" onclick="return show(\'q8\');" type="button"></td>';
    echo'<td class="td_border" height="80" align="left" valign="top"><span id=\'q8\' style="display: none">  '.$A.'</span></td>
    </tr>
    <tr>';
}    
########## QUESTION NINE ################## 
#select a query
$sql = "SELECT * from quiz_term  WHERE cat = 'ten_kup_term ' ORDER BY RAND() LIMIT 1 ";

#excute query
$dbname = mysql_query ( $sql );

#write data
while ( $row = mysql_fetch_array ( $dbname ) )
{

	$Q = (  $row["question"] );
	$A = ( $row["answer"] );  
    
    echo'  <td class="td_border" align="left" valign="top">'.$Q.'</td>';
    echo ' <td class="td_border" width="60" align="center"><input value="Answer" onclick="return show(\'q9\');" type="button"></td>';
    echo '<td class="td_border" height="80" align="left" valign="top"><span id=\'q9\' style="display: none">  '.$A.'</span></td>
   </tr>
   </table>
</td>
  </tr>
</table>';  
  
}
    echo '<div align="center"><br /><b><a href="javascript:history.back();">Back</a></b></div>';
    
  

?>

As you can see I have 6 questions from database table and 3 from a different table

 

Now is there a way I can do something like this

<?php
#select a query
$sql = "SELECT * from quiz_korean  WHERE cat = 'ten_kup_korean' ORDER BY RAND() LIMIT 1 ";

#excute query
$dbname = mysql_query ( $sql );
########question one #########

#write data
while ( $row = mysql_fetch_array ( $dbname ) )
{
                       $id = ( $row["ID"];
	$Q = (  $row["question"] );
	$A = ( $row["answer"] );      
     
     echo' <td class="td_border">'.$Q.'</td>';
     echo ' <td class="td_border" width="60" align="center"><input value="Answer" onclick="return show(\'q6\');" type="button"></td>';
     echo'<td class="td_border"><span id=\'q6\' style="display: none">  '.$A.'</span></t';
#######question 2############select a query


$sql = "SELECT * from quiz_korean  WHERE cat = 'ten_kup_korean' ORDER BY RAND() LIMIT 1 ";

#excute query
$dbname = mysql_query ( $sql );


#write data
while ( $row = mysql_fetch_array ( $dbname ) )
{
                       $id2 = ( $row["ID"];
	$Q = (  $row["question"] );
	$A = ( $row["answer"] );      
                 if ($id == $id2)
   {SOME HOW GO BACK TO $sql}ELSE{

     echo' <td class="td_border">'.$Q.'</td>';
     echo ' <td class="td_border" width="60" align="center"><input value="Answer" onclick="return show(\'q6\');" type="button"></td>';
     echo'<td class="td_border"><span id=\'q6\' style="display: none">  '.$A.'</span></t}';

?>


 

I added id then I would need to carry this on six times  I guess giving each question $id $id2 $id3 $id4 $id5 $id6

 

Any help which out changing to much code would be great thanks

Link to comment
https://forums.phpfreaks.com/topic/48932-loop-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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