Select a Row from <?php foreach : ?> - php

I'm trying to select a particular row using jquery from a looped foreach. For now I'm just using a simple alert to see if I get the right row. Problem is, it only selects the last row, and not the row I clicked.
Example code:
<?php foreach ($customers as $c) : ?>
<tr>
<td><?php echo $count++; ?></td>
<td><?php echo $c['firstname'] . " " . $c['lastname']; ?></td>
<td><?php echo $c['phone']; ?></td>
<td><?php echo $c['email']; ?></td>
<td style="display:none" id="'<?php echo $c['id'] ?>'"> </td>
</tr>
<?php endforeach ?>
The Jquery call:
$('tr td ').click(function(){
var fn = '<?php echo $c['firstname']; ?>';
alert(fn);}
);

This will output all td values. Credits
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
$count = 0;
$customers = array (
array('firstname'=>'fname1', 'lastname'=>'lname1','phone'=>'111','email'=>'a#gmail.com','id'=>1),
array('firstname'=>'fname2', 'lastname'=>'lname2','phone'=>'222','email'=>'b#gmail.com','id'=>2),
array('firstname'=>'fname3', 'lastname'=>'lname3','phone'=>'333','email'=>'c#gmail.com','id'=>3),
array('firstname'=>'fname4', 'lastname'=>'lname4','phone'=>'444','email'=>'d#gmail.com','id'=>4),
);
?>
<table class="table table-bordered">
<?php foreach ($customers as $c) : ?>
<tr class="getdetails">
<td><?php echo $count++; ?></td>
<td><?php echo $c['firstname'] . " " . $c['lastname']; ?></td>
<td><?php echo $c['phone']; ?></td>
<td><?php echo $c['email']; ?></td>
<td style="display:none" id="'<?php echo $c['id'] ?>'"> </td>
</tr>
<?php endforeach ?>
</table>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$(".getdetails").click(function(){
$(this).find("td").each(function(){
console.log($(this).html());
});
});
</script>
</body>
</html>

You can access particular row's column like bellow
$('tr.item').each(function() { // selected tr
var col1 = $(this).find("td:eq(0) > input").val(); // find the input field value inside the first column of the row
var col2 = $(this).find("td:eq(1) > input").val();}

Related

PHP Export to excel from MYSQL

I'm trying to get my MySQL data to Excel file, but I'm having problems with Excel cells. All my text goes to one cell, I would like to have each row value in separate Excel cell. Here is my code:
<html xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns="http://www.w3.org/TR/REC-html40">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta charset="UTF-8">
<title>Daily Log</title>
</head>
<body>
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$strKeyword = null;
if (isset($_POST["txtKeyword"])) {
$strKeyword = $_POST["txtKeyword"];
}
?>
<form name="frmSearch" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<table>
<tr>
<th>Date
<input name="txtKeyword" type="month" id="txtKeyword" value="<?php echo $strKeyword;?>">
<input type="submit" value="Search"></th>
<input type="submit" value="Export"></th>
</tr>
<tr>
<th><label>Date</label></th>
<th><label>Requested</label></th>
<th><label>Requested Time</label></th>
<th><label>Location</label></th>
<th><label>Description</label></th>
<th><label>Order By</label></th>
<th><label>Completed by</label></th>
<th><label>Completed Time</label></th>
</tr>
<tr><!--/*DESC ASP*/-->
<?php include('config.php');
$filename = "excelfilename";
$strsql = "select * from log WHERE dateorder LIKE '%".$strKeyword."%'";
$result = mysqli_query($objConnect, $strsql);
$strExcelFileName="Member-All.xls";
header("Content-Type: application/x-msexcel; name=\"$strExcelFileName\"");
header("Content-Disposition: inline; filename=\"$strExcelFileName\"");
header("Pragma:no-cache");
while ($rs = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
?>
<td><?php echo $rs['dateorder'] ?></td>
<td><?php echo $rs['request'] ?></td>
<td><?php echo date("H:i", strtotime($rs['requesttime'])) ?></td>
<td><?php echo $rs['location'] ?></td>
<td><?php echo $rs['description'] ?></td>
<td><?php echo $rs['orderby'] ?></td>
<td><?php echo $rs['completed'] ?></td>
<td><?php echo date("H:i", strtotime($rs['completedtime'])) ?></td>
</tr>
<?php } ?>
</table>
</form>
<script>
window.onbeforeunload = function(){return false;};
setTimeout(function(){window.close();}, 10000);
</script>
</body>
</html>
I need Step 1 search button step 2 export button
Consider separating the HTML output and Excel export using the same PHP array fetched from MySQL query. Integrate below pieces into your code.
Specifically, Excel requires tab-delimited text to map to cells with carriage and line breaks.
Database Fetch
...same db config...
$data = []
while ($rs = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
$data[] = $row;
}
HTML Output
foreach ($data as $row) { ?>
<tr>
<td><?php echo $row['dateorder'] ?></td>
<td><?php echo $row['request'] ?></td>
<td><?php echo date("H:i", strtotime($row['requesttime'])) ?></td>
<td><?php echo $row['location'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['orderby'] ?></td>
<td><?php echo $row['completed'] ?></td>
<td><?php echo date("H:i", strtotime($row['completedtime'])) ?></td>
</tr>
<?php } ?>
Excel Export
$strExcelFileName="Member-All.xls";
header("Content-Type: application/x-msexcel; name=\"$strExcelFileName\"");
header("Content-Disposition: attachment; filename=\"$strExcelFileName\"");
header("Pragma:no-cache");
$i = 1;
foreach ($data as $row) {
if ($i == 1) {
// COLUMN HEADERS
echo implode("\t", array_keys($row)) . "\r\n";
}
// DATA ROWS
echo implode("\t", array_values($row)) . "\r\n";
$i++;
}

php code to break page after 6th row

I try to display 2 table side by side with php code....
but i need to display only 6 table on single page ......and rest on another page......
so plz can any one help me to break page or break loop after 6th iteration..... 7th table display on another page like wise...
plz see my image attached below....i am facing problem on print preview...
page breaks my table during print...like below image...
I attached croped imaged here...
my page actuaally display 8 tables on single page...but i need is only 6 on one page.
below is my code..
<?php if (is_array($data)) { foreach($data as $row) { ?>
<table border="1px solid #666" summary="" width="48%" class="pos_fixed1">
<thead>
<tr>
<td colspan="4">Dainik Bhaskar Nagpur</td>
</tr>
<tr>
<td>Receipt</td>
<td><?php echo htmlspecialchars($row['receipt_no']); ?></td>
<td>Coupon</td>
<td><?php echo htmlspecialchars($row['coupon']); ?></td>
</tr>
<tr>
<td>Receipt Date</td>
<td><?php echo htmlspecialchars($row['bookingdate']); ?></td>
<td>Coupon Date</td>
<td><?php echo htmlspecialchars($row['coupondate']); ?></td>
</tr>
<tr>
<td>Copy Start Date</td>
<td><?php echo htmlspecialchars($row['startingdate']); ?></td>
<td>HawkerName</td>
<td><?php echo htmlspecialchars($row['hawkername']); ?></td>
</tr>
<tr>
<td>SubagentName</td>
<td><?php echo htmlspecialchars($row['subagentname']); ?></td>
<td>CenterName</td>
<td><?php echo htmlspecialchars($row['ward']); ?></td>
</tr>
<tr>
<td>customer</td>
<td><?php echo htmlspecialchars($row['customer_name']); ?></td>
<td>Address</td>
<td><?php echo htmlspecialchars($row['society']); ?></td>
</tr>
</thead>
</table>
<?php } }?>
Try using CSS:
<style>
#media print {
.pageBreak {
page-break-after: always;
}
}
</style>
And at every 6 table, add a pageBreak:
<?php
$lineCounter = 0;
if (is_array($data)) {
foreach($data as $row) {
$lineCounter++;
?>
<!-- output a table... -->
<?php
if($lineCounter % 6 == 0) {
echo '<span class="pageBreak"></span>' . PHP_EOL;
}
}
}
?>
<?php
// get total records
TotalNoOfRecords = count($data);
$Count = 1;
foreach($data as $row) {
// write here your content
// break page after 6 rows
if($Count % 6 == 0 && $Count != $TotalNoOfRecords)
{
echo "<p style='page-break-after:always'></p>";
}
$Count++;
}
?>
try this is code or visit link
<?php
$q = "SELECT * FROM your_table ";
$myq = mysqli_query($link, $q);
$fixtures ='';
$i=0;
while($row=mysqli_fetch_assoc($myq)) {
$r[]=$row;
}
foreach ($r as $val) {
$i++;
?>
<!-- your value from database -->
<table>
<tr>
<td><?php echo $val['your_column']; ?></td>
</tr>
</table>
<!-- your value from database -->
<?php
if($i % 6==0){
echo '<div style="page-break-after: always;">[------ break ------]</div>' . PHP_EOL;
$i=0;
}
}
?>

Iframe not working for multiple rows

I am trying to automate the hallticket process for an institution. This includes a certain verification process.
Iframe is working only for the first row, what should I do to open it for multiple rows based on the row selection
php code
<?php
while($row = mysqli_fetch_assoc($rows)) //Retriving the rows from the database
{
?>
<td><?php echo $app_id ?></td>
<td><?php echo $first_name ?></td>
<td><?php echo $last_name ?></td>
<td><?php echo $email_id ?></td>
<td><?php echo $mobile_number ?></td>
<td><?php echo $dd_number ?></td>
<td><?php echo $course_name ?></td>
<?php
if($dd_submit==1 && $ht_sent==0)
{?>
<td class="verified"><span>Verified</span></td>
<?php }elseif($dd_submit==0 && $ht_sent==0){ ?>
<td class="non_verified"><span>None Verified</span></td>
<?php }else{ ?> <td class="Pending"><span>Hall Ticket Sent</span></td>
<?php } ?>
<!-- I think the problem is here the java script is not repeating -->
<td><button id="dialog" name="verify" value="<?php echo $row['app_id'] ?>" > <img src="images/success_tick.png"></button><img src="images/meassage_table.png"><img src="images/comment.png"></td>
</tr>
<?php
}
?>
<div id="dialogContent" title="DD Verification for Vinay Draksharam">
<iframe src='ddverification_page.php?app_id=<?php echo $app_id ?> '></iframe>
Java Script
<script>
$(function () {
$("#dialogContent").dialog({
autoOpen: false,
modal: true
});
$('#dialog').click(function () {
$("#dialogContent").dialog( "open" );
});
});
</script>
First problem- you have many rows but you only create one dialog. You probably have to put dialog creation inside loop to have separate dialog for each row. You also need some way to create connection between dialog and row. Best idea would probably to use $app_id as id of dialog.
What i would do:
PHP:
<?php
while($row = mysqli_fetch_assoc($rows)) //Retriving the rows from the database
{
?>
<td><?php echo $app_id ?></td>
<td><?php echo $first_name ?></td>
<td><?php echo $last_name ?></td>
<td><?php echo $email_id ?></td>
<td><?php echo $mobile_number ?></td>
<td><?php echo $dd_number ?></td>
<td><?php echo $course_name ?></td>
<?php
if($dd_submit==1 && $ht_sent==0)
{?>
<td class="verified"><span>Verified</span></td>
<?php }elseif($dd_submit==0 && $ht_sent==0){ ?>
<td class="non_verified"><span>None Verified</span></td>
<?php }else{ ?> <td class="Pending"><span>Hall Ticket Sent</span></td>
<?php } ?>
<!-- I think the problem is here the java script is not repeating -->
<td><button class="dialog" name="verify" value="<?php echo $row['app_id'] ?>" >
<div class="dialogContent" id="dialogContent_<?php echo $app_id ?>" title="DD Verification for Vinay Draksharam">
<iframe src='ddverification_page.php?app_id=<?php echo $app_id ?> '></iframe> </div>
<img src="images/success_tick.png"></button><img src="images/meassage_table.png"><img src="images/comment.png"></td>
</tr>
<?php
}
?>
And Javascript:
$(function () {
$(".dialogContent").dialog({
autoOpen: false,
modal: true
});
$('.dialog').click(function () {
$("#dialogContent_"+$(this).val()).dialog( "open" );
});
});

function add new input

I have add_person.ctp file, and there I have Inputs and function, but this function is not working, My question is why its not working, I dont see any mistake in this code. If I click button, then this doing nothing.
Here is my code
<h2>People</h2>
<table id="mytable">
<tr><th></th><th>Last Name</th><th>First Name</th><th>Email</th><th>Gender</th></tr>
<tr id="person0" style="display:none;">
<td><?php echo $this->Form->button(' - ',array('type'=>'button','title'=>'Click Here to remove this person')); ?></td>
<td><?php echo $this->Form->input('unused.lastName',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.firstName',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.email',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.gender',array('label'=>'','type'=>'select','options'=>array('M'=>'M','F'=>'F','T'=>'T'))); ?></td>
</tr>
<tr id="trAdd"><td> <?php echo $this->Form->button('+',array('type'=>'button','title'=>'Click Here to add another person','onclick'=>'addPerson()')); ?> </td><td></td><td></td><td></td><td></td></tr>
</table>
<?php echo $this->Html->script(array('jquery-2.1.1.min.js'));?>
<script type='text/javascript'>
var lastRow=0;
function addPerson() {
lastRow++;
$("#mytable tbody>tr:#person0").clone(true).attr('id','person'+lastRow).removeAttr('style').insertBefore("#mytable tbody>tr:#trAdd");
$("#person"+lastRow+" button").attr('onclick','removePerson('+lastRow+')');
$("#person"+lastRow+" input:first").attr('name','data[Person]['+lastRow+'][lastName]').attr('id','personLastName'+lastRow);
$("#person"+lastRow+" input:eq(1)").attr('name','data[Person]['+lastRow+'][firstName]').attr('id','personFirstName'+lastRow);
$("#person"+lastRow+" input:eq(2)").attr('name','data[Person]['+lastRow+'][email]').attr('id','personEmail'+lastRow);
$("#person"+lastRow+" select").attr('name','data[Person]['+lastRow+'][gender]').attr('id','personGender'+lastRow);
}
function removePerson(x) {
$("#person"+x).remove();
}
</script>
Thank you for helping !
You have wrong selector here #mytable tbody>tr:#person0 and here #mytable tbody>tr:#trAdd. Replace with #mytable tbody>tr#person0 and #mytable tbody>tr#trAdd respectively. Keep in mind there is no need to use colon symbol in selectors.

Editable Datatables PHP MySQL jQuery

I have a table which is fed dynamically from the database, this works as it should, I've then put the datatables functionality on the top which is working well, the sorting, search, the pagination is again working as it should.
I'm now looking at being able to edit a row and update the data back into the db and refresh the table.
My table structure
<table class="table table-striped table-hover table-bordered" id="tobebookedtable">
<thead>
<tr>
<th style="display:none;">PropID</th>
<th>ProjectNo</th>
<th>Householder</th>
<th>HouseNoName</th>
<th>Street Name</th>
<th>Postcode</th>
<th>Telephone</th>
<th>EPCPlanned</th>
<th>TAM</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<td style="display:none;"><?php echo $planning->PropID; ?></td>
<td><?php echo $planning->ProjectNo; ?></td>
<td><?php echo $planning->Householder; ?></td>
<td><?php echo $planning->HouseNoName; ?></td>
<td><?php echo $planning->StreetName; ?></td>
<td><?php echo $planning->Postcode; ?></td>
<td><?php echo $planning->Telephone; ?></td>
<td><?php echo $planning->EPCPlanned; ?></td>
<td><?php echo $planning->TAM; ?></td>
</tr>
<?php }; ?>
</tbody>
</table>
The EPCPlanned is the only field I want to update.
This here is my code for the jQuery side, the edit ability works, I can click into a cell and edit the record.
<script type="text/javascript">
$(document).ready(function() {
$('#tobebookedtable').dataTable( {
//"bProcessing": true,
//"bServerSide": true,
//"bJQueryUI": true,
"bPaginate": true,
"bStateSave": true
} );
/* Init DataTables */
var oTable = $('#tobebookedtable').dataTable();
/* Apply the jEditable handlers to the table */
oTable.$('td').editable( 'tobebooked_edit.php', {
"callback": function( sValue, y ) {
var aPos = oTable.fnGetPosition( this );
oTable.fnUpdate( sValue, aPos[0], aPos[1] );
//window.location.reload();
},
"submitdata": function ( value, settings ) {
console.log(value);
console.log(settings);
return {
"row_id": this.parentNode.getAttribute('id'),
"column": oTable.fnGetPosition( this )[2]
};
},
"height": "26px",
"width": "100%"
} );
} );
</script>
this is the tobebooked_edit.php
<?php
require 'core/init.php';
$table="temp_tobebooked";
$rawdata = $_POST;
$query = "show columns from $table";
$result= mysql_query($query) or die(mysql_error());
$fields = array();
while ( $row = mysql_fetch_assoc($result) )
{
$fieldname = $row['Field'];
array_push($fields, $fieldname);
}
$id = $rawdata['row_id'];
$value = $rawdata['value'];
$column = $rawdata['column'];
$column = $column + 1;
$fieldname = $fields[$column];
$value = utf8_decode($value);
$query = "update $table set $fieldname = '$value' where PropID = '$id'";
$result = mysql_query($query);
if (!$result) { echo "Update failed"; }
else { echo "UPD: $value"; }
?>
When it all runs and I update a record the field Updates on the screen and shows with the "UPD:" and the entered value.
But when I check my database there is no record updated. No errors show either
How can I get the data to update in my db?
First of all, I think that this
<tr>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
should be
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<tr>
Then, with
"row_id": this.parentNode.getAttribute('id'),
for each td element, you select the tr element and search for the id attribute, that doesn't exists
Firstly as per rsella's comment you nee to move the row inside the loop.
Secondly as suspected you are holding the ID in a hidden cell which is infact a sibling of the editable elements rather than the parent. Try changing the table structure to the following
<tbody>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<tr id="<?php echo $planning->PropID; ?>" >
<td><?php echo $planning->ProjectNo; ?></td>
<td><?php echo $planning->Householder; ?></td>
<td><?php echo $planning->HouseNoName; ?></td>
<td><?php echo $planning->StreetName; ?></td>
<td><?php echo $planning->Postcode; ?></td>
<td><?php echo $planning->Telephone; ?></td>
<td><?php echo $planning->EPCPlanned; ?></td>
<td><?php echo $planning->TAM; ?></td>
</tr>
<?php };
?>
</tbody>
This should mean that the parent has an ID and thus this.parentNode.getAttribute('id') should be able returning the required value.
As you're only editing the EPCPlanned cell then an alternative would be
<tbody>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<tr>
<td><?php echo $planning->ProjectNo; ?></td>
<td><?php echo $planning->Householder; ?></td>
<td><?php echo $planning->HouseNoName; ?></td>
<td><?php echo $planning->StreetName; ?></td>
<td><?php echo $planning->Postcode; ?></td>
<td><?php echo $planning->Telephone; ?></td>
<td id="<?php echo $planning->PropID; ?>"><?php echo $planning->EPCPlanned; ?></td>
<td><?php echo $planning->TAM; ?></td>
</tr>
<?php };
?>
</tbody>
and in your JQuery change "row_id": this.parentNode.getAttribute('id') to this.getAttribute('id')

Categories