Get row content to a popup window - javascript

I have used this function to pop up a window, when a user clicked the table row
<script type="text/javascript">
$(document).ready(function() {
$("td.abc").click(function() {
var t = 'Ticket ID: ' + $(this).find('td:eq(0)').html()+'\n'+'\n';
var r = 'Subject: ' + $(this).find('td:eq(1)').html()+'\n'+'\n';
var e = 'Messege: ' + $(this).find('td:eq(2)').html()+'\n'+'\n';
var f = 'Developer: ' + $(this).find('td:eq(3)').html()+'\n'+'\n';
var w = 'Current Status: ' + $(this).find('td:eq(4)').html()+'\n'+'\n';
var q = 'Uploaded Date & Time: ' + $(this).find('td:eq(5)').html()+'\n'+'\n';
var o = $("#79").attr("name")+'\n'+'\n';
alert(t+r+e+f+w+q+o);
});
});
</script>
my table is as follows
echo "<br> <table border='0' id='example' width='980' align='center'>
<tr>
<th width='60'>Ticket ID</th>
<th width='150'>Subject</th>
<th width='670'>Messege</th>
<th width='60'>Developer</th>
<th width='70'>Status</th>
<th width='105'>Date - Time</th>
</tr>";
while($row = mysql_fetch_array($result)) {
//echo $row['jobSatus'];
echo "<tr>";
echo "<td class=\"abc\" width='60'>" . $row['id'] . "</td>";
echo "<td class=\"abc\" width='150'>" . $row['subject'] . "</td>";
echo "<td class=\"abc\" width='670'>" . $row['msg'] . "</td>";
echo "<td class=\"abc\" width='60'>" . $row['developer'] . "</td>";
echo "<td class=\"abc\"width='60'>" . $row['jobstatus'] . "</td>";
echo "<td class=\"abc\" width='105'>" . $row['date_time'] . "</td>";
echo "<input class=\"abc\" type='hidden' name=".$row['image']." id=".$row['id'].">";
echo "</tr>";
}
echo "</table>";
Now inside the pop window I don't get any values. It says "undefined". I would be very thankful if someone could show what I have done wrong.

try to change this...
var t = 'Ticket ID: ' + $(this).find('td:eq(0)').html()+'\n'+'\n';
to this
var t = 'Ticket ID: ' +$(this).closest('td:eq(0)').attr('class');
Hope this will help....

You are clicking on a table cell, not a table row
$("td.abc").click(function() {
^^
Change it to look at the table
$("#example").on("click", "tr", function () {
or
$("#example").on("click", "tr:has(td.abc)", function () {
And your HTML markup is invalid since an input element can not be a child element of a tr.

Set onclick on table-row
$("tr.ticker-row").click(function(ev) {
var $this = $(ev.currentTarget);
var t = 'Ticket ID: ' + $this.find('td:eq(0)').html()+'\n'+'\n';
var r = 'Subject: ' + $this.find('td:eq(1)').html()+'\n'+'\n';
var e = 'Messege: ' + $this.find('td:eq(2)').html()+'\n'+'\n';
var f = 'Developer: ' + $this.find('td:eq(3)').html()+'\n'+'\n';
var w = 'Current Status: ' + $this.find('td:eq(4)').html()+'\n'+'\n';
var q = 'Uploaded Date & Time: ' + $this.find('td:eq(5)').html()+'\n'+'\n';
var o = $this.find('input').attr('name') + '\n'+'\n';
alert(t+r+e+f+w+q+o);
});
On php, do not insert any node in tr except td
while($row = mysql_fetch_array($result)) {
//echo $row['jobSatus'];
echo "<tr class=\"ticket-row\" data-row-index=\"".$row['id']."\">";
echo "<td class=\"abc\" width='60'>" . $row['id'] . "<input class=\"abc\" type='hidden' name=".$row['image']." ></td>";
echo "<td class=\"abc\" width='150'>" . $row['subject'] . "</td>";
echo "<td class=\"abc\" width='670'>" . $row['msg'] . "</td>";
echo "<td class=\"abc\" width='60'>" . $row['developer'] . "</td>";
echo "<td class=\"abc\"width='60'>" . $row['jobstatus'] . "</td>";
echo "<td class=\"abc\" width='105'>" . $row['date_time'] . "</td>";
echo "</tr>";
}
echo "</table>";

Related

problem with jquery datepicker on dynamic content with datatables plugin

im having this issue after populating an html table with the jquery datatables plugin, i have an input field where i need to show the datepicker when i put the focus on the field, i have tried using .on, using body and document as selectors and it doesnt trigger the datepicker. Even when i use the body selector i can get the id of the element and display it on the console, but the datepicker doesnt work.
here is my code:
$(document).ready(function(){
$.ajax({
type: "POST",
url: "classes/ajax.class.php",
data: {
task: "getAllOrdenesSinAnularJson"
}
}).done(function(data){
//console.log(data);
data = data.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
// remove non-printable and other non-valid JSON chars
data = data.replace(/[\u0000-\u0019]+/g,"");
var data = JSON.parse(data);
$("#datatable").show();
$("#esconder").hide();
j=1;
for(i=0; i<data.length;i++) {
var rowIndex = $('#datatable').dataTable().fnAddData([
data[i].ordenar,
data[i].pedido,
data[i].consecutivo,
data[i].cedula,
data[i].cliente,
data[i].fecha_creado,
data[i].fecha_entrega,
data[i].fecha_diseno,
data[i].fecha_produccion,
data[i].fecha_abierta,
data[i].diseno,
data[i].produccion,
data[i].entregada,
data[i].pedido
]);
}
})
$(document).on('focus',".fechaAbierta", function(){
var id = $(this).attr("id");
console.log(id);
$("#"+id).datepicker();
});
the php code:
public function getAllOrdenesSinAnularJson() {
$db = new database();
$conn = $db->conn();
try {
$sql = "SELECT * from dummy_orden_pedido where estado not in (4,5) order by orden DESC";
$query = $conn->prepare($sql);
$query->execute();
$results = $query->fetchAll();
$i=0;
//die(var_dump($results));
foreach ($results as $rsl) {
$id_paciente = $rsl['id_paciente'];
$consecutivo = $rsl['consecutivo'];
$sql_paciente = "SELECT id_preregistro ,nombre, apellido, cedula, telefono, id_cliente FROM dummy_preregistro WHERE id_preregistro = '".$id_paciente."'";
$query = $conn->prepare($sql_paciente);
$query->execute();
$results_paciente = $query->fetch();
switch ($rsl['estado']) {
case '1':
$estado="Diseño";
break;
case '2':
$estado="Producción";
break;
case '3':
$estado="Terminado";
break;
case '4':
$estado="Entregada";
break;
case '5':
$estado="Anulada";
break;
case '6':
$estado="Inicial";
break;
case '7':
$estado="Facturada";
break;
}
//$ordenes_completas[$i]['pedido'] = "<td align='center' class='row_der sorting_1'><center>".str_pad($rsl["id_orden_pedido"], 8, '0', STR_PAD_LEFT)."</center></td>";
$ordenes_completas[$i]['ordenar'] = "<td align='center' class='row_der sorting_1'>ordenar</td>";
$ordenes_completas[$i]['pedido'] = "<td align='center' class='row_der sorting_1'><center><a target='_blank' href='index.php?id=editar_orden&id_orden=".$rsl["id_orden_pedido"]."'>".str_pad($rsl["id_orden_pedido"], 8, '0', STR_PAD_LEFT)."</a></center></td>";
$ordenes_completas[$i]['consecutivo'] = "<td align='center' class='row_der'><center>".$consecutivo."</center></td>";
$ordenes_completas[$i]['cedula'] = "<td align='center' class='row_der'><center>".$results_paciente['cedula']."</center></td>";
$nombre = $results_paciente['nombre']." ".$results_paciente['apellido'];
$ordenes_completas[$i]['cliente'] = "<center><a target='_blank' href='index.php?id=historicoUsuario&user_id=".$results_paciente['id_preregistro']."'>".$nombre."</a></center>";
//$ordenes_completas[$i]['entrega'] = "<td align='center' class='row_der'><center>".$results_paciente['telefono']."</center></td>";
$ordenes_completas[$i]['fecha_creado'] = "<td align='center' class='row_der'><center>".$rsl['fecha_creado']."</center></td>";
$ordenes_completas[$i]['fecha_produccion'] = "<td align='center' class='row_der'><center>".$rsl['fecha_produccion']."</center></td>";
$ordenes_completas[$i]['fecha_entrega'] = "<td align='center' class='row_der'><center>".$rsl['fecha_entrega']."</center></td>";
//$ordenes_completas[$i]['fecha_abierta'] = "<td align='center' class='row_der'><center>".$rsl['fecha_abierta']."</center></td>";
$ordenes_completas[$i]['fecha_abierta'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['fecha_abierta'] .= "<div class='preregistro_campo_fecha_abierta'>";
$ordenes_completas[$i]['fecha_abierta'] .= "<input name='fechaAbierta' type='text' class='fechaAbierta hasDatepicker' id='fechaAbierta_".$rsl["id_orden_pedido"]."' value=''>";
$ordenes_completas[$i]['fecha_abierta'] .= "</div>";
$ordenes_completas[$i]['fecha_abierta'] .= "</td>";
//$ordenes_completas[$i]['fecha_produccion'] = "<td align='center' class='row_der'><center>$ ".number_format($rsl["precio"],2)."</center></td>";
$ordenes_completas[$i]['estado'] = "<td align='center' class='row_der'><center>".$rsl['estado']."</center></td>";
$ordenes_completas[$i]['fecha_diseno'] = "<td align='center' class='row_der'><center>".$rsl['fecha_diseno']."</center></td>";
/* Diseño */
if ($rsl['estado'] == "1") {
$ordenes_completas[$i]['diseno'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['diseno'].= "<div class='hoja_iconos_no_active'><img src='assets/image/diseno/022_hoja_trabajo/ico_02.png'/></div>";
$ordenes_completas[$i]['diseno'].= "</td>";
$ordenes_completas[$i]['produccion']= "<td align='center' class='row_der'>";
$ordenes_completas[$i]['produccion'].= "<div class='hoja_iconos' style='cursor:pointer;' id='P".$value["id_orden_pedido"]."' orden=".$value["id_orden_pedido"]."><label class='trans'>2</label><img src='assets/image/diseno/022_hoja_trabajo/ico_01.png'/></div>";
$ordenes_completas[$i]['produccion'].= "</td>";
$ordenes_completas[$i]['entregada']= "<td align='center' class='row_izq'>";
$ordenes_completas[$i]['entregada'].= "<div class='hoja_iconos' style='cursor:pointer;' id='E".$value["id_orden_pedido"]."' orden=".$value["id_orden_pedido"]."><label class='trans'>2</label><img src='assets/image/diseno/022_hoja_trabajo/ico_01.png'/></div>";
$ordenes_completas[$i]['entregada'].= "</td>";
}
/* Producción */
if ($rsl['estado'] == "2") {
$ordenes_completas[$i]['diseno'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['diseno'].= "<div class='hoja_iconos_no_active'><label class='trans'>3</label><img src='assets/image/diseno/022_hoja_trabajo/ico_02.png'/></div>";
$ordenes_completas[$i]['diseno'].= "</td>";
$ordenes_completas[$i]['produccion'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['produccion'].= "<div class='hoja_iconos_no_active'><label class='trans'>3</label><img src='assets/image/diseno/022_hoja_trabajo/ico_02.png' /></div>";
$ordenes_completas[$i]['produccion'].= "</td>";
$ordenes_completas[$i]['entregada'] = "<td align='center' class='row_izq'>";
$ordenes_completas[$i]['entregada'].= "<div class='hoja_iconos' style='cursor:pointer;' id='E".$value['id_orden_pedido']."' orden='".$value['id_orden_pedido']."'><label class='trans'>3</label><img src='assets/image/diseno/022_hoja_trabajo/ico_01.png'/></div>";
$ordenes_completas[$i]['entregada'].= "</td>";
}
/* Terminado */
if ($rsl['estado'] == "3") {
$ordenes_completas[$i]['diseno'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['diseno'] .= "<div class='hoja_iconos_no_active'><label class='trans'>4</label><img src='<?php echo $base_path;?>assets/image/diseno/022_hoja_trabajo/ico_02.png' /></div>";
$ordenes_completas[$i]['diseno'] .= "</td>";
$ordenes_completas[$i]['produccion'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['produccion'] .= "<div class='hoja_iconos_no_active'><label class='trans'>4</label><img src='<?php echo $base_path;?>assets/image/diseno/022_hoja_trabajo/ico_02.png' /></div>";
$ordenes_completas[$i]['produccion'] .= "</td>";
$ordenes_completas[$i]['entregada'] = "<td align='center' class='row_izq'>";
$ordenes_completas[$i]['entregada'].= "<div class='hoja_iconos' style='cursor:pointer;' id='E".$value['id_orden_pedido']."' orden='".$value['id_orden_pedido']."'><label class='trans'>4</label><img src='assets/image/diseno/022_hoja_trabajo/ico_01.png' /></div>";
$ordenes_completas[$i]['entregada'].= "</td>";
}
/* Entregado */
if ($rsl['estado'] == "4") {
$ordenes_completas[$i]['diseno'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['diseno'] .= "<div class='hoja_iconos_no_active'><label class='trans'>5</label><img src='assets/image/diseno/022_hoja_trabajo/ico_02.png' /></div>";
$ordenes_completas[$i]['diseno'] .= "</td>";
$ordenes_completas[$i]['produccion'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['produccion'] .= "<div class='hoja_iconos_no_active'><label class='trans'>5</label><img src='assets/image/diseno/022_hoja_trabajo/ico_02.png' /></div>";
$ordenes_completas[$i]['produccion'] .= "</td>";
$ordenes_completas[$i]['entregada'] = "<td align='center' class='row_izq'>";
$ordenes_completas[$i]['entregada'] .= "<div class='hoja_iconos_no_active'><label class='trans'>5</label><img src='assets/image/diseno/022_hoja_trabajo/ico_02.png' /></div>";
$ordenes_completas[$i]['entregada'] .= "</td>";
}
/* Anulada */
if ($rsl['estado'] == "5") {
$ordenes_completas[$i]['diseno'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['diseno'] .= "<div class='hoja_iconos_no_active'>";
$ordenes_completas[$i]['diseno'] .= "Anulada";
$ordenes_completas[$i]['diseno'] .= "</div>";
$ordenes_completas[$i]['diseno'] .= "</td>";
$ordenes_completas[$i]['produccion'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['produccion'] .= "<div class='hoja_iconos_no_active'>";
$ordenes_completas[$i]['produccion'] .= "Anulada";
$ordenes_completas[$i]['produccion'] .= "</div>";
$ordenes_completas[$i]['produccion'] .= "</td>";
$ordenes_completas[$i]['entregada'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['entregada'] .= "<div class='hoja_iconos_no_active'>";
$ordenes_completas[$i]['entregada'] .= "Anulada";
$ordenes_completas[$i]['entregada'] .= "</div>";
$ordenes_completas[$i]['entregada'] .= "</td>";
}
/* Estado inicial */
if ($rsl['estado'] == "6") {
$ordenes_completas[$i]['diseno'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['diseno'] .= "<div class='hoja_iconos' style='cursor:pointer;' id='D".$value['id_orden_pedido'].' orden="'.$value['id_orden_pedido']."'><label class='trans'>1</label><img src='assets/image/diseno/022_hoja_trabajo/ico_01.png'/></div>";
$ordenes_completas[$i]['diseno'] .= "</td>";
$ordenes_completas[$i]['produccion'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['produccion'] .= "<div class='hoja_iconos' style='cursor:pointer;' id='P".$value['id_orden_pedido']."' orden='".$value['id_orden_pedido']."'><label class='trans'>1</label><img src='assets/image/diseno/022_hoja_trabajo/ico_01.png'/></div>";
$ordenes_completas[$i]['produccion'] .= "</td>";
$ordenes_completas[$i]['entregada'] = "<td align='center' class='row_izq'>";
$ordenes_completas[$i]['entregada'] .= "<div class='hoja_iconos' style='cursor:pointer;' id='E".$value['id_orden_pedido']."' orden='".$value['id_orden_pedido']."'><label class='trans'>1</label><img src='assets/image/diseno/022_hoja_trabajo/ico_01.png'/></div>";
$ordenes_completas[$i]['entregada'] .= "</td>";
}
/* Facturado */
if ($rsl['estado'] == "7") {
$ordenes_completas[$i]['diseno'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['diseno'] .= "<div class='hoja_iconos_no_active'><label class='trans'>6</label><img src='assets/image/diseno/022_hoja_trabajo/ico_02.png'/></div>";
$ordenes_completas[$i]['diseno'] = "</td>";
$ordenes_completas[$i]['produccion'] = "<td align='center' class='row_der'>";
$ordenes_completas[$i]['produccion'] = "<div class='hoja_iconos_no_active'><label class='trans'>6</label><img src='assets/image/diseno/022_hoja_trabajo/ico_02.png'/></div>";
$ordenes_completas[$i]['produccion'] = "</td>";
$ordenes_completas[$i]['entregada'] = "<td align='center' class='row_izq'>";
$ordenes_completas[$i]['entregada'] .= "<div class='hoja_iconos_no_active'><label class='trans'>6</label><img src='assets/image/diseno/022_hoja_trabajo/ico_02.png'/></div>";
$ordenes_completas[$i]['entregada'] .= "</td>";
}
//die(var_dump($ordenes_completas[$i]['diseno']));
//$ordenes_completas[$i]['diseno'] = "<td align='center' class='row_der'><div class='hoja_iconos_no_active'><label class='trans'>3</label><img src='http://www.dummy.net/intranet/assets/image/diseno/022_hoja_trabajo/ico_02.png'></div></td>";
//$ordenes_completas[$i]['diseno'] = "<center><a href='index.php?id=nueva_factura&idorden=".$rsl["id_orden_pedido"]."&cliente=".$results_paciente['id_preregistro']."'>";
//$ordenes_completas[$i]['produccion'] .= "<img src='assets/image/diseno/014_facturacion/facturar.png' title='Facturar'>";
//$ordenes_completas[$i]['facturar'] .= "</a></center>";
//$ordenes_completas[$i]['entregada'] = "<center><div class='pacientes_icono_editar elimina anula' id='eliminaAction' idpaciente='".$results_paciente['id_preregistro']."' style='cursor:pointer;'>";
$i++;
}
$results = json_encode($ordenes_completas);
echo $results;
} catch ( PDOException $e ) {
var_dump($e->getMessage());
echo 'Caught exception: ', $e->getMessage(), "\n";
return false;
}
}
Thanks in advance!

Passing Javascript Variables to PHP function

I am trying to get my Javascript variables passed back to my PHP function. I am aware that I need to use the GET method but am unsure how to implement it here.
My PHP Function:
function showRailroadSchedule()
{
global $conn;
global $thisPHP;
$sql = "SELECT * FROM trip";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
echo '<div class="limiter">';
echo '<div class="container-table100">';
echo '<div class="wrap-table100">';
echo '<div class="table100 ver2 m-b-110">';
echo "<h2>Available Trip Reservations</h2>";
echo '<table data-vertable="ver2" id="tableMain">';
echo "<thead><tr class='row100 head'><th>Trip ID</th><th>Arrival Time</th><th>Departure Time</th><th>Train ID</th><th>Arrival Station</th><th>Track ID</th><th>Departure Station</th></tr></thead>";
while($row = $result->fetch_assoc())
{
echo '<tbody><tr class="row100">';
$TRIP_ID = $row["TRIP_ID"];
echo
' <td class="column100 column2" data-column="column2"> ' . $row["TRIP_ID"] .
' </td> <td class="column100 column3" data-column="column3"> ' . $row["TRIP_ARR_TIME"] .
' </td> <td class="column100 column4" data-column="column4"> ' . $row["TRIP_DEP_TIME"] .
' </td> <td class="column100 column5" data-column="column5"> ' . $row["TRAIN_ID"] .
'</td> <td class="column100 column6" data-column="column6"> ' . $row["TS_ARRIVAL"] .
'</td> <td class="column100 column7" data-column="column7"> ' . $row["TRACK_ID"] .
'</td> <td class="column100 column8" data-column="column8"> ' . $row["TS_DEPARTURE"] .
'</td> ';
echo "<form action='{$thisPHP}' method='post' style='display:inline' >";
echo "<input type='hidden' name='cID' value='{$cID}'>";
echo "</td></tr></tbody>";
}
echo "<tfoot><tr><td>'$_GET[w1]'</td><td>Arrival Time</td><td>Departure Time</td><td>Train ID</td><td>Arrival Station</td><td>Track ID</td><td>Departure Station</td></tr></tfoot>";
echo "</table>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
else
{
echo "0 results";
}
}
My Javascript Function:
<script>
$(document).ready(function() {
//=================================================================
//click on table body
//$("#tableMain tbody tr").click(function () {
$('#tableMain tbody').on('click', 'tr', function() {
//get row contents into an array
var tableData = $(this).children("td").map(function() {
return $(this).text();
}).get();
var tripid = tableData[0];
var arrivaltime= tableData[1];
var departuretime = tableData[2];
var trainid = tableData[3];
var arrivalstation = tableData[4];
var trackid = tableData[5];
var departurestation = tableData[6];
//var arrivaltime = tableData[1] + '*' + tableData[2] + '*' + tableData[3] + '*' + tableData[4] + '*' + tableData[5] + '*' + tableData[6] + '*' + tableData[7];
alert(tripid);
window.location.href = "railroadutils.php?w1=" + tripid;
});
$("#thebutton").click(function() {
$('#tableMain > tbody').append('<tr class="datarow"><td>11111</td><td>22222</td><td>33333</td><td>44444</td><td>55555</td></tr>')
})
});
</script>
After a row is clicked, the page refreshes and a blank screen appears, however the link does have the w1 variable in it. I am just wanting to display this w1 variable inside my table.
Thank you in advance!
Without seeing the rendered HTML, it's hard to see what's happening but I think this is your issue:
echo "<tfoot><tr><td>'$_GET[w1]'</td><td>Arrival Time</td><td>Departure Time</td><td>...
^^^^^^^^^^^
I think you need to concatenate it like you did the other echo statements like this:
echo "<tfoot><tr><td>'" . $_GET[w1] . "'</td><td>Arrival Time</td><td>Departure Time</td><td>...
^^^^^^^^^^^^^^^^^^

Need to change tr color in my php mysql table

I have a table where data is fetched from the MYSQLi table, I have a column as 'status' where it has two values of ACTIVE and DEACTIVE. I need deactive in different bg color/font color and active status in the different color, how could I achieve this through if else statement. At least td is changed also no problem.
<?php
// Include config file
require_once 'config.php';
// Attempt select query execution
$sql = "SELECT * FROM retail";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table js-dynamitable table-bordered table-striped table-condensed'>";
echo "<thead>";
echo "<tr>";
echo "<th>#</th>";
echo "<th>operator_name</th>";
echo "<th>zone</th>";
echo "<th>nas_ip</th>";
echo "<th>switch_name</th>";
echo "<th>switch_ip</th>";
echo "<th>switch_port</th>";
echo "<th>connected_port</th>";
echo "<th>vlan</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['operator_name'] . "</td>";
echo "<td>" . $row['zone'] . "</td>";
echo "<td>" . $row['nas_ip'] . "</td>";
echo "<td>" . $row['switch_name'] . "</td>";
echo "<td>" . $row['switch_ip'] . "</td>";
echo "<td>" . $row['switch_port'] . "</td>";
echo "<td>" . $row['connected_port'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>";
echo "<a href='readr.php?id=". $row['id'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "<a href='updater.php?id=". $row['id'] ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='deleter.php?id=". $row['id'] ."' data-toggle='modal' data-target='#exampleModalLong' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
I am Using this script too, which is correct format for header
var $table = $('table');
$table.bootstrapTable({
search: true,
pagination: true,
buttonsClass: 'primary',
minimumCountColumns: 2,
columns: [{
field: 'id',
title: 'ID',
sortable: true,
}, {
field: 'operator_name',
title: 'OPERATOR NAME',
sortable: true,
}, {
field: 'zone',
title: 'zone',
sortable: true,
},
{
field: 'nas_ip',
title: 'nas_ip',
sortable: true,
},
{
field: 'switch_name',
title: 'SWITCH NAME',
sortable: true,
},
{
field: 'switch_ip',
title: 'SWITCH IP',
sortable: true,
},
{
field: 'switch_port',
title: 'SWITCH PORT ',
sortable: true,
},
{
field: 'connected_port',
title: 'connected_port',
sortable: true,
},
{
field: 'vlan',
title: 'VLAN',
sortable: true,
},
{
field: 'operation',
title: 'Action',
},
],
});
So you're using bootstrab table plugin. You need to add rowStyle handler.
In your JS script you can try add.
function rowStyle(row, index) {
var classes = ['success','danger'];
if(row.vlan=="ACTIVE")
return {
classes: classes[0]
};
if(row.vlan=="DEACTIVE")
return {
classes: classes[1]
};
return {};
}
In your PHP file:
Change:
echo "<table class='table js-dynamitable table-bordered table-striped table-condensed'>";
To:
echo "<table data-row-style='rowStyle' class='table js-dynamitable table-bordered table-striped table-condensed'>";
Change:
echo "<th>vlan</th>";
To:
echo "<th data-field='vlan'>vlan</th>";
Demo: http://jsfiddle.net/jyjafk8v/
http://bootstrap-table.wenzhixin.net.cn/documentation/ - search 'rowStyle' if you need more info about it.
I have changed status td (column) . Add inline style and apply background color on if else base. Check the code below.
<?php
// Include config file
require_once 'config.php';
// Attempt select query execution
$sql = "SELECT * FROM retail";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table js-dynamitable table-bordered table-striped table-condensed'>";
echo "<thead>";
echo "<tr>";
echo "<th>#</th>";
echo "<th>operator_name</th>";
echo "<th>zone</th>";
echo "<th>nas_ip</th>";
echo "<th>switch_name</th>";
echo "<th>switch_ip</th>";
echo "<th>switch_port</th>";
echo "<th>connected_port</th>";
echo "<th>vlan</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['operator_name'] . "</td>";
echo "<td>" . $row['zone'] . "</td>";
echo "<td>" . $row['nas_ip'] . "</td>";
echo "<td>" . $row['switch_name'] . "</td>";
echo "<td>" . $row['switch_ip'] . "</td>";
echo "<td>" . $row['switch_port'] . "</td>";
echo "<td>" . $row['connected_port'] . "</td>";
echo "<td style='color:white;background-color:".($row['status'] == 'ACTIVE'?'green':'grey').";'>" . $row['status'] . "</td>";
echo "<td>";
echo "<a href='readr.php?id=". $row['id'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "<a href='updater.php?id=". $row['id'] ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='deleter.php?id=". $row['id'] ."' data-toggle='modal' data-target='#exampleModalLong' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " .
mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>

Sum 1 to a value by clicking an image - PHP

I guys, I need yo help for this. I have this code, wich shows me the products I have in my shopping cart:
for($i=0;$i<count($_SESSION['productos']);$i++)
{
$id = $_SESSION['productos'][$i];
$prods = mysql_query('SELECT * from productos where idprod='.$id.'');
$row = mysql_fetch_array($prods);
echo "<tr>";
echo "<td>" . $row['nombre']; echo "</td>";
if($_GET[action]=="suma")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] + 1;
}
elseif($_GET[action]=="resta")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] - 1;
}
echo "<td><input name=".$i." type='text' value=" . $_SESSION['unidades'][$i]; echo " size='5'/></td>";
echo "<td><a href='carro_detalle.php?action=suma'><img src='images/flecharriba.png' width='10x' height='10px'/></a></td>";
echo "<td><a href='carro_detalle.php?action=resta'><img src='images/flechabajo.png' width='10px' height='10px'/></a></td>";
echo "<td>" . $row['precio']; echo "</td>";
echo "<td>" . $row['precio'] * $_SESSION['unidades'][$i]; echo "</td>";
echo "</tr>";
}
I need to sum 1 unity or rest 1 unity to $_SESSION['unidades'][$i] just to the selected product, when I click the two images respectively.
The thing is that when I click, it adds me 1 to all the products. Any easy way of doing this without using $_GET vars? I dont have high knowledge about JavasScript.
Thanks!!
You probably should remove your condition from the loop and put it outside of it:
if($_GET[action]=="suma")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] + 1;
}
elseif($_GET[action]=="resta")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] - 1;
}
for($i=0;$i<count($_SESSION['productos']);$i++)
{
$id = $_SESSION['productos'][$i];
$prods = mysql_query('SELECT * from productos where idprod='.$id.'');
$row = mysql_fetch_array($prods);
echo "<tr>";
echo "<td>" . $row['nombre']; echo "</td>";
echo "<td><input name=".$i." type='text' value=" . $_SESSION['unidades'][$i]; echo " size='5'/></td>";
echo "<td><a href='carro_detalle.php?action=suma'><img src='images/flecharriba.png' width='10x' height='10px'/></a></td>";
echo "<td><a href='carro_detalle.php?action=resta'><img src='images/flechabajo.png' width='10px' height='10px'/></a></td>";
echo "<td>" . $row['precio']; echo "</td>";
echo "<td>" . $row['precio'] * $_SESSION['unidades'][$i]; echo "</td>";
echo "</tr>";
}

Remove value on click

I want to create a javascript that will change the value of anotif to 0 once clicked:
Approved:<?php if($anotif<1){
echo 0;
} else {
echo '<a class="anotif" onClick="validator()" href="?anotif">'.$anotif.'</a>';
} ?><br/>
But I am not sure how do that, considering that I'm so new in javascript. I've googled around a bit but I wasn't able to find the answer.
function validator(){
if(document.anotif.clicked){ // I'm not really sure if this is correct.
// if clicked, change value to zero
}
To be more precise, I want it to look like this once clicked:
From:
Approved:3
To:
Approved:0
Approved:<?php if($anotif<1){
echo 0;
} else {
echo '<a class="anotif" onClick="validator()" href="?anotif">'.$anotif.'</a>';
} ?><br/>
This code will lead me to ?anotif for me to view some data from the database in a table. Once approved: is clicked, it must be set to zero at the same time I move to the other page.
The problem is it requires refreshing for it to be set to 0 by the system.
Here is the rest of the code:
$firstname = getuserfield('txtFname');
$lastname = getuserfield('txtLname');
echo 'Hello '.$firstname.' '.$lastname.'.<br/>';
$anotif = 0;
$dnotif = 0;
$anotif = $anotif + getuserfield('approved_notif');
$dnotif = $dnotif + getuserfield('disapproved_notif');
?>
<h3>Notifications:</h3>
Approved:<?php if($anotif<1){echo 0;} else { echo '<a class="anotif" onClick="validator(this);" href="?anotif">'.$anotif.'</a>';}?><br/> //problem is over here
<?php
if(isset($_GET['anotif'])) {
$query = "SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 1 AND checked = 0";
$query_run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
echo "<table border=1>
<tr>
<th>Type of Leave</th>
<th>Specific Reason</th>
<th>Date From</th>
<th>Date To</th>
<th>Number of Days</th>
</tr>";
echo "<tr>";
while($record = mysql_fetch_array($query_run)){
$leave_id = $record['leave_id'];
echo "<td>" . $record['type_of_leave'] . "</td>";
echo "<td>" . $record['specific_reason'] . "</td>";
echo "<td>" . $record['date_from'] . "</td>";
echo "<td>" . $record['date_to'] . "</td>";
echo "<td>" . $record['num_of_days'] . "</td>";
echo "</tr>";
$query2 = "UPDATE hrf_leave SET checked=1 WHERE leave_id = $leave_id";
if($query_run2 = mysql_query($query2)){
$anotif = 0;
$query3 = "UPDATE hrms_emp_info SET approved_notif=$anotif WHERE empid = $empid";
if($query_run3 = mysql_query($query3)){
}
}
}
echo "</table>";
}
?>
Disapproved:<?php if($dnotif<1){echo 0;} else { echo ''.$dnotif.'<br/>';} ?> //and here
<?php
if(isset($_GET['dnotif'])) {
$query = "SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 2 AND checked=0";
$query_run = mysql_query($query);
echo "<table border=1>
<tr>
<th>Type of Leave</th>
<th>Specific Reason</th>
<th>Date From</th>
<th>Date To</th>
<th>Number of Days</th>
</tr>";
echo "<tr>";
while($record = mysql_fetch_array($query_run)){
$leave_id = $record['leave_id'];
echo "<td>" . $record['type_of_leave'] . "</td>";
echo "<td>" . $record['specific_reason'] . "</td>";
echo "<td>" . $record['date_from'] . "</td>";
echo "<td>" . $record['date_to'] . "</td>";
echo "<td>" . $record['num_of_days'] . "</td>";
echo "</tr>";
$query2 = "UPDATE hrf_leave SET checked=1 WHERE leave_id = $leave_id";
if($query_run2 = mysql_query($query2)){
$dnotif = $dnotif-1;
$query3 = "UPDATE hrms_emp_info SET disapproved_notif=$dnotif WHERE empid = $empid";
if($query_run3 = mysql_query($query3)){
}
}
}
echo "</table>";
}
You could change your anchor a bit like this:
<a class="anotif" onClick="return validator(this)" href="?anotif">
Then change the validator() function like this:
function validator(element)
{
// element is clicked already, so you can change its value
element.innerHTML = '0';
// prevent click from changing page
return false;
}
However, the real problem here is that the page will be changed to ?anotif and whatever you changed will be wasted effort.
Updated the code with a way to prevent the click from changing the page and just reducing the number to '0'.

Categories

Resources