I have two tables named bookings and packages. Checked checkboxes are retrieved from bookings table and are working perfectly. But I also need unchecked check boxes from packages table. The problem is checked checkbox values display again.
<?php
$get_pro = "select * from booking";
$result = mysqli_query($con, $get_pro);
$book_packages = $row_pro['Book_Packages'];
$book_packages = substr(trim($book_packages) , 0, -1);
$split_pkg = (explode(",", $book_packages));
if (!empty($split_pkg)) {
foreach ($split_pkg as $book_pkg) {
$checked = (in_array($book_pkg, $split_pkg)) ? 'checked="checked"' : '';
?>
<input type="checkbox" name="pkg_list_name[]" value="<?php echo $book_pkg; ?>" size="17" <?php echo $checked; ?>> <?php echo $book_pkg; ?> <br>
<?php
}
}
$get_pros = "select * from package";
$results = mysqli_query($con, $get_pros);
while ($row_pros = mysqli_fetch_array($results)) {
$package_name = $row_pros['Pkg_Name'];
if ($package_name != $split_pkg) {
echo "<input type='checkbox' data-parsley-required='true' value='$package_name' data-parsley-trigger='click' id='checkbox1' name='pkg_list_name[]'>
<label>$package_name</label><br> ";
} else {
}
}
?>
You shouldn't have two loops. You should only create the checkboxes from the package table. When you're creating those checkboxes, add checked="checked" if the package name is in $split_pkg.
$book_packages = trim($row_pro['Book_Packages']);
$split_pkg = array_map('trim', explode(',', $book_packages));
$get_pros = "select * from package";
$results = mysqli_query($con, $get_pros);
while ($row_pros = mysqli_fetch_array($results)) {
$package_name = $row_pros['Pkg_Name'];
$checked = (in_array($package_name, $split_pkg)) ? 'checked="checked"' : '';
echo "<label><input type='checkbox' data-parsley-required='true' value='$package_name' data-parsley-trigger='click' name='pkg_list_name[]' $checked>
$package_name</label><br> ";
}
BTW, it's not a good idea to to put comma-separated values in database columns. You should have a separate row for each booking. Then you would be able to join the two tables easily.
Related
I get an error like this;
Notice: Undefined offset: 0 on line 93
My php code is look like this below code..
$i=0;
while ($ww=mysqli_fetch_array($query))
{
if ($i%2==0)
$class="evenRow";
else
$class="oddRow";
$id=$ww[0];
$studentid=$ww[1];
$name=$ww[2];
$kelompok=$ww[8];
$block=$ww[9];
$level=$ww[10];
$house=$ww[11];
$status=$ww[14];
echo "<tr>
<input type=hidden name=applyid[] value=".$id."/>
<td>$studentid</td>
<td>$name</td>
<td>$kelompok</td>
<td>$block</a></td>
<td>$level</td>
<td>$house</td>
<td>
<input type=checkbox name=status approved checked> APPROVED <br>
</td>
</tr>";
}
$i++;
echo "</table>";
This is the error on line 93: $checkbox[] .= $_POST['applyid'][$i];}
And the SQL Query to update the status is look like this...
<?php
include("connection.php");
$checkbox = array();
if(isset($_POST['applyid']))
{
$check = count($_POST['applyid']);
for($i=0;$i<$check;$i++){
$checkbox[] .= $_POST['applyid'][$i];}
$check = "('" . implode( "','", $checkbox ) . "');" ;
$sql="UPDATE application SET apply_status = 'APPROVED' WHERE apply_id IN $check" ;
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
}
?>
I want to update multiple row selected by checkbox. This is the table output
Click Here
..............................................................................
VIEW THE PENDING STATUS:
This is my code if only the apply_status = 'PENDING' will only view.
I add the if else statement... but is not working. if there is several apply_status = approved. It will not showed the pending one. But if there is no apply_status = aprroved. It will view all the application.
<?php
include("connection.php");
$sql="SELECT * FROM application";
$record = mysqli_query($con, $sql) or die ("error".mysqli_error($con));
$apply = mysqli_fetch_assoc($record);
$status1 = $apply["apply_status"];
if ($status1 == "APPROVED") {
echo "<br>";
echo "No application from student yet.<br>";
echo "<br>";
} else {
echo "<table border='1'><tr>
<td><strong>Student ID</strong></td>
<td><strong>Student Name</strong></td>
<td><strong>Kelompok</strong></td>
<td><strong>Block</strong></td>
<td><strong>Level</strong></td>
<td><strong>House</strong></td>
<td><strong>Status</strong></td>
</tr>";
$i=0;
while ($ww=mysqli_fetch_array($query))
{
if ($i%2==0)
$class="evenRow";
else
$class="oddRow";
$id=$ww[0];
$studentid=$ww[1];
$name=$ww[2];
$kelompok=$ww[8];
$block=$ww[9];
$level=$ww[10];
$house=$ww[11];
$status=$ww[14];
echo '<tr>
<input type="hidden" name="applyid['.$i.']" value="'.$id.'"/>
<td>'.$studentid.'</td>
<td>'.$name.'</td>
<td>'.$kelompok.'</td>
<td>'.$block.'</a></td>
<td>'.$level.'</td>
<td>'.$house.'</td>
<td>
<input type="checkbox" name="status['.$i.']" value="approved" checked> APPROVED <br>
</td>
</tr>';
$i++;
}
echo '</table>';
}
?>
Try changing :
$check = count($_POST['applyid']);
for($i=0;$i<$check;$i++){
$checkbox[] .= $_POST['applyid'][$i];
}
TO
foreach($_POST['applyid'] as $index=>$idValue){
$checkbox[] .= $idValue;
}
EDIT :
use the index of the loop to index the input's so that you can associate them with each other in the receiving page :
$i = 0; // $i used to determain if it is odd or even, also used as the index in the html inputs
// comments are your friend
while ($ww=mysqli_fetch_array($query))
{
if ($i%2==0){ // best practice for readable code is to use the braces
$class="evenRow";
}
else{
$class="oddRow";
}
// easier to read when spaced equally
$id = $ww[0];
$studentid = $ww[1];
$name = $ww[2];
$kelompok = $ww[8];
$block = $ww[9];
$level = $ww[10];
$house = $ww[11];
$status = $ww[14];
// single quotes are faster to proccess in PHP
// use $i to force the array index
// place quotation marks arrount html attribute values
echo '<tr>
<input type="hidden" name="applyid['.$i.']" value="'.$id.'"/>
<td>'.$studentid.'</td>
<td>'.$name.'</td>
<td>'.$kelompok.'</td>
<td>'.$block.'</td> <!-- removed a closing "a" tag, as it wasn\'t closeing anything -->
<td>'.$level.'</td>
<td>'.$house.'</td>
<td>
<input type="checkbox" name="status['.$i.']" value="approved" checked> APPROVED <br>
</td>
</tr>';
$i++; // increment $i inside the loop, else it will never change until the loop is completed
}
// single quotes are faster to proccess in PHP
echo '</table>';
On the recieving page, use this,
include("connection.php");
if(isset($_POST['applyid']))
{
$allIDs = ''; // using this in the single SQL query, remove it if you not going to use it
$approvedIds = ''; // we will add all the approved Ids here for using in the SQL query
$unapprovedIds = ''; // we will add all the un-approved Ids here for using in the SQL query
// if the single SQL query works, remove the $unapprovedIds
foreach($_POST['applyid'] as $index=>$idValue){
if(isset($_POST['status'][$index])){ // if the status for this ID was posted, it was selected ( ony selected checkboxes get posted )
$approvedIds .= ($approvedIds === '' ? '' : ', ').$idValue; // we add it to the string that will be used in the "IN"
// if $approvedIds is not blank, add a comma to format corrctly for SQL
}else{ // if the single SQL query works, remove this entire else
$unapprovedIds .= ($unapprovedIds === '' ? '' : ', ').$idValue; // we add it to the string that will be used in the "IN"
// if $unapprovedIds is not blank, add a comma to format corrctly for SQL
}
$allIDs .= ($allIDs === '' ? '' : ', ').$idValue; // using this in the single SQL query, remove it if you not going to use it
}
// update all the approved ones
// single quotes
// format you SQL in a easy to read way
$sql = 'UPDATE application
SET apply_status = \'APPROVED\'
WHERE apply_id IN ('.$approvedIds.')' ;
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
// update all the unaproved ones
// format you SQL in a easy to read way
$sql = 'UPDATE application
SET apply_status = \'UNAPROVED\'
WHERE apply_id IN ('.$unapprovedIds.')' ;
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
/////////////////
//
// worth a try as a single query UNTESTED :
//
//////////////////////////////////////////////
$sql='UPDATE application
SET apply_status =
CASE WHEN apply_id IN ('.$approvedIds.') THEN \'APPROVED\'
ELSE \'UNAPPROVED\'
END
WHERE apply_id IN ('.$allIDs.')';
}
I have added some basic guidelines for you so that you can apply them going forward
I'm creating a simple survey and I want the values in the column to be just (1) one row and separated with "|". But the only problem is, how do I save those values that I've got from the $_POST into an array on my database? For example:
Male|Employed|Married| instead of this...
<?php
$cnt = 1; // VARIABLE FOR QUESTIONS //
$query = "SELECT * FROM questions";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_object($result))
{
$choices = explode ("|", $row->choices);
if(isset($_POST['submit_post']))
{ // eq - radio button name (choices) for $cnt - question # //
$choice = $_POST["eq$cnt"]; // What to do in this $_POST part? //
$query = "INSERT INTO users(answers)";
$query .= "VALUES ('{$choice}')";
$create_post_query = mysqli_query($connection, $query);
}
?>
<form action="" method="post">
<?php
if($row->num ==0)
{
$num=" ";
} else
{
$num=$row->num.". ";
}
?>
<p id="eqIdentify_<?php echo $cnt ?>"><strong><?php echo $num; echo $row->questions; ?></strong> <?php ?> <?php echo $cnt; ?>
<?php
// VARIABLE FOR CHOICES //
for($a=0;$a<count($choices);$a++)
{
?>
<br/><label><input type="radio" name="eq<?php echo $cnt;?>" value="<?php echo $choices[$a] ?>" /><?php echo $choices[$a]?></label>
<?php
}
?>
<?php
$cnt = $cnt + 1;
}
?>
<input type="submit" name="submit_post">
</form>
change your table structure so you have a field for each of the questions.
like
ID Question1 Question2
and then change your insert query such
INSERT INTO `answers`( `question1`, `question2`, `question3`, `question4`) VALUES ([value-2],[value-3],[value-4],[value-5])
But please please look in to PDO and prepared query's
I've got a problem with inserting multiple row to one table.
I've got a 3 tables:
1. student with id_student
2. ankieta with id_ankieta
3. student_ankieta with id_student and id_ankieta
I want to choose students from database using select or checkbox and choose one id_ankieta. After confirming, there are rows created in table (student_ankieta).
Now I can choose students but when I confirm, only one student gets added to the database.
Can anyone help me corect the code?
<?php
echo'<form method="post" action="student_ankieta.php">
<div class="box" style="margin:0 auto; top:0px;">
<h1>Student - ankieta:</h1>
<label>
<span><br/>Ankieta:</span>
</label>
<select class="wpis" name="id_ankieta">
<option>wybierz ankiete</option>';
$query = "SELECT * FROM ankieta";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo '<option value="'.$row['id_ankieta'].'">' . $row{'rok_akademicki'}.' '. $row{'semestr_akademicki'}.' '.$row{'active_ankieta'} .'</option>';
}
echo '
</select>';
$query = "SELECT * FROM student";
$result = mysql_query($query);
echo'
<label>
<span><br/>Wybierz stundentów:</span>
</label>
<select multiple="multiple" name="id_student[]" size="10">';
while ($row = mysql_fetch_assoc($result))
{
echo '<option class="wpis" value="'.$row['id_student'].'" />'.$row{'pesel'}.' '. $row{'nazwisko'}.' '.$row{'imie'} .'</option>';
}
echo'<br/><input class="button" type="submit" value="Dodaj ankiete" name="dodaja">';
if(isset($_POST['dodaja']))
{
$id_ankieta = $_POST['id_ankieta'];
if(empty($_POST['id_ankieta']))
{
echo '<p style="margin-top:10px; font-size:75%; font-family: Calibri; color: red; text-align:center;">Musisz wypełnić wszystkie pola.</p>';
}
else
{
$id_student = $_POST['id_student'];
for ($i = 0; $i < count($id_student); $i++)
{
$id_student = $id_student[$i];
mysql_query("INSERT INTO student_ankieta (id_student, id_ankieta) VALUES ('" . $id_student . "','$id_ankieta')");
}
}
}
echo'</div></form>';?>
Put all students in to an array with the key = id_student
$query = "SELECT * FROM ankieta";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
students[$row['id_student']] = array($row['pesel'],$row['nazwisko'],$row['imie'];
}
If the form was posted the confirm will = 1 (from hidden input)
When first enter script "confirm will = 0
When zero, display all student with a check box with a name which includes the id_student in the format of n-i_student.
if intval($_POST['confirm']) = 0){
echo '<form action = "confirm.php" method="post"><input type="hidden" name="confirm" value="1"/><table>';
foreach ($students as $id => val){
echo "<tr><td><input type=\"checkbox\" name=\"n-$id\" value=\"1\" /> Select </div></td>$val[0]<td>$val[0]</td><td>$val[1]</td><td>$val[2]</td></tr>";
}
echo '</table></form>';
}
When confirm = 1
The checkboxes that were checked are inserted.
Check each post value for a key the starts with "n-"
get the rest of the key value after the n- for the id_student value.
Still 1 Major Problem, I do not know where to get the $id_ankieta'
And match it with the id_student.
I left that value as $val[???]
elseif intval($_POST['confirm']) = 1){
foreach ($_POST as $k =>$val){
if (inval($val) == 1 && substr($k,0,2) == 'n-'){
$id = substr($k,2);
$sql = mysql_query("INSERT INTO student_ankieta (id_student, id_ankieta) VALUES ('$id','" . $students[$id][$val[???]] . "')");
}
}
}
I am having problem adding up the values in a column with php.
These values where sent from checkboxes and i want to count only the values that where checked from the unit column.
Here is my code:
<?php
$id = $_POST['course'];
foreach($id as $value)
{
//echo $value;
$query = " SELECT * FROM french WHERE id= $value ";
$result = mysql_query($query) or die('Error, query failed');
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$course = htmlspecialchars($row['course_name']);
$code = htmlspecialchars($row['course_code']);
$unit = $row['unit'];
$status = $row['status'];
?>
Are you trying like this? Getting form array of form checkboxs and checking on database via ID's?
HTML FORM
Check 1 <input type="checkbox" name="val[]" />
Check 2 <input type="checkbox" name="val[]" />
PHP RESULT
$val = $_POST['val'];
$count = count($val);
foreach ($val as $val_res)
{
$query = 'SELECT * FROM french WHERE id='.$val_res;
}
$query = mysql_query("SELECT DISTINCT status, vendor FROM tbl_softwareinstalled WHERE vendor NOT LIKE ''");
$nums = mysql_num_rows($query);
echo "<form name = 'filter' action='softwarefilter.php' method='POST'>
$nums<br>
<table border = 1><tr><td> </td>
<td><strong>Software Vendor</strong></td></tr>";
$ctr1 = 1;
while($fetch = mysql_fetch_array($query)) {
$vendor = $fetch['vendor'];
$status = $fetch['status'];
if(($ctr1 % 2)==1)
{ print "<tr bgcolor = 'white'>";}
else
{ print "<tr bgcolor = '#EEEEEE'>"; }
print "<td><input name= 'chk[]' type='hidden' value='0'>
<input type = 'checkbox' name = 'chk[]' value = '$vendor' ";
if ($status == 'Enabled') {
print "checked = 'checked'></td><td>$vendor</td></tr>"; }
else {
print "></td><td>$vendor</td></tr>"; }
$ctr1++;
}
print "</table>";
print "<input type = 'submit' name = 'submit' value = 'Update Filter'>
</form> ";
$submit = $_POST['submit'];
if(isset($submit))
{
$chk = $_POST['chk'];
$count = count($chk);
if (empty($chk)) {
echo "qweqwe<br>";
}
for ($i=0; $i<$count; $i++) {
$abc = $chk[$i];
$query = mysql_query("UPDATE tbl_softwareinstalled SET status = 'Enabled' WHERE vendor = '$abc'");
}
echo '<meta http-equiv="refresh" content="0.5;url=/assets/softwarefilter.php">
<script language="javascript">
alert("Software filter updated.");
</script>';
}
Hi, how can I count the UNCHECKED CHECKBOXES in a while loop with this block of code?
Let's say for example I have 10 checkboxes with 10 values, the user checked 3 checkboxes (with values: 'abc', 'def', 'ghi'), after submitting it. It will count all the UNCHECKED checkboxes and echoes the value of it. The values of 3 checked checkboxes SHOULD NOT be echoed.
The browser will only tell you which have been checked. Pull all the possible boxes from your database, and then remove those that the browser has told you are checked.
I had worked on a similar thing for counting unchecked boxes.
place two input fields of input type checkbox and input type hidden with different names
<input type="hidden" class="" name="test[]" value="0">
<input type="checkbox" class="" name="checkbox[]" value="1">
then on submit
if (isset($_POST['submit'])) {
$cnt = array();
$counter =0;
$testcnt = 0;
$cnt = 0;
if (isset($_POST['test'])) {
$testcnt = count($_POST['test']);
}
if (isset($_POST['checkbox'])) {
$cnt = count($_POST['checkbox']);
}
echo 'the count of unchecked boxes is ';
print_r($testcnt-$cnt);
}
For your case change this to:
<input name= 'unchecked[]' type='hidden' value='0'>
then on submit when your checking for isset(submit) you can add the following an d change accordingly
$unchecked_input = count($_POST['unchecked']);
$chk = $_POST['chk'];
$count = count($chk);
echo $unchecked_input-$count; //will output the count of unchecked boxes