how to get value of each table row using jquery - php

Hello i have these code.
<div class="col-md-6"> <br><br>
<table class="table">
<thead>
<th> Subject </th>
<th> Schedule </th>
<th> Day </th>
<th> Slots </th>
<th> Sections </th>
<th> Action </th>
</thead>
<tbody>
<?php
foreach($Subjects as $row)
{
$course = $this->session->userdata('course');
if($course == 'BSITWMA' || $course == 'BSCSSE' || $course == 'BSITDA' || $course == 'BSITGDD')
{
if($row->ite == '1' ) {
echo '<tr>';
echo '<td id="subj_code" name="subj_code">' .$row->subject_code. '</td>';
echo '<td id="sched_time" name="sched_time">' .$row->schedule_timestart. ' - ' .$row->schedule_timeend.' </td>';
echo '<td id="day" name="day">' .$row->schedule_day. '</td>';
echo '<td id="slot" name="slot">' .$row->slots. '</td>';
echo '<td id="sect_code" name="sect_code">' .$row->section_code. '</td>';
echo '<td > <button id = "btn-add" class="btn" > Reserve </button > </td>';
echo '</tr>';
}
}
else {
if($row->coe == '1' ) {
echo '<tr>';
echo '<td id="subj_code" name="subj_code">' .$row->subject_code. '</td>';
echo '<td id="sched_time" name="sched_time">' .$row->schedule_timestart. ' - ' .$row->schedule_timeend.' </td>';
echo '<td id="day" name="day">' .$row->schedule_day. '</td>';
echo '<td id="slot" name="slot">' .$row->slots. '</td>';
echo '<td id="sect_code" name="sect_code">' .$row->section_code. '</td>';
echo '<td > <button id = "btn-add" class="btn" > Reserve </button > </td>';
echo '</tr>';
}
}
}
?>
</tbody>
</table>
</div>
I have updated my code and this is what it looks like now.
what i want is to when i press the button. i will retrieve all the values and use it to insert into the database.
i dont have any code yet for the retrieving of value part.
i have the jquery part like this, and i still dont have any idea how to get the values in each TD.
$('button').on('click',function() {
var rowCount = $('#mhTable >tbody >tr').length;
});

You could get the ancestor <td> element of the button and get the values by accessing the text element of the sibling <td> elements using the jQuery .each() function.
The code should look something like this:
$(document).ready(function() {
$("#btn-add").on('click', function() {
var values = [];
$(this).parent('td').siblings().each(function() {
values.push($(this).text());
});
console.log(values);
});
});
Here I pass the values to an array for simplicity but you can pass these values using ajax or whatever other method you use to send values to the database.
Here is a fiddle with this example in practise

Why not do something like this for each of your variables within your foreach?
$subject_code = $row->subject_code;
and then you can use that variable for whatever you choose (such as your query)

Related

How to select all option for search to show in the below table?

I am creating the form to select all option to show in the below table, I want to search when I am selected the option show in the table. My part to do is let me to onchange "Transaction Type" then show the below table. How to function can let me to select all option to show in the below table? Hope anyone can give me example or edit in my coding to guide me how to do it. I want the output can follow my select date range and transaction type to show the table.
transaction_history file to show the frontend create the form and pass the data to the backend.
<?php
$system_user_type = $user_type;
$lang = $_COOKIE["Language"];
$new_cur_date = date("d-m-Y", strtotime($cur_date));
require_once("language/lang_transaction_" . $lang . ".php");
?>
<html>
<head>
</head>
<body>
<div class="row">
<div class="col-lg-12">
<div class="box form-group">
<header>
<h5><?php echo $language["LIST_TITLE1"]; ?></h5>
<!-- .toolbar -->
<div class="toolbar">
<nav style="padding: 8px;">
<a href="javascript:;" class="btn btn-default btn-xs collapse-box">
<i class="fa fa-minus"></i>
</a>
</nav>
</div><!-- /.toolbar -->
</header>
<section class="content">
<div class="col-lg-12 form-group" >
<label for="text1" class="form-group control-label col-lg-2"><?php echo $language['type']; ?>:</label>
<div class="col-lg-2">
<select id="select_type" class="form-group form-control required" onchange="show_table();">
<option value="transfer" selected><?php echo $language["transfer"]; ?></option>
<option value="withdraw"><?php echo $language["withdraw"]; ?></option>
<option value="upgrade"><?php echo $language["upgrade"]; ?></option>
<option value="register">Register</option>
<option value="receive"><?php echo $language["receive"]; ?></option>
</select>
</div>
<div class="col-lg-8"></div>
</div>
<div class="col-lg-12 form-group">
<label for="text1" class="form-group control-label col-lg-2">Date Range:</label>
<div class="col-lg-2">
<?php echo custom_period_opt(); ?>
</div>
<label for="text1" class="form-group control-label col-lg-2">Date Created</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="start_date" name="start_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
<label for="text1" class="form-group control-label col-lg-2">To</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="end_date" name="end_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
</div>
<div class="col-lg-12" style="text-align:center; padding-bottom:10px; padding-top:10px;">
<button id="search_button" type="button" class="btn btn-sm btn-primary" onclick="search2_('search', 'bill_table', 'billing');">Search</button>
<button id="clear" type="button" class="btn btn-sm btn-default" onclick="clearData()">Clear</button>
</div>
<div class="" id="table_result">
</div>
</section>
</div>
</div>
</div>
</body>
</html>
<script>
$(function() {
show_table();
$("#select_type").on("change", function() {
show_table();
});
});
function show_table() {
//alert(123);
var select_type = $("#select_type").val();
$.ajax({
url: "?f=transaction_table",
type: "POST",
data: {
select_type: select_type
},
before_send: function() {
show_overLay();
//$('#patient_result').html('');
},
success: function(data) {
hide_overLay('');
//alert(data);
if (data) {
$("#table_result").html("");
$("#table_result").append(data);
//
$('.dataTable').dataTable();
} else {
alert("Please fill in the field.");
}
}
});
}
</script>
<style>
.myClass
{
display: none;
}
</style>
transaction_table file to show the backend function send to frontend page.Below is my coding:
<?php
$select_type = $_POST['select_type'];
if ($select_type == "withdraw") {
echo '<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>';
$i = 1;
$select_transfer = 'SELECT * FROM withdrawal_record bp WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['withdraw_amount'] . '</td>';
echo '</tr>';
}
echo " </tbody>
</table>";
}elseif ($select_type == "transfer") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>To Type</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM transfer_history_bp WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
if($rs_transfer['point_type']=="2"){
$to_type="Register Point";
}elseif($rs_transfer['point_type']=="3"){
$to_type="Entertainment Point";
}elseif($rs_transfer['point_type']=="4"){
$to_type="Business Point";
}
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $to_type . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "upgrade") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM upgrade_point WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "register") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM transfer_history_rp WHERE user_id = ' . $user_id . ' and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05" and use_type=1 ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "receive") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM extra_point WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}
?>
Below is my output, this output just can follow what I select the transaction type to onchange to show in the table:
Below is my database information:
If I understand your question correctly, then you need to include these additional parameters in your request as you already to with select_type.
First, get the additional values in theshowTable function, also add them to the request data:
function show_table() {
var select_type = $("#select_type").val();
var start_date = $("#start_date").val();
var end_date = $("#end_date").val();
$.ajax({
url: "?f=transaction_table",
type: "POST",
data: {
select_type: select_type,
start_date: start_date,
end_date: end_date
},
// ...
}
then, receive the values in your PHP file like you already do with select_type:
<?php
$select_type = $_POST['select_type'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
// ...
Finally, you have to adjust your queries accordingly. As you already have and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05" in your queries, it's just a matter of replacing the hardcoded dates with the ones you received. Do not forget to sanitize your input dates! If possible use prepared statements.
Note 1: Your script and style tags are outside of the html part of your site. This is not valid. These tags belong into either the head or the body tag.
Note 2: There are whitespaces before your opening html tag. You may wan't to avoid this too, since it can trigger IE into quirks mode. Simply change this
?>
<html>
to this
?><html>

PHP foreach using $variable->property

I have some code that runs reports from $results returning from mysql query.
The queries are relatively standard, but their total number is dynamic. So far I have static HTML where it has the same code repeated, but thats only if ok the number of queries == the number of markup repetitions.
So, I am trying to implement a new foreach loop that will determine how many "queries" there are, and only markup for each one, and then $total at the end.
To date I have had static php foreach loop like this, works well.
<div class="row">
<div class="col-xs-12">
<table class="table table-striped">
<thead>
<tr>
<th>Year</th>
<th>Month</th>
<th>Item</th>
<th class="text-right">Minimum</th>
<th class="text-right">Maximum</th>
<th class="text-right">Change</th>
</tr>
</thead>
<tbody>
<?php foreach ($results as $result) :
if ($result->id == 151){
echo '<tr>';
echo '<td>', $result->Year, '</td>';
echo '<td>', $result->Month, '</td>';
echo '<td class="text-centre">', $result->name, '</td>';
echo '<td class="text-right">', $result->min, '</td>';
echo '<td class="text-right">', $result->max, '</td>';
echo '<td class="text-right">', $result->cumulative, '</td>';
$thirdtotal += $result->change;
$total += $result->change;
echo '</tr>';
}
endforeach; ?>
Where I am stuck is, do i put the above inside another for each loop? My thought is, if a query exists, it will have an "id" field in the array, so for the number of "id", thats how many outputs you'll get.
<?php foreach ($results as $key => $value;) :
{
echo '<tr>';
echo '<td>' $value, '</td>';
echo '</tr>';
}
endforeach; ?>
This piece of test code doesnt work, am I using the key value correctly? Is it the correct approach to solve this problem and reduce the amount of php in the controller?
Thanks

PHP Echo Table Column Alignment

I have this code that works fine in other pages that I made but doesn't work properly on my summary page.
<?php
//AAFES-date1
$sqlAAFES1 = "SELECT * FROM aafes WHERE dueDate ='$date1'";
$qAAFES1 = $pdo->prepare($sqlAAFES1);
$qAAFES1->execute(array($date1));
$dataAAFES1 = $qAAFES1->fetch(PDO::FETCH_ASSOC);
if ($dataAAFES1){
echo '<table class="table table-condensed table-hover">';
echo '<tr>';
foreach ($pdo->query($sqlAAFES1) as $rowAAFES1){
echo '<td width="60%">'.$rowAAFES1['facilityName'].'</td>';
echo '<td style="text-align:right" width="40%">'.$rowAAFES1['totalQty'].'</td>';
echo '</tr>';
echo '</table>';
};
};
?>
as you can see the 2nd row doesn't align with the first row.
while on the other page, I used the same foreach code, but alignment is perfect. So I wanna ask what seems to be the problem with this one.
Don't close foreach loop after table. Put <tr></tr> inside foreach loop.
if ($dataAAFES1){
echo '<table class="table table-condensed table-hover">';
foreach ($pdo->query($sqlAAFES1) as $rowAAFES1){
echo '<tr>';
echo '<td width="60%">'.$rowAAFES1['facilityName'].'</td>';
echo '<td style="text-align:right" width="40%">'.$rowAAFES1['totalQty'].'</td>';
echo '</tr>';
};
echo '</table>';
};

Adding dynamic data to a table using php

Database schema looks like
ID Link_name Description
and wish The link name from the database to load in the table as
-----------------------------
Link_name1 | Link_name2 |
------------------------------
-----------------------------
Link_name3 | Link_name4 |
------------------------------
But the code
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
require('config.php');
$datae = mysql_query("SELECT * FROM `products` ORDER BY id DESC")or die(mysql_error()); while($infoe = mysql_fetch_array( $datae )) {?>
<tr>
<td width="7%"> </td>
<td width="93%" class="main_text">
<h3><?php echo $infoe['Link_name']; }?><hr><br></h3>
</td>
</tr>
</table>
Output as
--------------
Link_name1 |
--------------
---------------
Link_name2 |
--------------
Not sure what are you asking for, but just a guess:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
require('config.php');
$datae = mysql_query("SELECT * FROM `products` ORDER BY id DESC")or die(mysql_error());
$leftRight = 'left';
while($infoe = mysql_fetch_array( $datae )) {
if ( $leftRight == 'left') {
echo '<tr><td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$leftRight = 'right';
} else {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td></tr>';
$leftRight = 'left';
}
}
if ( $leftRight == 'right') echo '<td></td><td></td></tr>'; ?>
</table>
EDIT If you need to format N column table:
$current = 1;
$columnLimit = 3; //you can set any number of column to output
while($infoe = mysql_fetch_array( $datae )) {
if ( $current == 1) {
echo '<tr><td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$current++;
} elseif ( $current == $columnLimit) { {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td></tr>';
$current = 1;
} else {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$current++;
}
}
while ( $current!=1 && $current <= $columnLimit) {
if ($current != $columnLimit)
echo '<td></td>';
else
echo '<td></td></tr>';
$current++;
}
?>
Use the modulus operator (http://php.net/manual/de/language.operators.arithmetic.php)
With that you know if your current data entry is a odd or even number. so you can say something like: If your a even dataset then close the current row and open a new one.

jquery highlight() breaking in dynamic table

I've got a little issue with a highlight function I'm working on. I basically load records out of a database that match the current form data in certain ways. Then, when someone is filling in the form, if they are describing an issue that already exists in my system, it will highlight words that their description has in common with the existing record(s). My issue is that the table breaks. It will work to a certain extent, but sometimes it breaks the PHP loop portion out of the rest of the table, and it then has no formatting, and the highlighting function will not work. To be more specific, once broken, the td tags in the body of the table do not follow the formatting of the header row.
Conditions that cause the undesirable effect:
tabbing through the text area
If too many classes have to be removed or applied at once (via deleting all, adding many words or deleting or searching for a single character with many occurrences)
html on the main page && script to trigger the highlighting
<textarea name="description" id="description"></textarea>
<script>
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$(function(){
$("#description").keydown(function(){
delay((function(){
$("#displayer *").removeClass('highlight');
var1 = $('textarea#description').val().split(' ');
for (var i=0;i<var1.length;i++){
$("#displayer *").highlight(var1[i], "highlight")};
}),1000);
});
});
</script>
the external php that builds the searched table based on an ajax call is this:
echo '<TABLE BORDER="0" CELLSPACING="5" CELLPADDING="5" id="displayer"><FONT FACE="ARIAL">';
echo ' <tr> ';
echo ' <td width="20" ALIGN="LEFT" height="1">ID</td>';
echo ' <td width="89" ALIGN="LEFT" height="1">Date</td> ';
echo ' <td width="200" ALIGN="LEFT" height="1" >Description</td>';
echo ' <td width="89" ALIGN="LEFT" height="1" >Solution</td>';
echo ' <td width="20" ALIGN="LEFT" height="1" >User:</td>';
echo ' <td ALIGN="LEFT" height="1" >Key?:</td>';
echo ' <td ALIGN="LEFT" height="1" >Part:</td>';
echo ' <td ALIGN="LEFT" height="1" >Classified As:</td>';
echo ' </tr> ';
for ($i=1; $i <= $num_results; $i++)
{
$row = mysql_fetch_array($result1);
echo '<TR BGCOLOR="#EFEFEF">';
echo '<TD width="20">';
echo stripslashes($row['0']) ;
echo '</TD>';
echo '<TD width="89" >';
echo $row['1'] ;
echo '</TD>';
echo '<TD width="200">';
echo stripslashes($row['6']) ;
echo '</TD>';
echo '<TD width="89">';
echo stripslashes($row['11']) ;
echo '</TD>';
echo '<TD width="20">';
echo $row['5'] ;
echo '</TD>';
echo '<TD>';
if ($row['8'] == 1)
{echo 'Yes' ;}
else
{echo 'No' ;}
echo '</TD>';
echo '<td>'.$row['10'].'</td>';
echo '<td>'.$row['9'].'</td>';
echo '</TR>';
}
echo '</TABLE>';
external highlight plugin:
jQuery.fn.highlight = function (str, className) {
var regex = new RegExp(str, "gi");
return this.each(function () {
$(this).contents().filter(function() {
return this.nodeType == 3 && regex.test(this.nodeValue);
}).replaceWith(function() {
return (this.nodeValue || "").replace(regex, function(match) {
return "<span class=\"" + className + "\">" + match + "</span>";
});
});
});
};
I think I should add a test for empty with some kind of escape, to solve the first condition, but with the second, I'm not sure what's happening. Any suggestions are definitely appreciated. Sorry for the post being huge and convoluted, but I wanted everyone to have all the information I could provide.
$(function(){
$("#description").keydown(function(){
delay((function(){
var divClone = $("#disp_hidden .serial_display").clone();
$("#displayer .serial_display").replaceWith(divClone);
if ($.trim($('textarea#description').val()) != ''){
var1 = $('textarea#description').val().trim().split(' ');
for (var i=0;i<var1.length;i++){
$("#displayer *").highlight(var1[i], "highlight")};
};
}),1000);
});
});
hidden table clone, replaces at new edit, fixed.

Categories