Show next database values on button click - php

I have made a quiz, but instead of showing all of the questions at once I want to show them 1 by 1. So question 1 is shown first and when the answer is submitted question 2 is shown etc. The questions and multiple choice answers are shown on the page from the database with sql statements. But I can't seem to manage it to show the questions after each other.The way the databasebase is setup is, there is a question with 4 options and a correct answer column. I want to show the question and the 4 options that go with that question. This is the code I use to show all the questions with the answers from the database, it would be nice if someone could help me with a way to show them one by one:
<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "kinderboekenweek");
$sql = "SELECT * FROM questions";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$i =0;
while($row = $result->fetch_assoc()) {
echo " Vraag: ". $row["vraag"].
"<ul class = 'answers".$i."'.>
</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=".$row['optie1']." id='optie1".$i."'> </br>".
"</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie2"]." id='optie2".$i."'> </br> ".
"</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie3"]." id='optie3".$i."'> </br>".
"</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie4"]." id='optie4".$i."'> </br>".
"</br></br>
</ul>";
$i++;
}
} else {
echo "0 results";
}

Related

get php button value and use on different page

Objective: My main.php displays the courses in my class. Based on which one I click, It should show me all the projects for that specific course ( either a dropdown or for now I'm displaying them in displayProjects.php) So i need to get the id of the clicked php button, write a query to search that name with it's corresponding class_id in the student table, in order to get the project names. DO I need some sort of Ajax call to get the php onclick button value or maybe a session var to display in my displayprojects.php page?
Main.php
result = mysqli_query($link,"SELECT `course_code`FROM `class`");
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<a href='displayProjects.php'> <input type='submit' id='project' class='btn btn-primary' style='display:block; width:10%; margin-bottom:10px;' value=". $row['course_code'] . ">";
}
echo "<tr><td><a href='CreateNewClass.php'><button type='button'> Create a New Class </button></a></td></tr>";
echo "</table>";
displayProjects.php
Need help modifying this query to get projects based on class_id
$projectQuery=mysqli_query($link, "SELECT DISTINCT project_name from Student S ");
echo "<table>";
while($row = mysqli_fetch_assoc($projectQuery)) {
echo "<a href='displayGroups.php'> <input type='button' id='project' class='btn btn-primary' style='display:block; width:10%; margin-bottom:10px;' value=". $row['project_name'] . ">";
}
echo "</table>";
class table .
.
student table

php update button only update last record

this is my first PHP program and I'm kind of stuck in update button code I think the problem is with the Driver_ID I tried many different things but nothing works for me. my problem is how can I get the Driver_ID to make the button work?
in this code below I only get the last row id. If someone can help me I would really appreciate it.
<?php
include("dbconnection.php");
//Fetch the data from tables
$query="SELECT * FROM drivers JOIN users ON users.User_ID = drivers.User_ID";
$result = mysqli_query($connect,$query);
while($row = mysqli_fetch_array($result))
{
echo "<tr>
<td>".$row['Driver_ID']."</td>
<td>" . $row['Driver_Name'] . "</td>
<td>" . $row['User_Name'] . "</td>
<td><p data-placement='top' data-toggle='tooltip' title='Edit'><button class='btn btn-primary btn-xs' data-title='Edit' data-toggle='modal' data-target='#edit'value='".$row['Driver_ID']."' name='editr'><span class='glyphicon glyphicon-pencil'></span></button></p></td>
<td><p data-placement='top' data-toggle='tooltip' title='Delete'><button class='btn btn-danger btn-xs' data-title='Delete' data-toggle='modal' data-target='#delete'><span class='glyphicon glyphicon-trash'></span></button></p></td>
</tr>";
$id=$row['Driver_ID'];
}
mysqli_close($connect);
?>
The html code for the hidden control
<input class="form-control " type="hidden" value="<?php echo $id ?>" name="driverid" >
the code for the update button
<?php
require("dbconnection.php");
if(isset($_POST['update']))
// include Database connection file
{
$driver_id=$_POST['driverid'];
$driver_name = $_POST['txtname'];
$user_id = $_POST['UserN'];
$sql = "UPDATE drivers
SET Driver_Name='$driver_name',User_ID='$user_id'
WHERE Driver_ID='$driver_id'";
if (mysqli_query($connect, $sql)) {
echo "Record updated successfully";
header('Refresh:5 ; url=drivers.php');
}
else {
echo "Error updating record: " . mysqli_error($connect);
}
}
mysqli_close($connect);
?>
Use input like this:
As you need all results you need to store them all
your main while loop could be like
$r=null;
while(){
$r++;
echo '<input class="form-control " type="hidden" value="<?php echo $id ?>" name="driverid[]" >'
}
echo '<input type="hidden" value='".$r."' name="records">';
After that just use some loop to see all results
and then in update /insert
$records=htmlentities($_POST['records'], ENT_QUOTES, "UTF-8");
$i=0;
while ($i <= $records){
$driverid=htmlentities($_POST['driverid'][], ENT_QUOTES, "UTF-8");
//your update/insert query here
$i++;
}
i hope this helps

Send an array of variable instead of a single variable, over html button

I would like to send 2 variables on a click of a button
My Code so far is that:
HTML:
<?php
while ($Case=mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>".$Case['SubmissionID']."</td>";
echo "<td>".$Case['AppID']."</td>";
echo "<td>".getFullNameApp($Case['AppID'])."</td>";
echo "<td>".getCourseName($Case['SubmissionCourseID'])."</td>";
echo "<td>".getDepartmentName($Case['SubmissionCourseID'])."</td>";
echo "<td>".DateFormat($Case['Date'])."</td>";
echo "<td>".$Case['SubmissionStatus']."</td>";
if ($Case['SubmissionStatus'] == 'Draft') {
echo "<form action='ApplicantApplyDetails.php' method='POST'>";
echo "<td><button type='submit' class='btn btn-success btn-xs' name='modifybtn' value='".array($Case['SubmissionID'], $Case['SubmissionCourseID'])."'>Modify</button></td>";
}
else {
echo "<td><button type='submit' class='btn btn-danger btn-xs disabled' disabled='disabled' name='modifybtndis' value=''>Modify</button></td>";
}
echo "</form>";
echo "</tr>";
}
mysql_close();
?>
PHP:
if (isset($_POST['modifybtn'])) {
$SubmissionID =$_POST['modifybtn'][0];
$CourseID = $_POST['modifybtn'][1];
echo $CourseID;
}
And then get those variables for further processing. Is that possible? I've done single ones but no arrays. My code is not working. The problem I think is in the HTML part, but I have no idea how to fix it. Any hints?
To answer your question; You can convert an array to a string:
$value = json_encode([$Case['SubmissionID'], $Case['SubmissionCourseID']]);
echo "<td><button type='submit' class='btn btn-success btn-xs' name='modifybtn' value='".$value."'>Modify</button></td>";
And covert the string back to the array when reading.
var_dump(json_decode($_POST['modifybtn'], true));
However there is also the option to submit a hidden field:
<input type="hidden" name="courseid" value="<?= $Case['SubmissionCourseID']>">
And just use:
var_dump($_POST);

Show next database value when a button is clicked

I have this quiz which displays a question with 4 answers beneath. What I'm trying to do is show the next question when the first question is answered.
The questions and multiple choice answers are grabbed from the database and put inside buttons with a loop. I want the next question to show when any of the 4 buttons is clicked. I have tried to do this with questionId where question 1 has an id of 1 and question 2 an id of 2. The value of k goes to 2 but it doesn't show the question with id 2. If I manually change line 3 from $k=1; to $k=2; it shows the question with id 2. The end goal is to increment $k every time an option is clicked to show the next question.
<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "vragendb");
$k=1;
$sql = "SELECT * FROM vraag WHERE vraagId = '".$k."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$i =0;
while($row = $result->fetch_assoc()) {
echo " Vraag: ". $row["vraag"].
"<ul class = 'answers".$i."'.>
</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=".$row['optie1']." id='optie1".$i."'> </br>".
"</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie2"]." id='optie2".$i."'> </br> ".
"</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie3"]." id='optie3".$i."'> </br>".
"</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie4"]." id='optie4".$i."'> </br>".
"</br></br>
</ul>";
$i++;
$k++;
}
} else {
echo "0 results";
}
echo $k;
?>
Have you tried using $_GET parameters?
You can redirect the user to a URL for the next question, something like:
http://example.com/quiz.php?question=1
where question will be the $k in your case.
As demonstrated here, the user navigates to the ^above-mentioned URL and processes the stuff as follows:
<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "vragendb");
$k = $_GET['question']; //this gets the question depending on your parameter
$sql = "SELECT * FROM vraag WHERE vraagId = '".$k."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$i =0;
while($row = $result->fetch_assoc()) {
// we changed stuff here to accommodate the changes
echo " Vraag: ". $row["vraag"].
"<ul class = 'answers".$i."'.>
</br> <a href='http://example.com/quiz.php?question=".$k++."'> <input type='button' class='btn btn-default' name='Optie".$i."' value=".$row['optie1']." id='optie1".$i."'> </a> </br>".
"</br> <a href='http://example.com/quiz.php?question=".$k++."'> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie2"]." id='optie2".$i."'</a> </br> ".
"</br> <a href='http://example.com/quiz.php?question=".$k++."'> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie3"]." id='optie3".$i."'> </a> </br>".
"</br> <a href='http://example.com/quiz.php?question=".$k++."'> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie4"]." id='optie4".$i."'> </a> </br>".
"</br></br>
</ul>";
$i++;
$k++;
}
} else {
echo "0 results";
}
echo $k;
?>

Delete multiple rows if checkbox is checked

I want to build a feature that enables the user to moderate questions and is similar to wordpress comment or typical email moderation that uses a checkbox to approve/delete many questions at once.
How would I modify the following code to check which the questions are checked and react to the delete buttons when pressed? You may notice from the below code that I can currently do this with individual questions but I want to give the user more power.
<h2>Moderate Questions</h2>
<div>
<button type="button" class="btn" name="delete" id="delete">Delete</button>
<button type="button" class="btn" name="approve" id="approve">Approve</button>
<?php
// Select all unapproved questions in db
$sql = "SELECT question_id, question, DATE_FORMAT(question_date, '%e %b %Y at %H:%i') AS dateattime FROM questions WHERE question_approved='0' ORDER BY question_date DESC";
$result = mysql_query($sql);
$myquestions = mysql_fetch_array($result);
if($myquestions) {
do {
$question_id = $myquestions["question_id"];
$question = $myquestions["question"];
$dateattime = $myquestions["dateattime"];
echo "<div class='question'>";
echo "<span value='$question_id'>";
echo "<input name='checkbox' type='checkbox' id='checkbox' value='$question_id'>";
echo "$question - <span class='date'>Asked $dateattime </span>";
echo "</span>\n";
echo "<div class='question-controls'><a href='".$_SERVER["PHP_SELF"]."?delete=$question_id' title='Delete this question' class='delete' onclick='return confirm(\"Are you sure you want to delete this question?\")'>Delete</a> | <a href='#'>Edit</a> |";
echo " <a href='".$_SERVER["PHP_SELF"]."?approve=$question_id' title='Publish this question'>Approve</a></div>";
echo "</div>";
} while ($myquestions = mysql_fetch_array($result));
} else {
echo "<p>No one has asked a question recently.</p>";
}
?>
</div>
You need to modify 'checkbox' element in order to make an array of values. Code:
echo "<input name='checkbox[]' type='checkbox' id='checkbox' value='$question_id'>";
After that your can call this variable from php code(using $_REQUEST['checkbox'] for example) and it will be array. So your can iterate through it and delete these rows from database.

Categories