Pop-up modal on each element of a list - php

recently I,ve been trying to make a modal to pop-up on every element of a list. That list comes from a MySQL Database with this code:
<?php
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$cadenaSQL = "SELECT idPedido, fecha, fechaActual, nombre, apellido1, apellido2, telefono, email, productos, mensaje, aprobado
FROM pedido
WHERE borrado = 0
ORDER BY -fecha";
$limit = 10;
$page = validatepage($mysqli,$cadenaSQL,$limit,$page);
$startpoint = ($page * $limit) - $limit;
$res= $mysqli->query($cadenaSQL." LIMIT {$startpoint} , {$limit}");
?>
Most of the code above comes from a php class with functions related to the database, so they are called from there.
It shows on a list with the following html-php code, relating almost every field of the database, posting it first on a column with the showed width and then making every line with the values from every entry:
<table class="table table-hover">
<thead>
<tr>
<th scope="col" style="width:8%" class="num-pedido">NºPedido</th>
<th scope="col" style="width:10%" class="fecha">Fecha</th>
<th scope="col" style="width:15%" class="nombre-completo">Nombre</th>
<th scope="col" style="width:12%" class="telefono">Teléfono</th>
<th scope="col" style="width:15%" class="email">Email</th>
<th scope="col" style="width:20%" class="productos">Productos</th>
<th scope="col" style="width:10%" class="aprobado">Aceptado</th>
<th scope="col" style="width:10%" class="accion">Acción</th>
</tr>
</thead>
<tbody>
<?php
while($row=mysqli_fetch_array($res)){
$idPedido = $row['idPedido'];
$id = "P".str_pad($idPedido, 8, "0", STR_PAD_LEFT);
$fecha = $row['fecha'];
$fechaActual = $row['fechaActual'];;
$nombre = $row['nombre'];
$apellido1 = $row['apellido1'];
$apellido2 = $row['apellido2'];
$telefono = $row['telefono'];
$email = $row['email'];
$productos = $row['productos'];
$aprobado = $row['aprobado'].'';
$mensaje = $row['mensaje'];
$nombrecompleto = $nombre." ".$apellido1." ".$apellido2;
?>
<tr class="tabla-fila" <?php if($aprobado==1){echo "style='background:#afd5a2'";}?>>
<td data-label="Nº Pedido">
<a>
<div class="num-pedido" ><div id="myModalBtn"><?=$id?></div></div>
</a>
</td>
<td data-label="Fecha">
<div class="fecha"><?=$fecha?></div>
</td>
<td data-label="Nombre">
<div class="nombre-completo-pedido"><?=$nombrecompleto?></div>
</td>
<td data-label="Telefono">
<div class="telefono"><?=$telefono?></div>
</td>
<td data-label="Email">
<div class="email-pedido"><?=$email?></div>
</td>
<td data-label="Productos">
<div class="productos"><?=$productos?></div>
</td>
<td data-label="Aceptado">
<div class="aprobado"><?php if($aprobado==0){
echo "NO";
}else{
echo "SI";
}?></div>
</td>
<td data-label="Acción">
<div class="accion">
<span class="ti-check" id='<?=$idPedido?>' data-id="<?= $idPedido ?>"></span>
<span class="ti-trash" id='<?=$idPedido?>' data-id='<?= $idPedido ?>'></span>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
The problem comes when I try to call the model with "myModalBtn" (I will post the code a few lines later), as it only shows the last entry of my database. Here is the code:
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class=>
<button class="close">×</button>
</div>
<?php sectionbradcam('Detalles del pedido');?>
<section class="subcaja-central">
<div id = "central" class="container">
<div class="area-contenido">
<p><br>
Id Pedido: <?=$id?><br>
Fecha solicitada: <?=$fecha?><br>
Fecha de la solicitud: <?=$fechaActual?><br>
Nombre completo: <?=$nombrecompleto?><br>
Teléfono: <?=$telefono?><br>
Email: <?=$email?><br>
Productos: <?=$productos?><br>
Mensaje: <?=$mensaje?><br>
</p>
</div>
</div>
</section>
</div>
</div>
And also the json called to make the modal popup:
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myModalBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
How can I make it on every item of the list I mentioned above? And how would I point each lane of the list to it´s information on the database?
Thanks in advance to everyone who can help me.

Related

Get data from buttons populated in while loop

I have a while loop that loops through my drivers table and populates the buttons with the drivers names
function driverMenu()
{
global $conn;
$query = mysqli_query($conn, "SELECT * FROM driver");
while ($row = mysqli_fetch_assoc($query)) {
$id = $row['DriverID'];
$name = $row['driverName'];
echo "<div class='col text-center'>
<input type='submit' name='driverNameBtn' class='btn btn-primary rounded-pill text-light' value='{$name}'></input>
</div>";
}
}
I then call that function in another page that shows jobs associated to the drivers.
What I need to figure out now is how to change the data that is shown when they click on the different driver buttons.
This is my code that shows each job currently in the database but does not filter out the jobs by driver yet. But it does sort each job by days of the week.
<?php ini_set('error_reporting', E_ALL); ?>
<?php ini_set('display_errors', 1); ?>
<?php ini_set('display_startup_errors', 1); ?>
<?php include '../header.php' ?>
<!-- Page Title -->
<div class="container-sm text-dark px-3 p-4 truckList">
<div class="row m-auto align-items-center">
<?php include '../includes/functions.php'; driverMenu();?>
</div>
</div>
<?php
global $conn;
$monday = mysqli_query($conn, "SELECT *
FROM openjobs
INNER JOIN driver ON openjobs.driverName_fk = driver.driverName
WHERE weekday(jobDate) = 0");
$tuesday = mysqli_query($conn, "SELECT *
FROM openjobs
INNER JOIN driver ON openjobs.driver_fk = driver.DriverID
WHERE weekday(jobDate) = 1");
$wednesday = mysqli_query($conn, "SELECT *
FROM openjobs
INNER JOIN driver ON openjobs.driver_fk = driver.DriverID
WHERE weekday(jobDate) = 2");
$thursday = mysqli_query($conn, "SELECT *
FROM openjobs
INNER JOIN driver ON openjobs.driver_fk = driver.DriverID
WHERE weekday(jobDate) = 3");
$friday = mysqli_query($conn, "SELECT *
FROM openjobs
INNER JOIN driver ON openjobs.driver_fk = driver.DriverID
WHERE weekday(jobDate) = 4");
?>
<!-- Truck's Weekly Job List -->
<div class="container-fluid bg-secondary darkContainer">
<div class="container py-5 px-4 p-3 webWeeklyPlanTruckCard">
<div class="row gy-2">
<div class="col-12">
<!-- Monday -->
<div class="card mondayJobCard my-1">
<div class="card-body">
<div class="row justify-content-between">
<div class="col-11">
<h5 class="card-title">Monday ...</h5>
</div>
<div class="col-1">
Add Job
</div>
</div>
<div class="row">
<div class="col pt-3">
<table class="table table-bordered table-responsive">
<thead>
<tr class="table-light">
<th scope="col" class="col-2">Job</th>
<th scope="col">Driver</th>
<th scope="col">Type</th>
<th scope="col" class="col-2">Order #</th>
<th scope="col" class="col-2">Reference</th>
<th scope="col">Pallets</th>
<th scope="col">Weight (kg)</th>
<th scope="col" class="col-2">Status</th>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_assoc($monday)) {
//$id = $row['DriverID'];
$driverName_fk = $row['driverName_fk'];
$jobName = $row['jobName'];
$jobType = $row['jobType'];
$orderNumber = $row['orderNumber'];
$referenceNumber = $row['referenceNumber'];
$pallets = $row['pallets'];
$jobWeight = $row['jobWeight'];
$jobStatus = $row['jobStatus'];
echo "<tbody>
<tr>
<th>{$jobName}</th>
<th>{$driverName_fk}</td>
<td>{$jobType}</td>
<td>{$orderNumber}</td>
<td>{$referenceNumber}</td>
<td>{$pallets}</td>
<td>{$jobWeight}</td>
<td>{$jobStatus}</td>
</tr>
</tbody>";
}
?>
</table>
</div>
</div>
</div>
</div>
<!-- Tuesday -->
<div class="card tuesdayJobCard my-2">
<div class="card-body">
<div class="row justify-content-between">
<div class="col-11">
<h5 class="card-title">Tuesday ...</h5>
</div>
<div class="col-1">
Add Job
</div>
</div>
<div class="row">
<div class="col pt-3">
<table class="table table-bordered table-responsive">
<thead>
<tr class="table-light">
<th scope="col" class="col-2">Job</th>
<th scope="col">Driver</th>
<th scope="col">Type</th>
<th scope="col" class="col-2">Order #</th>
<th scope="col" class="col-2">Reference</th>
<th scope="col">Pallets</th>
<th scope="col">Weight (kg)</th>
<th scope="col" class="col-2">Status</th>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_assoc($tuesday)) {
//$id = $row['DriverID'];
$driverName_fk = $row['driverName_fk'];
$jobName = $row['jobName'];
$jobType = $row['jobType'];
$orderNumber = $row['orderNumber'];
$referenceNumber = $row['referenceNumber'];
$pallets = $row['pallets'];
$jobWeight = $row['jobWeight'];
$jobStatus = $row['jobStatus'];
echo "<tbody>
<tr>
<th>{$jobName}</th>
<th>{$driverName_fk}</td>
<td>{$jobType}</td>
<td>{$orderNumber}</td>
<td>{$referenceNumber}</td>
<td>{$pallets}</td>
<td>{$jobWeight}</td>
<td>{$jobStatus}</td>
</tr>
</tbody>";
}
?>
</table>
</div>
</div>
</div>
</div>
<!-- Wednesday -->
<div class="card wednesdayJobCard my-2">
<div class="card-body">
<div class="row justify-content-between">
<div class="col-11">
<h5 class="card-title">Wednesday ...</h5>
</div>
<div class="col-1">
Add Job
</div>
</div>
<div class="row">
<div class="col pt-3">
<table class="table table-bordered table-responsive ">
<thead>
<tr class="table-light">
<th scope="col" class="col-2">Job</th>
<th scope="col">Driver</th>
<th scope="col">Type</th>
<th scope="col" class="col-2">Order #</th>
<th scope="col" class="col-2">Reference</th>
<th scope="col">Pallets</th>
<th scope="col">Weight (kg)</th>
<th scope="col" class="col-2">Status</th>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_assoc($wednesday)) {
//$id = $row['DriverID'];
$driverName_fk = $row['driverName_fk'];
$jobName = $row['jobName'];
$jobType = $row['jobType'];
$orderNumber = $row['orderNumber'];
$referenceNumber = $row['referenceNumber'];
$pallets = $row['pallets'];
$jobWeight = $row['jobWeight'];
$jobStatus = $row['jobStatus'];
echo "<tbody>
<tr>
<th>{$jobName}</th>
<th>{$driverName_fk}</td>
<td>{$jobType}</td>
<td>{$orderNumber}</td>
<td>{$referenceNumber}</td>
<td>{$pallets}</td>
<td>{$jobWeight}</td>
<td>{$jobStatus}</td>
</tr>
</tbody>";
}
?>
</table>
</div>
</div>
</div>
</div>
<!-- Thursday -->
<div class="card thursdayJobCard my-2">
<div class="card-body">
<div class="row justify-content-between">
<div class="col-11">
<h5 class="card-title">Thursday ...</h5>
</div>
<div class="col-1">
Add Job
</div>
</div>
<div class="row">
<div class="col pt-3">
<table class="table table-bordered table-responsive">
<thead>
<tr class="table-light">
<th scope="col" class="col-2">Job</th>
<th scope="col">Driver</th>
<th scope="col">Type</th>
<th scope="col" class="col-2">Order #</th>
<th scope="col" class="col-2">Reference</th>
<th scope="col">Pallets</th>
<th scope="col">Weight (kg)</th>
<th scope="col" class="col-2">Status</th>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_assoc($thursday)) {
//$id = $row['DriverID'];
$driverName_fk = $row['driverName_fk'];
$jobName = $row['jobName'];
$jobType = $row['jobType'];
$orderNumber = $row['orderNumber'];
$referenceNumber = $row['referenceNumber'];
$pallets = $row['pallets'];
$jobWeight = $row['jobWeight'];
$jobStatus = $row['jobStatus'];
echo "<tbody>
<tr>
<th>{$jobName}</th>
<th>{$driverName_fk}</td>
<td>{$jobType}</td>
<td>{$orderNumber}</td>
<td>{$referenceNumber}</td>
<td>{$pallets}</td>
<td>{$jobWeight}</td>
<td>{$jobStatus}</td>
</tr>
</tbody>";
}
?>
</table>
</div>
</div>
</div>
</div>
<!-- Friday -->
<div class="card fridayJobCard my-2">
<div class="card-body">
<div class="row justify-content-between">
<div class="col-11">
<h5 class="card-title">Friday ...</h5>
</div>
<div class="col-1">
Add Job
</div>
</div>
<div class="row">
<div class="col pt-3">
<table class="table table-bordered table-responsive">
<thead>
<tr class="table-light">
<th scope="col" class="col-2">Job</th>
<th scope="col">Driver</th>
<th scope="col">Type</th>
<th scope="col" class="col-2">Order #</th>
<th scope="col" class="col-2">Reference</th>
<th scope="col">Pallets</th>
<th scope="col">Weight (kg)</th>
<th scope="col" class="col-2">Status</th>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_assoc($friday)) {
//$id = $row['DriverID'];
$driverName_fk = $row['driverName_fk'];
$jobName = $row['jobName'];
$jobType = $row['jobType'];
$orderNumber = $row['orderNumber'];
$referenceNumber = $row['referenceNumber'];
$pallets = $row['pallets'];
$jobWeight = $row['jobWeight'];
$jobStatus = $row['jobStatus'];
echo "<tbody>
<tr>
<th>{$jobName}</th>
<th>{$driverName_fk}</td>
<td>{$jobType}</td>
<td>{$orderNumber}</td>
<td>{$referenceNumber}</td>
<td>{$pallets}</td>
<td>{$jobWeight}</td>
<td>{$jobStatus}</td>
</tr>
</tbody>";
}
?>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>
<script src="../JS/app.js"></script>
<script src="../JS/ui.js"></script>
<?php include '../footer.php' ?>
I'm assuming everything will need to be in an if (isset) etc. but can't figure out what the condition needs to be. Nor how to get the data from the original functions.
The following is wholly untested and may have errors - so apologies but I think the initial page can be simplified by using a single sql select statement with no where clause and then use logic in PHP to determine which records are displayed based upon weekday number. In PHP you can use the date function to find the weekday number so the same block of code is repeated for each weekday number - and each table row now has the driver's name and ID to aid identification in Javascript. PHP & mySQL use a different numbering system for weekday - PHP uses 0 for Sunday whereas mySQL uses 0 for Monday!
<?php ini_set('error_reporting', E_ALL); ?>
<?php ini_set('display_errors', 1); ?>
<?php ini_set('display_startup_errors', 1); ?>
<?php include '../header.php' ?>
<!-- Page Title -->
<div class="container-sm text-dark px-3 p-4 truckList">
<div class="row m-auto align-items-center">
<?php
include '../includes/functions.php';
driverMenu();
?>
</div>
</div>
<?php
$sql='SELECT *
FROM openjobs
INNER JOIN driver ON openjobs.driverName_fk = driver.driverName';
$results = $conn->query( $sql );
$days=array(
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday'
);
for( $i=1; $i<=5; $i++ ){
printf('
<div class="container-fluid bg-secondary darkContainer">
<div class="container py-5 px-4 p-3 webWeeklyPlanTruckCard">
<div class="row gy-2">
<div class="col-12">
<!-- %1$s -->
<div class="card %2$sJobCard my-1">
<div class="card-body">
<div class="row justify-content-between">
<div class="col-11">
<h5 class="card-title">%1$s ...</h5>
</div>
<div class="col-1">
Add Job
</div>
</div>
<div class="row">
<div class="col pt-3">
<table class="table table-bordered table-responsive">
<thead>
<tr class="table-light">
<th scope="col" class="col-2">Job</th>
<th scope="col">Driver</th>
<th scope="col">Type</th>
<th scope="col" class="col-2">Order #</th>
<th scope="col" class="col-2">Reference</th>
<th scope="col">Pallets</th>
<th scope="col">Weight (kg)</th>
<th scope="col" class="col-2">Status</th>
</tr>
</thead>
',
$days[ $i ],
strtolower( $days[ $i ] )
);//close printf()
while( $row = $results->fetch_object() ) {
if( (int)date( 'w', strtotime( $row->jobDate ) )==$i ){
printf('<tbody>
<tr data-did="%9$s" data-driver="%1$s">
<th>%2$s</th>
<th>%1$s</td>
<td>%3$s</td>
<td>%4$s</td>
<td>%5$s</td>
<td>%6$s</td>
<td>%7$s</td>
<td>%8$s</td>
</tr>
</tbody>',
$row->driverName_fk,
$row->jobName,
$row->jobType,
$row->orderNumber,
$row->referenceNumber,
$row->pallets,
$row->jobWeight,
$row->jobStatus,
$row->DriverID
);
}
}
echo '
</table>
</div>
</div>
</div>
</div>';
}
?>
</div>
</div>
</div>
</div>
<!-- Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>
<script src="../JS/app.js"></script>
<script src="../JS/ui.js"></script>
<?php include '../footer.php' ?>
The driverMenu function is lightly modified to include the driver ID as an attribute to the input/button - again to aid identification in Javascript. One other change was the use of OO style syntax rather than the procedural - simply for convenience.
function driverMenu(){
global $conn;
$results=$conn->query('select * from driver');
while( $row = $results->fetch_object() ) {
printf('
<div class="col text-center">
<input type="button" data-name="driverNameBtn" class="btn btn-primary rounded-pill text-light" data-did="%s" value="%s" />
</div>',
$row->DriverID,
$row->driverName
);
}
}
The Javascript to show/hide various rows - the buttons are assign the same event handler which queries the DOM to find all table rows containing data and then shows/hides rows based upon the driver name and ID - taken from attributes assigned in the button and in the table row.
<script>
const qa=(e,n=document)=>n.querySelectorAll(e);
qa('input[ data-name="driverNameBtn" ]').forEach( bttn=>bttn.addEventListener('click',function(e){
e.preventDefault();
qa('table.table-responsive tbody tr').forEach( tr=>{
if( tr.dataset.did==this.dataset.did && tr.dataset.driver==this.value ){
tr.style.display='table-row';
}else{
tr.style.display='none';
}
})
}));
</script>

CSS not applying on ajax response

I want the external CSS to be applied on ajax response.
I am trying to create a page where after user selects options in a form and submits it then another form is loaded via ajax. But CSS is not applying on the ajax response.
I could not figure out the mistake. Please correct me if the approach is not correct.
$('#getStudents').submit(function(e) {
e.preventDefault();
$.ajax({
url: 'feature1/getStudents.php',
type: 'POST',
data: $('#getStudents').serialize(),
cache: false,
success: function(returndata) {
//alert(returndata);
$("#listStudents").html(returndata);
}
});
});
Here is the problem. On the 4th column css is not applying. The check box should appear as ios switch (http://abpetkov.github.io/switchery/)
<?php
$output='';
$output = $output. '
<div class="panel panel-white">
<div class="panel-heading clearfix">
<h3 class="panel-title"> Students </h3>
</div>
<div class="panel-body">
<div class="table-responsive">
<table id="students" class="display table" style="width: 100%; cellspacing: 0;">
<thead>
<tr>
<th> ID</th>
<th>Student ID</th>
<th>Name</th>
<th>Attendance</th>
</tr>
</thead>
<tfoot>
<tr>
<th> ID</th>
<th>Student ID</th>
<th>Name</th>
<th>Attendance</th>
</tr>
</tfoot>
<tbody>';
$i=1;
while($data = mysqli_fetch_assoc($results)) {
$output = $output. '<tr>
<td>'.$i++.'</td>
<td>'.$data["id"].'</td>
<td>'.$data["id"].'</td>
<td>
<div class="ios-switch pull-right switch-md">
<input type="checkbox" class="js-switch pull-right fixed-header-check" checked>
</div>
</td>
</tr>';
}
$output = $output. '
</tbody>
</table>
</div>
</div>
</div>';
echo $output;
mysqli_close($con);
?>
this is because your html page where you include all the css are loaded as the document is ready..it reads all classes and according to the style sheet it applies on that.. but in ajax we load the other page on the same html page and this time browser not map the html classes to the style sheet that is why your css is not applying on that
As I Can Understand , you are using an external JQuery Table Plugin and all the data (rows) are being fetched by AJAX Call.
You can use this setup[Backup your Previous File]:
What I am trying to do is: fetching only the required rows in AJAX call(not complete table). All the table structures and CSS are already loaded and on form Submission trying to fetch required rows. In your case the browser doesn't able to dynamically render the Table and CSS.
1. dashboard.php
<html>
<head>
<!-- Link to Plugin CSS-->
<!--Link to Your Custom CSS -->
<head>
<body>
//HTML and PHP code Here
<!-- Link to Plugin Script-->
<script>
// AJAX CALL
$('#getStudents').submit(function(e) {
e.preventDefault();
$.ajax({
url: 'feature1/getStudents.php',
type: 'POST',
data: $('#getStudents').serialize(),
cache: false,
success: function(returndata) {
//alert(returndata);
$("#students").html(returndata); //Fetch rows Only Not table(See the Next home.php page)
}
});
});
</script>
</body>
</html>
2. home.php
<div class="row">
<div class="panel panel-white">
<div class="panel-heading clearfix">
<h3 class="panel-title"> Students </h3>
</div>
<div class="panel-body">
<div class="table-responsive">
<table id="students" class="display table" style="width: 100%; cellspacing: 0;">
</table>
</div>
</div>
</div>
</div>
3. getStudents.php
In this page Modify $output variable.
Like
$output='';
$output = $output. '
<thead>
<tr>
<th> ID</th>
<th>Student ID</th>
<th>Name</th>
<th>Attendance</th>
</tr>
</thead>
<tfoot>
<tr>
<th> ID</th>
<th>Student ID</th>
<th>Name</th>
<th>Attendance</th>
</tr>
</tfoot>
<tbody>';
$i=1;
while($data = mysqli_fetch_assoc($results)) {
$output = $output. '<tr>
<td>'.$i++.'</td>
<td>'.$data["id"].'</td>
<td>'.$data["id"].'</td>
<td>
<div class="ios-switch pull-right switch-md">
<input type="checkbox" class="js-switch pull-right fixed-header-check" checked>
</div>
</td>
</tr>';
}
$output = $output. '
</tbody></div>';
OOps..It isn't the css problem. It's javascript that caused the problem.
Actually, the switchery.min.js appends a div with class switchery to . Because the script isn't initialised the rendering has failed.
Now adding the following javascript at the end of getStudents.php solved the issue.
echo '<script language="javascript">
if (Array.prototype.forEach) {
var elems = Array.prototype.slice.call(document.querySelectorAll(".js-switch"));
elems.forEach(function(html) {
var switchery = new Switchery(html);
});
} else {
var elems = document.querySelectorAll(".js-switch");
for (var i = 0; i < elems.length; i++) {
var switchery = new Switchery(elems[i]);
}
}
</script>';

Changing the value of a line with n lines (jquery, php, mysql)

When I click on "lock" I want only the line where I clicked to change the status to true and the lock be locked. When I click on "unlock" I want only the line where I clicked to change the status to false and the released lock.
In my current code:
when I click on a lock on the first line your status changes and also the lock, but the lock of the other lines also change. I need help to fix this.
There is also another thing that I think I should use ajax. When the table is generated and executed for the first time all status will be in false and the lock "open" To mark a line as locked (lock) the status will make is true, and I need to send this "true" pro database and save it through a php script, so that, to reload the page status I had changed continues saved.
I am using the following plugin Context menu:
For better visualization, I put my code in pastebin.
change.js
$(function(){
$.contextMenu({
selector: '.context-menu-one',
items: {
"block": {
name: "Lock",
icon: "edit",
callback: function(key, options){
var m = $(this).text();
var set = block(m, $(this)); // passando - this - como parâmetro de função
}
},
"sep1": "----------",
"unblock": {
name: "Unlock",
icon: "cut",
callback: function(key, options){
var m = $(this).text();
var set = unblock(m, $(this)); // passando - this - como parâmetro de função
}
}
}
});
});
function block(x, thisObj)
{
if (x === "false")
x = "true";
thisObj.html(x);
$(".lock1").html('<i class="fa fa-unlock" aria-hidden="true"></i>');
}
function unblock(x, thisObj)
{
if (x === "true")
x = "false";
thisObj.html(x);
$(".lock1").html('<i class="fa fa-lock" aria-hidden="true"></i>');
}
PasteBin.
network.php
<?php
$con = mysqli_connect("localhost", "root", "", "mydb2");
?>
<ul class="breadcrumb">
<li>Ir para</li>
<li>dashboard</li>
</ul>
<div class="page-content-wrap">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<div class="page-title">
<h2><span class="fa fa-hand-spock-o"></span> - Virtual Private Network</h2>
<p id="test">abc</p>
</div>
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table datatable table-striped">
<thead>
<tr>
<th>Common Name</th>
<th>Real Address</th>
<th>Virtual Address</th>
<th>Bytes Sent</th>
<th>Bytes Received</th>
<th>Since</th>
<th>Status</th>
<th>Blocked</th> <!--inserir a imagem de cliente bloqueado ou não -->
</tr>
</thead>
<tbody>
<tr>
<td class="context-menu-one">orion</td>
<td class="context-menu-one">177.43.212.110</td>
<td class="context-menu-one">172.16.191.145</td>
<td class="context-menu-one">872199</td>
<td class="context-menu-one">860412</td>
<td class="context-menu-one">Wed May 25 07:22:52 2016</td>
<td class="context-menu-one">false</td>
<td class="lock1"><i class="fa fa-unlock" aria-hidden="true"></i></td>
</tr>
<tr>
<td class="context-menu-one">elgin</td>
<td class="context-menu-one">189.10.58.244</td>
<td class="context-menu-one">172.16.6.210</td>
<td class="context-menu-one">301365</td>
<td class="context-menu-one">250459</td>
<td class="context-menu-one">Wed May 25 07:01:37 2016</td>
<td class="context-menu-one">false</td>
<td class="lock1"><i class="fa fa-unlock" aria-hidden="true"></i></td>
</tr>
<tr>
<td class="context-menu-one">databits</td>
<td class="context-menu-one">187.17.235.203</td>
<td class="context-menu-one">172.16.136.217</td>
<td class="context-menu-one">459833</td>
<td class="context-menu-one">409771</td>
<td class="context-menu-one">Wed May 25 06:09:01 2016</td>
<td class="context-menu-one">false</td>
<td class="lock1"><i class="fa fa-unlock" aria-hidden="true"></i></td>
</tr>
<tr>
<td class="context-menu-one">proficio</td>
<td class="context-menu-one">179.213.177.117</td>
<td class="context-menu-one">172.16.196.93</td>
<td class="context-menu-one">1083595</td>
<td class="context-menu-one">945154</td>
<td class="context-menu-one">Tue May 24 21:36:21 2016</td>
<td class="context-menu-one">false</td>
<td class="lock1"><i class="fa fa-unlock" aria-hidden="true"></i></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
?>
PasteBin.
My table:
The "lock" option:
It was a fun one!
First, I changed the position of context-menu-one class for the parent's.
And delegated the action on it's tds by this: selector: '.context-menu-one td'.
Because it makes less elements to "watch" for jQuery... And cleans the code.
Then... I modified the callback functions in your items object.
I played with .parent(), siblings() and prev() to target all the right elements.
HTML:
<ul class="breadcrumb">
<li>Ir para</li>
<li>dashboard</li>
</ul>
<div class="page-content-wrap">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<div class="page-title">
<h2><span class="fa fa-hand-spock-o"></span> - Virtual Private Network</h2>
<p id="test">abc</p>
</div>
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table datatable table-striped">
<thead>
<tr>
<th>Common Name</th>
<th>Real Address</th>
<th>Virtual Address</th>
<th>Bytes Sent</th>
<th>Bytes Received</th>
<th>Since</th>
<th>Status</th>
<th>Blocked</th> <!--inserir a imagem de cliente bloqueado ou não -->
</tr>
</thead>
<tbody>
<tr class="context-menu-one row">
<td>orion</td>
<td>177.43.212.110</td>
<td>172.16.191.145</td>
<td>872199</td>
<td>860412</td>
<td>Wed May 25 07:22:52 2016</td>
<td>false</td>
<td class="lock1"><i class="fa fa-unlock" aria-hidden="true"></i></td>
</tr>
<tr class="context-menu-one row">
<td>elgin</td>
<td>189.10.58.244</td>
<td>172.16.6.210</td>
<td>301365</td>
<td>250459</td>
<td>Wed May 25 07:01:37 2016</td>
<td>false</td>
<td class="lock1"><i class="fa fa-unlock" aria-hidden="true"></i></td>
</tr>
<tr class="context-menu-one row">
<td>databits</td>
<td>187.17.235.203</td>
<td>172.16.136.217</td>
<td>459833</td>
<td>409771</td>
<td>Wed May 25 06:09:01 2016</td>
<td>false</td>
<td class="lock1"><i class="fa fa-unlock" aria-hidden="true"></i></td>
</tr>
<tr class="context-menu-one row">
<td>proficio</td>
<td>179.213.177.117</td>
<td>172.16.196.93</td>
<td>1083595</td>
<td>945154</td>
<td>Tue May 24 21:36:21 2016</td>
<td>false</td>
<td class="lock1"><i class="fa fa-unlock" aria-hidden="true"></i></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
JavaScript:
$(function(){
$.contextMenu({
selector: '.context-menu-one td',
items: {
"block": {
name: "Lock",
icon: "edit",
callback: function(key, options){
console.log("HERE");
if ($(this).parent().hasClass("row")){
console.log("HERE IF");
thisRowPadlock = $(this).siblings(".lock1");
var m = $(this).siblings(".lock1").prev().text();
console.log(m);
var set = block(m, thisRowPadlock, $(this).siblings(".lock1").prev()); // passando - this - como parâmetro de função
}
}
},
"sep1": "----------",
"unblock": {
name: "Unlock",
icon: "cut",
callback: function(key, options){
console.log("THERE");
if ($(this).parent().hasClass("row")){
thisRowPadlock = $(this).siblings("lock1");
var m = $(this).siblings(".lock1").prev().text();
console.log(m);
var set = unblock(m, thisRowPadlock, $(this).siblings(".lock1").prev()); // passando - this - como parâmetro de função
}
}
}
}
});
});
function block(x, padlock, thisObj)
{
console.log("Function block");
if (x === "false")
x = "true";
thisObj.html(x);
$(padlock).html('<i class="fa fa-unlock" aria-hidden="true"></i>');
}
function unblock(x, padlock, thisObj)
{
console.log("Function unblock");
if (x === "true")
x = "false";
thisObj.html(x);
$(padlock).html('<i class="fa fa-lock" aria-hidden="true"></i>');
}
And a working Fiddle (with some missing CSS, sorry).

Unable to make the html of the data in the controller and show on the dropdown select

i want to show the restaurant after selecting its area from dropdown list. but my code did not show the restaurant name and the menu button of that restaurant please tell me where i did mistake
This is my view code
<div id="restaurant">
</div>
This is controller code
public function get_rests()
{
$cit_id = $this->input->post('cit_id');
$area = $this->input->post('areaID');
$where = array('city_id'=>$cit_id,'city_area_id'=>$area);
$html = '
<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th>
No.
</th>
<th style="">
Restaurant Names
</th>
</tr>
</thread>
<tbody>
<?php $i=1;foreach($result as $row){
?>
<tr id="<?php echo $row->restaurant_id; ?>" class="res_id">
<th style="">
<?php echo $i++; ?>
</th>
<th style="">
<?php echo $row->restaurant_name; ?>
</th>
<th style="width: 1%" >
</i>See Menu
</th>
</tr>
<?php } ?>
</tbody>
</table>
</div>
';
echo $html;
This is my model code view
function select_record($table, $where = NULL){
$this->db->select();
if($where)
$this->db->where($where);
$this->db->from($table);
$query = $this->db->get();
// echo $this->db->last_query();
return $query->result();
}
Script code
function get_rests(){
var city_id = $('#city_id').val();
var area_id = $("#area_id").val();
$.ajax({
type: "POST",
url: "<?=base_url();?>index.php/Bulk_Controller/get_rests",
data: {cit_id: city_id,areaID: area_id},
dataType: "text",
cache:false,
success:
function(data){
// alert(data);
$('#restaurant').html(data);
}
});
}
You didn't called a model function to get data from your DB Table.
public function get_rests()
{
$cit_id = $this->input->post('cit_id');
$area = $this->input->post('areaID');
$where = array('city_id'=>$cit_id,'city_area_id'=>$area);
$result= $this->your_model->select_record('table_name',$where);
$html = '
<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th>
No.
</th>
<th style="">
Restaurant Names
</th>
</tr>
</thread>
<tbody>
<?php $i=1;foreach($result as $row){
?>
<tr id="<?php echo $row->restaurant_id; ?>" class="res_id">
<th style="">
<?php echo $i++; ?>
</th>
<th style="">
<?php echo $row->restaurant_name; ?>
</th>
<th style="width: 1%" >
</i>See Menu
</th>
</tr>
<?php } ?>
</tbody>
</table>
</div>
';
echo $html;

Slide Up/Down does not work properly

Hi I want to display some search results using tables. In my code the basic details are displayed in a one table and if we want to see more details you have to click view more option. The hide details are also be displayed in a separate table.But I have a problem in my code. If I have more than one search result in my database the details regards to first person are displayed as I want. that means it I want to see more details i want to click on view more and it slide down the hide table. But the rest of search result displays the hidden tables as well. and also if i click the view more option of the rest, it again slide down the hidden table of very first person's table. here I have attached my code. Could you please help me to solve this issue?
<body>
<div class="container"> <!-- container -->
<div class="row" id="main-content">
<div class="span8">
<div class="well"> <!-- -start of well class -->
<?php
dbConnect();
$district = $_POST['district'];
$category = $_POST['catID'];
$subject = $_POST['subID'];
//get the category name
$get_cat_name = mysql_query("SELECT catName FROM tutor_category WHERE catID='{$category}' ");
while($row = mysql_fetch_assoc($get_cat_name ))
{
$cat_name = $row['catName'];
}
//get the subject name
$get_sub_name = mysql_query(" SELECT subName FROM tutor_subjects WHERE catID='{$category}' AND subID='{$subject}'");
while($row = mysql_fetch_assoc($get_sub_name ))
{
$sub_name = $row['subName'];
}
?>
<!-- ****************** Heading Table *******************-->
<table class="table table-bordered">
<tr>
<th> <?php echo $district." District - ". $cat_name ." - ". $sub_name ?> </th>
</tr>
</table>
<!-- ****************** end of heading table *******************-->
<?php
//get tutor IDs
$get_tutor_id = mysql_query(" SELECT DISTINCT tutorID FROM tutor_register_subjects WHERE district='{$district}' AND catID='{$category}' AND subID='{$subject}' ");
while($row = mysql_fetch_assoc($get_tutor_id)) // first
{
$tutor_id = $row['tutorID'];
$query = mysql_query(" SELECT * FROM tutor WHERE tutorID='{$tutor_id}' ");
while($row = mysql_fetch_assoc($query))
{ // second
$fname = $row['firstName'];
$lname = $row['lastName'];
$nic = $row['nic'];
$gender = $row['gender'];
$education = $row['education'];
$address = $row['address'];
$profession= $row['profession'];
$email = $row['email'];
$cnum = $row['contactNum'];
$avatar = $row['avatar'];
} // second
?>
<div class="control-group">
<!-- basic details -->
<table class="table table-bordered">
<tr>
<td width="120" rowspan="4"><?php echo "<img src='uploads/".$avatar."' height='120' width='100'>"?></td>
<th width="120">Name</th>
<td><?php echo $fname." ". $lname?></td>
</tr>
<tr>
<th>NIC</th>
<td><?php echo $nic ?></td>
</tr>
<tr>
<th>Gender</th>
<td><?php echo $gender ?></td>
</tr>
<tr>
<td colspan="2"><a class="more">View More</a> <a class="less">View Less</a></td>
</tr>
</table>
</div>
<!-- end of basic details -->
<!-- more details-->
<div class="control-group" id="more">
<table class="table table-bordered">
<tr>
<th>Contact Number</th>
<td><?php echo $cnum ?></td>
</tr>
<tr>
<th>Email</th>
<td><?php echo $email ?></td>
</tr>
<tr>
<th>Address</th>
<td><?php echo $address ?></td>
</tr>
<tr>
<th>Education</th>
<td><?php echo $education ?></td>
</tr>
<tr>
<th>Work Place</th>
<td><?php echo $profession ?></td>
</tr>
</table>
</div>
<!-- end of more details-->
<legend></legend>
<?php
} // first
?>
</div> <!-- -start of well class -->
</div> <!-- end span12 class -->
</div> <!-- end of row class -->
</div> <!-- container -->
<!-- view more script-->
<script src="../js/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#more").hide();
$(".less").click(function(){
$("#more").slideUp(1000);
});
$(".more").click(function(){
$("#more").slideDown(1000);
});
});
</script>
</body>
What's happening is that $("#more") selects every div with ID more, and changes it
Give each .less and .more something which is unique to the current user:
<div class="less" data-id="<?php echo $person_id; ?>">Show less</div>
<div class="more" data-id="<?php echo $person_id; ?>">Show more</div>
Then in your #more, where the actual data is, do something about the same
<div id="more-<?php echo $person_id; ?>">More information here</div>
Now in your jQuery, select the correct id:
$(".less").click(function(){
$("#more-"+ $(this).attr('data-id')).slideUp(1000);
});
$(".more").click(function(){
$("#more-"+ $(this).attr('data-id')).slideDown(1000);
});
What you are effictively doing now is telling jQuery to select div with e.g. id="more-15" in case you clicked on the .more with attribute data-id="15", thus selecting the correct div :)
Note: You don't have to use divs to be able to do this. This would also solve invalid HTML because you got tons of elements with the same id's
See: http://ejohn.org/blog/html-5-data-attributes/

Categories