Jump to content

Problem with loops?


JoelRocks

Recommended Posts

Hey guys, sorry again

 

<?php
$con = mysql_connect("localhost","remotepa_joel","fanfrog");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("remotepa_harrypotter", $con);

$result = mysql_query("SELECT * FROM tbl_question");

while($row = mysql_fetch_array($result))
  {
?>
<?echo($row['question']);?>
<form action="sqltest.php" method="post">
<input type="radio" name=" <?echo($row['question']);?>" value="<? echo ($row['option_one']); ?>"><? echo ($row['option_one']); ?>
<br />
<input type="radio" name=" <?echo($row['question']);?>" value="<? echo ($row['option_two']); ?>"><? echo ($row['option_two']); ?>
<br />
<input type="radio" name=" <?echo($row['question']);?>" value="<? echo ($row['option_three']); ?>"><? echo ($row['option_three']); ?>
<br />
<input type="radio" name=" <?echo($row['question']);?>" value="<? echo ($row['option_four']); ?>"><? echo ($row['option_four']); ?>
<br />
<input type="submit"> 
</form> 
<?

  $answer=$row["answer"];



$current_question =$row['question'];

$input=($_POST[$current_question]);
if (strcasecmp($answer, $input) == 0)
{
	echo("Answer Correct");
}
else
{
	echo("Answer Incorrect");
}

echo("<br />");
//echo($input);
}
mysql_close($con);
?>

 

Having a problem with the loop, works with one question but does not work where there is more than one.

Link to comment
https://forums.phpfreaks.com/topic/49089-problem-with-loops/
Share on other sites

Your problem is not with the loop itself. You are calling the page again with your form and it is doing the query over again and of course Question 1 is first everytime. I am self taught so maybe there is a more efficient way but I would put a hidden value (question #) into the form and advance it by one each time you hit submit and that way the query would be able to pull question 2 the next pass through. So something like this would probably work (I have not tested it though)

 

<?php
$con = mysql_connect("localhost","remotepa_joel","fanfrog");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

if($_REQUEST['question_number'] == "")
{
  $question_number = "1"
}
else
{
  $question_number = $_REQUEST['question_number']; // maybe a more efficient way to do this and the next line together
  $question_number = $question_number++;
}
mysql_select_db("remotepa_harrypotter", $con);

$result = mysql_query("SELECT * FROM tbl_question WHERE question = '$question'");

while($row = mysql_fetch_array($result))
  {
?>
<?echo($row['question']);?>
<form action="sqltest.php" method="post">
<input type="radio" name=" <?echo($row['question']);?>" value="<? echo ($row['option_one']); ?>"><? echo ($row['option_one']); ?>
<br />
<input type="radio" name=" <?echo($row['question']);?>" value="<? echo ($row['option_two']); ?>"><? echo ($row['option_two']); ?>
<br />
<input type="radio" name=" <?echo($row['question']);?>" value="<? echo ($row['option_three']); ?>"><? echo ($row['option_three']); ?>
<br />
<input type="radio" name=" <?echo($row['question']);?>" value="<? echo ($row['option_four']); ?>"><? echo ($row['option_four']); ?>
<br />
             <input type="hidden" name="question_number" value="<?$question_number;?>">
<input type="submit"> 
</form> 
<?

  $answer=$row["answer"];



$current_question =$row['question'];

$input=($_POST[$current_question]);
if (strcasecmp($answer, $input) == 0)
{
	echo("Answer Correct");
}
else
{
	echo("Answer Incorrect");
}

echo("<br />");
//echo($input);
}
mysql_close($con);
?>

Link to comment
https://forums.phpfreaks.com/topic/49089-problem-with-loops/#findComment-240562
Share on other sites

This is the best I can do you without knowing what your trying to do. This is more or less just a code clean up.

<?php
$con = mysql_connect("localhost","remotepa_joel","fanfrog");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

$select = mysql_select_db("remotepa_harrypotter", $con);
if (!$select)
{
    die('Could not select database: ' . mysql_error());
}

$result = mysql_query("SELECT * FROM tbl_question");
if (!$results)
{
    echo "No questions avalible!";
}
else
{
    while($row == mysql_fetch_array($result))
    {
    ?>
        <? echo $row['question']; ?>
        <form action="sqltest.php" method="post">
            <input type="radio" name="<? echo $row['question']; ?>" value="<? echo $row['option_one']; ?>" /> <? echo $row['option_one']; ?>
            <br />
            <input type="radio" name="<? echo $row['question']; ?>" value="<? echo $row['option_two']; ?>" /> <? echo $row['option_two']; ?>
            <br />
            <input type="radio" name="<? echo $row['question']; ?>" value="<? echo $row['option_three']; ?>" /> <? echo $row['option_three']; ?>
            <br />
            <input type="radio" name="<? echo $row['question']; ?>" value="<? echo $row['option_four']; ?>" /> <? echo $row['option_four']; ?>
            <br />	
            <input type="submit" value="Submit">
        </form> 
    <?

        $answer = $row["answer"];

        $current_question = $row['question'];

        $input = $_POST[$current_question];
        if (strcasecmp($answer, $input) == 0)
    {
            echo("Answer Correct");
    }
        else
    {
   	   echo("Answer Incorrect");
    }

        echo("<br />");
        //echo($input);
    }
}
mysql_close($con);
?>

 

If this doesn't work, please explain what your trying to do. With more information, I may be able to help you more...

Link to comment
https://forums.phpfreaks.com/topic/49089-problem-with-loops/#findComment-240567
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.