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.
Related
I have this delete function in my system but first I need the server needs to know which table he has to delete that is why I am sending a data when the user click the delete button into the server which is the ID of the data I want to delete..first I need to try and get that data being sent by the form but the problem is sending data is not working in my part I tried to echo out the ID just to see if I have a result but it works fine but when I send it to the server it doesn't print anything.. Here is my code where I fetch the scheduleID and the form
if ($strand<>""){
$query1 = mysqli_query($conn,"SELECT * from schedule natural join instructor where day = 'm' and schedule.strand= '$strand' and timeID ='$id' and grade = '$grade' and semester = '$semester'");
}
$row1 = mysqli_fetch_array($query1);
$schedID = $row['scheduleID'];
$id = $row1 ['scheduleID'];
$count=mysqli_num_rows($query1);
if ($count==0)//checking
{
//echo "<td></td>";
}
else
{
//print
echo "<div class='show'>";
echo "<ul>
<li class='options' style='display:inline'>
<span style='float:left;'><a href='sched_edit.php?id=$id1' class='edit' title='Edit'>Edit</a></span>
<span class='action'><a href='#' id='$id1' class='delete' title='Delete'>Remove</a></span>
</li>";
echo "<form class = 'delete' method = 'post' action ='../functions/delete.php'>";
echo "<li class='showme'>";
echo " <input type='hidden' name='delete' value='$id'>";
echo "<button type='submit' name='delete' class='btn btn-danger'>Display Schedule</button>";
echo $row1['subject'];
echo "</li>";
echo "<li class='$displayc'>$row1[strand]";
echo "<li class='$displaym'>$row1[fname], $row1[lname]</li>";
echo "<li class='$displayr'>Room $row1[room]</li>";
echo "</form>";
echo "</ul>";
echo "</div>";
}
?>
</td>
I tried the hidden attribute in some of my forms and it work there but I don't know why it won't in this form, the $id is working also I tried echoing that inside in this page but the data sent is not printing in the server, here's my server
<?php
session_start();
include 'database.php';
if (isset($_POST['delete'])){
$ID = $_POST['delete'];
}
thanks in advance
The problem here is that you have 2 form elements using the same name attribute, so PHP is only keeping the last value, which is not defined.
See both the <input> and the <button> have the same name! The button is last and has no value; that is what is being used by PHP.
<input type='hidden' name='delete' value='$id'>
<button type='submit' name='delete' class='btn btn-danger'>Display Schedule</button>
So you can just remove the name from the button or change it to something other than delete :-)
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";
}
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;
?>
this my table code
$mem=mysql_query("SELECT * FROM tbl_user_profile WHERE comp_id= $cmp");
while($result=mysql_fetch_array($mem))
{
echo"<tr>" ;
echo"<td><font color='red' size=4> ".$result['fname']." </font></td>";
echo"<td><font color='red' size=4 >".$result['emp_id']."</font></td>";
inside this table in the third column i tried while loop to show radio buttons.
$query = "SELECT * FROM tbl_house where event_id = $eid";
$hid = mysql_query($query);
while ($row = mysql_fetch_array($hid))
{
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<td> . <input type=\"checkbox\" name=\"q1\" value=\"$house_id\" />$house_name </td>";
}
echo "<td> <a href='house_member.php?operation=dele&id=".$result['profile_id']."'><input type='button' value='add' id='btn'></a></td>";
echo"</tr>" ;
echo"</table>";
}
its not working properly. somebody please help. i'm just a beginner in php.
waiting for a better solution.
if you want them all to be in the same column, don't put <td> around each of them, do that outside the loop. And the type of the input should be radio, not checkbox.
echo "<td>";
while ($row = mysql_fetch_array($hid)) {
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<input type='radio' name='q1' value='$house_id'/>$house_name ";
}
echo "</td>";
please change the <Input type="checkbox"> to <input type "radio">
and
echo "<input type='radio' name='q1' value='$house_id'/>$house_name "; this is the right way to display html code into php
Are you trying to add radio buttons in the third column or do you want to create a column for each radio??
my guess is that you remove the from within the while loop and place the starting and ending tags outside the while loop. something like
<td>
while loop
</td>
hope this helps
You have to use the following code at the place of your while loop.....
<td>
while ($row = mysql_fetch_array($hid))
{
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<input type='radio' name='q1' value='$house_id' />$house_name ";
}
</td>
I have tried this code:
<?php
$sql="SELECT id, comment FROM comments
ORDER BY comments.id DESC";
$result = mysql_query($sql) or trigger_error ( mysql_error ( ));
while($row = mysql_fetch_array($result))
{
echo "<div>";
echo $row['comment'];
echo "</br>";
// Answer to comment
echo "<a id='href".$row['id']."' href='#'> Answer </a>";
echo "<div>";
echo "Your answer is: ";
echo "<span id='answerdone'";
echo "</span>";
echo "</div>";
}
?>
<script>
$('#href".$row['id']."').click(function() {
$(this).empty().next.append(<div> <input type='text' value='' name='answer'
id='".$row['id']."'
<input type='submit' value='Answer' />
</div>);
});
var input = document.getElementById('".$row['id']."'),
placeholder = document.getElementById('answerdone');
input.onsubmit = function() {
placeholder.innerHTML = input.value
}
</script>
In order to make a form appear in each result of a MySQL query and what is written in this form to be written on the same page, after "Your answer is: ". Unfortunately, this isn't working and I don't know why. In addition I want what is written after "Your answer is: " to be inserted as an additional column, in the comments table.
How do I do that and what is wrong?
You are mixing php and javascript variables.
For example:
echo "<a id='href".$row['id']."' href='#'> Answer </a>";
will appear as:
<a id='href1' href='#'> Answer </a>";
So you can't find this element with javascript:
$('#href".$row['id']."').click(function() {
It has to be something like
$('#href1').click(function() {
You should probably have a look at your javascript-console. There you get the right error-description.