Bootstrap Table select all and update cell value through modal - php

I currently have a Bootstrap Table displaying some data from a SQL DB with the ability to edit each row seperately with the options Consign, Edit and Delete. Consign simply changes the consign value in the DB from No to Yes.
It's more likely that multiple rows will be consigned at once so the aim is to be able to select mutiple using the built in checkboxes and then click the Consign button on the toolbar. This would then open a modal to confirm the action and then apply the changes to the DB - i.e. change all the selected rows consign value from No to Yes.
I've managed to get the button to only work when the checkboxes are selected and I've also created a modal which appears when clicking the button if it's active. There are a couple of hidden inputs in there for the DB update (a select option showing Yes and today's date).
I guess I'm stuck at the point of how do I get the IDs (called no here) of my rows over to allow the DB to be updated. I've tried to MacGyver a few suggestions on here together but nothing has worked so far, so any suggestions would be immensly appreciated.
This is the code I have so far.
HYML Table - records.php
<div class="datatable-dashv1-list custom-datatable-overright">
<div id="toolbar">
<button id="consigned" class="btn btn-warning btn-md" data-toggle="modal" data-target="#modal_form" disabled><i class="glyphicon glyphicon-check"></i> Consign Selected</button>
</div>
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Consign Multiple Entries</h4>
</div>
<form action="#" id="form" class="form-horizontal">
<div class="modal-body form">
<input type="hidden" value="" name="no"/>
<div class="form-body">
<div class="form-group">
<p class="text-center">Are you sure you want to consign the selected items?</p>
<div class="col-md-9">
<select class="form-control" name="consigned" style="visibility:hidden;" required readonly>
<option value="Yes">Yes</option>
</select>
</div>
</div>
<div class="form-group">
<!-- below list_check shows the IDs of the rows selected -->
<label class="control-label col-md-3">* This is supposed to be hidden value</label>
<div class="col-md-9">
<input name='list_check'>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
<button type="submit" id="btnConsign" class="btn btn-success"><span class="glyphicon glyphicon-check"></span> Consign</button>
</div>
</form>
</div>
</div>
</div>
<table id="table" data-toggle="table" data-id-field="no" data-select-item-name="no" data-sort-name="no" data-sort-order="asc" data-pagination="true" data-page-list="[10, 25, 50, 100, 200, All]" data-search="true" data-searchable="true" data-show-columns="true" data-show-pagination-switch="true" data-key-events="true" data-show-toggle="true" data-resizable="true" data-cookie="true"
data-cookie-id-table="saveId" data-show-columns-toggle-all="true" data-show-export="true" data-export-types="['xlsx', 'pdf']" data-export-options="{}" data-click-to-select="true" data-filter-control="true" data-filter-control-visible="true" data-filter-show-clear="true" data-show-refresh="false" data-maintain-meta-data="true" data-cookie="true" data-cookie-id-table="wasteId" data-cookie-expire="3h" data-toolbar="#toolbar">
<thead>
<tr>
<th data-field="selected" data-checkbox="true">Select</th>
<th data-field="no" data-sortable="true" data-sorter="dateSorter" data-visible="false">No</th>
<th data-field="location" data-sortable="true" data-filter-control="select" data-filter-control-placeholder="Select Location">Location</th>
<th data-field="name" data-visible="false">Name</th>
<th data-field="area" data-visible="false">Area</th>
<th data-field="sendto" data-visible="false">Send To</th>
<th data-field="reference">Reference</th>
<th data-field="consigned" data-filter-control="select" data-filter-default="No">Consigned</th>
<th data-field="date_consigned" data-visible="false">Date Consigned</th>
<th data-field="action" class="col-lg-2">Action</th>
</tr>
</thead>
<tbody>
<?php
include 'include/fetch-data.php';
?>
</tbody>
</table>
</div>
PHP fetch-data.php
<?php
include 'include/dbconnect.php';
$sql = "SELECT * FROM Sendlist";
$result = $conn->query( $sql );
if ( $result->num_rows > 0 ) {
while ( $row = $result->fetch_assoc() ) {
echo '<tr>';
echo '<td></td>';
echo '<td>' . $row[ "no" ] . '</td>';
echo '<td>' . $row[ "location" ] . '</td>';
echo '<td>' . $row[ "name" ] . '</td>';
echo '<td>' . $row[ "area" ] . '</td>';
echo '<td>' . $row[ "sendto" ] . '</td>';
echo '<td>' . $row[ "reference" ] . '</td>';
echo '<td>' . $row[ "consigned" ] . '</td>';
echo '<td>' . $row[ "date_consigned" ] . '</td>';
if ( $row[ 'consigned' ] == 'Yes' ) {
echo "<td>
<a href='#consign_" . $row[ 'no' ] . "' class='btn btn-warning btn-sm hide' data-toggle='modal' id='consign' name='consign'><span class='glyphicon glyphicon-check'></span> Consign</a>
<a href='#unconsign_" . $row[ 'no' ] . "' class='btn btn-warning btn-sm' data-toggle='modal' id='unconsign' name='unconsign'><span class='glyphicon glyphicon-check'></span> Unconsign</a>
<a href='#edit_" . $row[ 'no' ] . "' class='btn btn-success btn-sm' data-toggle='modal'><span class='glyphicon glyphicon-edit'></span> Edit</a>
<a href='#delete_" . $row[ 'no' ] . "' class='btn btn-danger btn-sm' data-toggle='modal'><span class='glyphicon glyphicon-trash'></span> Delete</a>
</td>";
}
echo "</tr>";
include( 'include/edit-delete-modal.php' );
echo "</tr>";
}
}
?>
<script>
var $table = $('#table')
var $consigned = $('#consigned')
$(function() {
$table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
$consigned.prop('disabled', !$table.bootstrapTable('getSelections').length)
})
$consigned.click(function () {
var ids = $.map($table.bootstrapTable('getSelections'), function (row) {
return row.id
})
$table.bootstrapTable('getSelections', {
field: 'no',
values: ids
})
$consigned.prop('disabled', true)
})
})
</script>

Just so others can find the answer if needed in the future, I used the following script to get the IDs of my selected rows to pass them through a hidden input in the modal which will then be later used (with explode) to update SQL entries.
<script type="text/javascript">
var $table = $('#table');
function getRowSelections() {
return $.map($table.bootstrapTable('getSelections'), function(row) {
return row;
})
}
$('#consigned').click(function() {
var selectedRows = getRowSelections();
var selectedItems = '\n';
$.each(selectedRows, function(index, value) {
selectedItems += value.no + ',';
});
$('#modal_form').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget); // Button that triggers the modal
var recipient = button.data('whatever'); // Extract info from data-* attributes
var modal = $(this);
modal.find(".modal-body input[name='list_check']").val(selectedItems);
});
});
</script>

Related

crud edit button modal variable is undefined

I am creating my first php app and running into an issue on how I should try and edit a sql entry. I was planning on trying to get a modal window to pop up in order to edit.
When I select the edit link to open the modal window the variable says it is undefined. I am thinking it is because the Modal and variable are added to DOM before I click the link to edit and the variable never gets set. How do I get around this?
<?php
$sql = "SELECT id, image, name, score, points FROM teams";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)){
$teams_id = $row["id"];
$teams_image = $row["image"];
$teams_name = $row["name"];
$teams_score = $row["score"];
$teams_points = $row["points"];
// echo "id: " . $teams_id . " image path: " . $teams_image . " name: " . $teams_name . " score " . $teams_score . " points " . $teams_points . "</br>";
echo "<div class='row'>";
echo "<div class='row oval'>";
echo "<div class='col-4 flag {$teams_image}'>";
echo "</div>";
echo "<div class='col-8 text'>";
echo "<p>{$teams_name}</p>";
echo "</div>";
echo "</div>";
// edit and delete button
echo "<a href='add_teams_working.php?edit={$teams_id}' class='btn btn-outline-light btn-sm ml-4 align-self-center' data-toggle='modal' data-target='#editModal'>edit</a>";
echo "<button class='btn btn-outline-light btn-sm ml-1 align-self-center'>delete</button>";
echo "</div>";
}
}else {
echo "No teams added yet";
}
?>
<!-- SET VARIABLE for MODAL -->
<?php
if(isset($_GET['edit'])) {
$edit_id = $_GET['edit'];
$query = "SELECT * FROM teams WHERE id = $edit_id ";
$select_team = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($select_team)) {
$edit_id = $row['id'];
$edit_image = $row['image'];
$edit_name = $row['name'];
}
}
?>
Here is modal if needed.
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h3>Edit <?php echo $edit_name; ?> </h3>
</div>
<div class="modal-body">
<?php echo $edit_name; ?>
<?php echo $edit_image; ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button class="btn btn-primary" type="button">Save Changes</button>
</div>
</div>
</div>
</div>

PHP Increment in For-each Works on Buttons Labels but not on Posted URL

I'm creating a web page that reads and displays files from a directory within the website, I'm using a for-each loop to iterate through the files and give the specific names based off the increment. When the user clicks the specific button I wan't my form to pass the specific increment so I know which file to use when it goes to the next page. The buttons display the increment as expected but the url always passes "1" as the value and I really dont know why, I attached a picture to show the buttons working but the link always becomes http://Mywebsite.com/AuthorizeCapabilityRequest.php?1
<?php
$images = glob($directory . "*.*");
echo "<div class='col-md-12' align='center'><div class='col-md-10'>
<table class='table table-bordered table-striped'>
<tr>
<th width='65%'>Filename</th>
<th width='20%'>Edit</th>
<th width='15%'>Refer</th>
</tr>";
$dir = "Capability_Request/*";
$zed = 1;
foreach(glob($dir) as $file)
{
$base = basename($file);
echo "<tr><td>";
if(!is_dir($file)) { echo basename($file);}
echo "</td><td><button type='button' class='btn btn-primary btn-
small' data-toggle='modal' data-target='#exampleModal$zed'>
$zed
</button> </td><td>
<!-- Button trigger modal -->
<button type='button' class='btn btn-primary' data-toggle='modal'
data-target='#exampleModalCenter'>
$zed
</button>
<!-- Modal -->
<div class='modal fade' id='exampleModalCenter' tabindex='-1'
role='dialog' aria-labelledby='exampleModalCenterTitle' aria-
hidden='true'>
<div class='modal-dialog modal-dialog-centered' role='document'>
<div class='modal-content'>
<div class='modal-header'>
<h5 class='modal-title' id='exampleModalLongTitle'>Refer
Capability to UNF</h5>
<button type='button' class='close' data-dismiss='modal'
aria-label='Close'>
<span aria-hidden='true'>×</span>
</button>
</div>
<div class='modal-body'>";
$url = "AuthorizeCapabilityRequest.php?".$zed;
echo "<form action='$url' method='post'>";
echo "<h6>If you would like to refer the capability to the
UNF, and accept the capability request, select this
button:</h6>";
echo "<input class='btn btn-success btn-block' type='submit'
value='Submit'>";
echo "<hr>";
echo "<h6>If you would like to refer the capability to the
UNF, but deny the capability request, select this
button:</h6>";
echo "<button type='button' class='btn btn-warning btn-
block'>Refer</button>";
echo "<hr>";
echo "<h6>If you would like to deny the capability referral
to the UNF, select this button:</h6>";
echo "<button type='button' class='btn btn-danger btn-
block'>Deny</button>";
echo "<div class='modal-footer'>";
echo "<button type='button' class='btn btn-secondary' data-
dismiss='modal'>Cancel</button>";
echo "</form>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</td></tr>";
$zed++;
}
echo "</table></div></div>";
ID's of elements have to be unique. Currently you give your 'modal div' the same ID for every iteration in
<div class='modal fade' id='exampleModalCenter' tabindex='-1'
role='dialog' aria-labelledby='exampleModalCenterTitle' aria-hidden='true'>
So what you should do are a few things: make sure the IDs are all unique (use your $zedvariable) and then have your data-target correspond to that unique ID (again with your $zed variable).
EDIT: here's an example of how this would work:
<button type="button" data-toggle="modal" data-target="#exampleModal<?php echo $zed; ?>">
your button
</button>
<div class="modal" id="exampleModal<?php echo $zed; ?>">
your modal
</div>

Code Igniter foreach loop function only returns one row

what I'm trying to do seems very easy but for the life of me, I can't figure out what I'm doing wrong.
What I want to achieve is to call a function which contains a loop with a return statement in another function in my model. For some reason beyond me, the loop function only returns the first row in the db table, even when there are more than 1.
Here is the loop function:
public function download_contents($id) {
$the_contents = $this->db->get_where('contents', array('request_id' => $id));
$count = 1;
foreach ($the_contents->result() as $y) {
$file_name = $y->file_name;
$download_path = base_url('assets/client/contents/'.$file_name);
return '<p>' .$count++. '. ' .$file_name. '
<a class="btn btn-primary btn-xs" href="' .$download_path. '" title="Download Content">Download</a></p>';
}
}
Now I want to echo the results of the function above in the function below:
public function modal_content_download($id) {
$y = $this->db->get_where('website_requests', array('id' => $id))->row();
$the_contents = $this->db->get_where('contents', array('request_id' => $id));
if ( $the_contents->num_rows() > 0 ) {
return '<button class="btn btn-primary btn-sm modal-toggle-btn" data-toggle="modal" data-target="#contents' .$id. '" title="Download Contents"> <i class="fa fa-download"></i></button>
<div class="modal fade" id="contents' .$id. '" role="dialog">
<div class="modal-dialog">
<div class="modal-content modal-width">
<div class="modal-header">
<div class="pull-right">
<button class="btn btn-danger btn-xs" data-dismiss="modal" title="Close dialog"> Close </button>
</div>
<h5 class="modal-title"> <i class="fa fa-download"></i> Download Contents: ' .$y->project_name. '</h5>
</div><!--/.modal-header-->
<div class="modal-body">'
.$this->download_contents($id). //calling the first function
'</div>
</div>
</div>
</div>';
} else {
return '<b style="color: red">No content uploaded yet.</b>';
}
}
The second function is passed to the controller and loaded to the view (with some other info). Now I know about code igniter's query logic and result() should return more than 1 row. What am I missing?
Try this
public function download_contents($id) {
$the_contents = $this->db->get_where('contents', array('request_id' => $id));
$count = 1;
$loop_result="";
foreach ($the_contents->result() as $y) {
$file_name = $y->file_name;
$download_path = base_url('assets/client/contents/'.$file_name);
$loop_result .= '<p>' .$count++. '. ' .$file_name. '
<a class="btn btn-primary btn-xs" href="' .$download_path. '" title="Download Content">Download</a></p>';
}
return $loop_result;
}

passing php variable in javascript function

I am new to php and javscript.I am working on some project of question and answer system.I want to send question id to javascript function.
here is my php code
<?php
if ($result->num_rows > 0)
{
$i = 1;
while($row = $result->fetch_assoc())
{
$p = $row["description"] ;
echo '<div onclick="myOverFunction(<?= $p ?>)" align="left" class="j" role="group" aria-label="..." > <button id = "btnl<?=$i?>" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';
echo $i;
echo '</button>';
echo '<p class="question" style ="display:inline; ">';
echo $p;
echo '</p>';
echo '</div>';
}
}
?>
My javacript function is:
<script>
function myOverFunction(x) {
document.getElementById("demo").innerHTML = x;
document.getElementById("btnl"+x).style.backgroundColor = "#d9534f";
}
</script>
my code does not working ..please help me ...
Your problem is:
echo '<div onclick="myOverFunction(<?= $p ?>)"
Change it to:
echo '<div onclick="myOverFunction('. $p .')".
Same goes for the other occurrences.
You can't echo from inside an echo
Your final echo should look like this:
echo '<div onclick="myOverFunction(\''. $p .'\')" align="left" class="j" role="group" aria-label="..." > <button id = "btnl'.$i.'" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';
Note: If $p is a string, it needs quotes. And quotes inside quotes need escaping
You are breaking this line by trying to re-open php tags within the echo:
echo '<div onclick="myOverFunction(<?= $p ?>)" align="left" class="j" role="group" aria-label="..." > <button id = "btnl<?=$i?>" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';
Try this:
echo '<div onclick="myOverFunction('.$p.')" align="left" class="j" role="group" aria-label="..." > <button id = "btnl'.$i.'" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';

Twitter Bootstrap - Passing a unique ID to a Modal

I have searched a lot and am still having some trouble passing a Unique Id to a Value and then using that ID for a PHP MySQL Query to get the book info.
I currently am passing the unique ID to the Modal and Displaying it through an input type text. I am just having a hard time figuring out how to put the id into a php Variable
Here is my HTMl/PHP code
echo '<a data-toggle="modal" data-id="' . $book_id .'" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">View Info</a>';?>
<!-- Modal -->
<div class="modal hide" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Book Info</h3>
</div>
<div class="modal-body">
<p>some content</p>
<?php
echo $test = '<input type="text" name="bookId" id="bookId"/>';
$book_info = get_book_info($book_id);
while($row1 = mysqli_fetch_array($book_info))
{
$email = $row['email'];
echo "<p><strong>Title: </strong>" . $row1['title'] . "</p>";
echo "<p><strong>Author: </strong>" . $row1['author'] . "</p>";
echo "<p><strong>Edition: </strong>" . $row1['edition'] . "</p>";
echo "<p><strong>ISBN: </strong>" . $row1['isbn'] . "</p>";
echo "<p><strong>Price: </strong>" . $row1['price'] . "</p>";
echo "<p><strong>Seller's Name: </strong>" . $row1['name'] . "</p>";
echo "<p><strong>Seller's E-mail: </strong><a href='mailto:$email'>$email</a></p>";
}?>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
here is my jquery
$(document).on("click", ".open-AddBookDialog", function () {
var myBookId = $(this).data('id');
$(".modal-body #bookId").val( myBookId );
});
You need to echo the value:
data-id="<?php $book_id ?>"
Use this instead:
data-id="<?php echo $book_id; ?>"
You could do something like this by javascript/coffee script.
($ "a[data-toggle=modal]").click ->
target = ($ #).attr('data-target')
url = ($ #).attr('href')
($ target).load(url)
URL = unique ID

Categories