i m trying to increment value 1 using while loop condition, i m developing quiz generation system, but stuck here and not found any solution, please help and tell what to do....
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$i = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$i++;
$qech1=$rows['optiona'];
$qech2=$rows['optionb'];
$qech3=$rows['optionc'];
$qech4=$rows['optiond'];
$correct=$rows['correct'];
?>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech1; ?>'><?php echo $qech1."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech2; ?>'><?php echo $qech2."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech3; ?>'><?php echo $qech3."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech4; ?>'><?php echo $qech4."<br>"; ?><br>
<?php
}
?>
you seen above code, it is $i = 1; and then i echo $i as well in 4 fields of radio. it retrives around 3 questions from database and each question has 4 values, but all of the total of 12 values of 3 questions has same name that is 1, it is not increment using $i++ not sure why ?
here is out put
<span style='font-size:18px;font-weight:550px; '>english</span><br><input type='radio' name='1' value='kjk'>kjk<br><br>
<input type='radio' name='2' value='kjk'>kjk<br><br>
<input type='radio' name='2' value='jkj'>jkj<br><br>
<input type='radio' name='2' value='kjk'>kjk<br><br>
<span style='font-size:18px;font-weight:550px; '>adnan </span><br><input type='radio' name='1' value='jhwjksahajsh'>jhwjksahajsh<br><br>
<input type='radio' name='2' value='kjhjhjh'>kjhjhjh<br><br>
<input type='radio' name='2' value='kjhkjjh'>kjhkjjh<br><br>
<input type='radio' name='2' value='hjkjhj'>hjkjhj<br><br>
<span style='font-size:18px;font-weight:550px; '>sdfsd</span><br><input type='radio' name='1' value='gfgf'>gfgf<br><br>
<input type='radio' name='2' value='gfg'>gfg<br><br>
<input type='radio' name='2' value='gfg'>gfg<br><br>
<input type='radio' name='2' value='gf'>gf<br><br>
You can use a Heredoc string to help you with this. I made a simple example that will output what you want in a simple way. Take a look:
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$i = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$qech1=$rows['optiona'];
$qech2=$rows['optionb'];
$qech3=$rows['optionc'];
$qech4=$rows['optiond'];
$correct=$rows['correct'];
echo <<<HTML
<input type='radio' name='$i' value='$qech1'>$qech1<br><br>
<input type='radio' name='$i' value='$qech2'>$qech2<br><br>
<input type='radio' name='$i' value='$qech3'>$qech3<br><br>
<input type='radio' name='$i' value='$qech4'>$qech4<br><br>
HTML;
$i++;
}
The reason it isn't incrementing is because you are calling $i right after incrementing it.
while ... {
$i++
...
..name="$i"...
}
Right now, it goes through the loop once and prints all names as 2. The next time all of them would be 3, etc.
What you want is more of a foreach loop.
I assigned basic values just for my testing purposes. I personally would eliminate assigning $quech1 = $rows[optiona]. You can just work with the array of $rows with the foreach loop.
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
while($rows=mysql_fetch_assoc($ques2))
{
$rows['optiona'] = "optiona";
$rows['optionb'] = "optionb";
$rows['optionc'] = "optionc";
$rows['optiond'] = "optiond";
$correct=$rows['correct'];
}
$i = 0;
foreach ($rows as $row){
$i++;?>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $row; ?>'><?php echo $row."<br>"; ?>
<?php } ?>
You'll see from my inspect element, that you are now getting incremented names:
However, if I'm not mistaken. Don't you generally want all of the names to be the same for a group of radio buttons? You'll want unique ID's. And then have all buttons that are to be grouped together with the same name. This makes it so only one button in that group can be selected. If that is what you're trying to accomplish, use the revised code:
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$name = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$rows['optiona'] = "optiona";
$rows['optionb'] = "optionb";
$rows['optionc'] = "optionc";
$rows['optiond'] = "optiond";
$name++;
}
$i = 0;
foreach ($rows as $row){
$i++;?>
<input type='radio' id='<?php echo $i; ?>' name='<?php echo $name; ?>'value='<?php echo $row; ?>'><?php echo $row."<br>"; ?>
<?php } ?>
Only change is that $i is being assigned to id now. And we'll declare a $name counter as well. We'll increment that in the while loop, so that it will only be incremented once for each set of button options. And then you use $name in the name attribute for the button.
That's what makes sense to me. But without more information, we don't really know exactly what you're trying to accomplish. :)
Related
I'm developing php page for "Online Test"I've maintained a database for question-answers and the options of the same. What I'm unable to do is assigning different names to a group of radio buttons for the options.Here's the code :
<?php
$sql="SELECT ques,ques_ans.q_no,A,B,C,D from ques_ans JOIN options ON ques_ans.q_no=options.q_no";
$result=mysqli_query($db,$sql);
$numrows=mysqli_num_rows($result);
$radiogrp=array();
for($i=0;$i<$numrows;$i++){
$radiogrp[$i]="q".$i;
}
for($i=0;$i<$numrows;$i++){
echo $radiogrp[$i];
}
foreach($radiogrp as $rg){
while($myrow=mysqli_fetch_array($result)){
echo $myrow["q_no"]." ";
echo $myrow["ques"]."<br><br>";
?>
<html>
<body>
<input type='radio' name='$rg' value='Option A'/><?php echo $myrow["A"]; ?><br>
<input type='radio' name='$rg' value='Option B'/><?php echo $myrow["B"]; ?><br>
<input type='radio' name='$rg' value='Option C'/><?php echo $myrow["C"]; ?><br>
<input type='radio' name='$rg' value='Option D'/><?php echo $myrow["D"]; ?><br><br><br>
</body>
</html>
<?php
}
}
?>
I want to change the radio button group name as the new question is retrieved from the database.
You can do the same as you did here <?php echo $myrow["A"]; ?>
See the following:
name=' <?php echo $value; ?>'
If this is not what you're looking for please describe your problem better.
It got solved!Just moved the radio buttons in php tags and added one more condition in the while loop and changed the value of name attribute to array-name with the subscript.
$result=mysqli_query($db,$sql);
$numrows=mysqli_num_rows($result);
$radiogrp_name=array();
for($i=0;$i<$numrows;$i++){
$radiogrp_name[$i]="q".$i;
}
$i=0;
while(($myrow=mysqli_fetch_array($result)) && ($i<$numrows)){
echo $myrow["q_no"].". ";
echo $myrow["ques"]."<br><br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='Option A'/>".$myrow["A"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='Option B'/>".$myrow["B"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='Option C'/>".$myrow["C"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='Option D'/>".$myrow["D"]."<br><br><br>";
$i++;
}
overview:
1 What this is about
2 What questions i have
3 What is bugging
1. What this is about
this is about programming a little number game. i think it is also known as mastermind. the game has the following rules.
there are two players
first the starting player types 5 numbers
then the second player types 5 numbers to guess the numbers from the
first player
the programm has to echo how many numbers of the second player are at
the right place and also how many numbers are correctly guessed.
the programm has to run a fiew times, because player two has several
tries
i want to use one formular for both players if possible aka one
submit button
i don't want to know how this programm is coded as a whole but i have specific questions for some steps.
2. What questions i have
As you see i did player two as well. But i still have the problem that i cannot count how often player two typed the numbers. I have to count it because i want the game to quit if a certain number is reached.
Also i want to clear the screen for player two without clearing his formular, too. but the formular for player one should vanish after player ones submit.
3. Bugs/Debugging
i tried to use a loop but i couldn't figure out how i can use it without creating several formulars at once. it just should count + 1 for every player two submit of numbers.
<html>
<head>
<title>guess a number</title>
</head>
<body>
<h4> guess a number</h4>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>"method="post">
<!-- player one is starting here -->
<p>Spieler 1</p>
<p>chose your five numbers/Wähle deine 5 Zahlen</p><br />
<input type="password" name="one" size="1" maxlength="1">
<input type="password" name="two" size="1" maxlength="1">
<input type="password" name="three" size="1" maxlength="1">
<input type="password" name="four" size="1" maxlength="1">
<input type="password" name="five" size="1" maxlength="1">
<input type="submit" name="gesendet" value="ok"></button> <br />
</form>
<?php
session_start();
if(isset($_POST['gesendet'])){
$one = $_POST['one'];
$two = $_POST['two'];
$three = $_POST['three'];
$four = $_POST['four'];
$five = $_POST['five'];
// array to safe the input of player one with sessions
$_SESSION['anumberone'][0] = $one;
$_SESSION['anumberone'][1] = $two;
$_SESSION['anumberone'][2] = $three;
$_SESSION['anumberone'][3] = $four;
$_SESSION['anumberone'][4] = $five;
foreach ($_SESSION['anumberone'] as $ausgabe) {
echo "$ausgabe";
}
}
$i = 0; // how can i count the second submits?
while ( $i < 5 ) {
$i = $i + 1;
echo "$i";
//start with player two here!
echo "<br>";
echo "Spieler 2";
echo "<form method='post'>";
echo "Ihre Ziffern:<br>";
echo "<input type='text' name='sechs' size='1' maxlength='1'>";
echo "<input type='text' name='sieben' size='1' maxlength='1'>";
echo "<input type='text' name='acht' size='1' maxlength='1'>";
echo "<input type='text' name='neun' size='1' maxlength='1'>";
echo "<input type='text' name='zehn' size='1' maxlength='1'>";
echo "<input type='submit' name='submitzwei' value='OK'>";
echo "</form>";
if(!empty($_POST['submitzwei'])){
$sechs = $_POST['sechs'];
$sieben = $_POST['sieben'];
$acht = $_POST['acht'];
$neun = $_POST['neun'];
$zehn = $_POST['zehn'];
$_SESSION['anumber2'][0] = $sechs;
$_SESSION['anumber2'][1] = $sieben;
$_SESSION['anumber2'][2] = $acht;
$_SESSION['anumber2'][3] = $neun;
$_SESSION['anumber2'][4] = $zehn;
foreach ($_SESSION['anumber2'] as $ausgabe) {
echo "$ausgabe";
}
}
}
?>
</body>
</html>
In order to do that you need to use session which keeps the data until you end the session, follow the below altered code:
<?php
session_start();
$eins = $_POST['eins'];
$zwei = $_POST['zwei'];
$drei = $_POST['drei'];
$vier = $_POST['vier'];
$fuenf = $_POST['fuenf'];
$_SESSION['inputeins'][0] = $eins;
$_SESSION['inputeins'][1] = $zwei;
$_SESSION['inputeins'][2] = $drei;
$_SESSION['inputeins'][3] = $vier;
$_SESSION['inputeins'][4] = $fuenf;
foreach ($_SESSION['inputeins'] as $ausgabe) {
echo "$ausgabe<br>";
}
/* echo "<form>";
echo "<form action='ratespiel.php' method='post'>";
echo "<input type='submit' value='ok' name='verstecken'>";
echo "</form>";
if (isset($_POST['verstecken'])){
$ausgabe = "";
echo "$ausgabe";
} */
/* echo $input[0] . $input[1] . $input[2] . $input[3] . $input[4]; */
echo "<form method='post'>";
echo "Ihre Ziffern:<br>";
echo "<input type='text' name='sechs' size='1' maxlength='1'>";
echo "<input type='text' name='sieben' size='1' maxlength='1'>";
echo "<input type='text' name='acht' size='1' maxlength='1'>";
echo "<input type='text' name='neun' size='1' maxlength='1'>";
echo "<input type='text' name='zehn' size='1' maxlength='1'>";
echo "<input type='submit' name='submitzwei' value='OK'>";
echo "</form>";
if(!empty($_POST['submitzwei'])){
$sechs = $_POST['sechs'];
$sieben = $_POST['sieben'];
$acht = $_POST['acht'];
$neun = $_POST['neun'];
$zehn = $_POST['zehn'];
$_SESSION['inputzwei'][0] = $sechs;
$_SESSION['inputzwei'][1] = $sieben;
$_SESSION['inputzwei'][2] = $acht;
$_SESSION['inputzwei'][3] = $neun;
$_SESSION['inputzwei'][4] = $zehn;
foreach ($_SESSION['inputzwei'] as $ausgabe) {
echo "$ausgabe<br>";
}
}
?>
I'm trying to create a radio button in a loop, but I want to assign a unique id so it becomes a group where only one radio can be selected. Is this possible? My loop duplicates the same radio and I can select every button. I only want to select one.
<?php
$i = 0;
$i = $i++;
while($i>=0)
{
echo "<input type='radio' name='test[$i++]' value='test[$i++]'>test[$i++] ";
echo "<BR>";
$i++;
}
?>
UPDATE THIS WORKS!!!
<?php
$i = 0;
while($i++ < 5)
{
echo "<input type='radio' name='test' value='test[$i]'>test[$i] ";
}
?>
Learn basic PHP syntax. "$i++" is a string that contains a variable ($i), and two + characters. it's NOT going to increment your $i variable.
You're literally generating the following html:
<input type='radio' name='test[1++]' value='test[1++]'>test[1++]
<input type='radio' name='test[1++]' value='test[1++]'>test[1++]
<input type='radio' name='test[1++]' value='test[1++]'>test[1++]
As well, as written, your while() is going to be an infinite loop, since $i will ALWAYS be greater than 0.
Try this instead:
while($i++ < $limit) {
echo "<input type='radio' name='test[$i]' value='test[$i]'>test[$i] ";
}
you can use of bottom code.please preuse input tag(value).
`<?php for($i=1;$i<10;$i++) { `?>`
<input name="RadioBox" type="radio" value="<?php print($i); ?>" />
<?php } ?>
I am trying to get the response of a survey, all question answers are displayed in a while loop and in action PHP page I am not getting proper responses, I'm getting the right response for the radio button only, I'm attaching the code.
<?php
$i = 1;
while ($row = mysqli_fetch_array($questions)) {
?>
<div class="control-group">
<label class="control-label" for="focusedInput">(<?php echo $i; ?>)
<?php
$questionid = $row['question_id'];
$question = $row['question'];
?>
<input type="hidden" name="questionid" value="<?php echo $questionid; ?>" />
<input type="hidden" name="question" value="<?php echo $question; ?>" />
<?php echo $row['question']; ?></label>
<div class="controls">
<?php
if ($row['answer_type'] == "Ratings") {
echo "<p>
Low<input type='radio' name='rating$i' value='1' id='rating_0'>
<input type='radio' name='rating$i' value='2' id='rating_1'>
<input type='radio' name='rating$i' value='3' id='rating_2'>
<input type='radio' name='rating$i' value='4' id='rating_3'>
<input type='radio' name='rating$i' value='5' id='rating_4'>High
</p>";
} else if ($row['answer_type'] == "Comments") {
echo "<textarea name='answer' cols='' rows=''></textarea>";
}
$i++;
echo "<br />";
?>
</div>
</div>
<?php } ?>
Action file code:
foreach($_POST as $val){
$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) values (1,$_SESSION[surveyid],$_POST[questionid],'$_POST[question]',$val,'$_POST[answer]')";
$result2 = mysqli_query($con,$query2);
if(!$result2) {
echo mysqli_error($result2);
}
}
I want to insert the survey answers in the MySQL table including the fields displayed in the output picture.
Your form tags are missing from the exposed code, so there might be some extra fields available that are important for the scenario.
There is no obvious reason for using foreach on $_POST. You should explain further why you're cycling it.
Here is some code for your action file, that might work for you:
/* create a prepared statement */
$stmt = $mysqli->stmt_init();
if ($stmt->prepare("INSERT INTO review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) VALUES(1,?,?,?,?,?)")) {
/* bind parameters for markers */
$stmt->bind_param("sssss", $_SESSION['surveyid'], $_POST['questionid'], $_POST['question'], $val, $_POST['answer']);
/* execute query */
$stmt->execute();
/* close statement */
$stmt->close();
}
I have set the below in my HTML form. How do I retrieve it back in my PHP page(ON Submit)?
foreach ($rows as $i => $value) {
echo "<br/>What do you think about : ";
echo "<li>$value<br/>";
echo "1<input type='radio' value='1' name='answer-$i' /> ";
echo "2<input type='radio' value='2' name='answer-$i' /> ";
echo "3<input type='radio' value='3' name='answer-$i' /> ";
echo "4<input type='radio' value='4' name='answer-$i' /> ";
echo "5<input type='radio' value='5' name='answer-$i' /> ";
}
?>
How do I retrieve my answer back ?
I use POST method for my form. Help me!!
Adding Details.
<?php
echo "hello";
$answer = _POST("answer");
echo $answer-0;
echo $answer[0];
foreach ($answer as $i => $value) {
echo $value;
echo $i;
}
?>
answer-0=3&answer-1=1
Above Values shows that The parameters are set(Get Method). But In POST METHOD I am not able to access them.
Can Someone help me? I still havent resolved the issue.
<form method="post">
<?php
foreach ($_POST as $i => $value) {
echo "$i $value<br/>";
}
echo $_POST['answer-1'];
echo "<br>";
echo $_POST['answer-2'];
$rows = array(
"1" => "bar",
"2" => "foo",
);
foreach ($rows as $i => $value) {
echo "<br/>What do you think about : ";
echo "<li>$value<br/>";
echo "1<input type='radio' value='1' name='answer-$i' /> ";
echo "2<input type='radio' value='2' name='answer-$i' /> ";
echo "3<input type='radio' value='3' name='answer-$i' /> ";
echo "4<input type='radio' value='4' name='answer-$i' /> ";
echo "5<input type='radio' value='5' name='answer-$i' /> ";
}
?>
<input type="submit"/>
</form>
Looks more like, what you need.
So you need to use $_POST array with square brackets (it's an array). This code will build an array of $answers for you, assuming you still have $rows available.
$answers = array();
foreach ($rows as $i => $value) {
$answers[$i] = $_POST["answer-$i"];
}
print_r($answers);