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++;
}
Related
Hi I have shopping cart and i want print item name in different php file. I try this.
<?php
if(!empty($_SESSION["cart"]))
{
$total = 0;
foreach($_SESSION["cart"] as $keys => $values)
{
$product_cart = $values["item_name"];
?>
<tr>
<td><?php echo $values["item_name"]; ?></td> <?php session_start(); $_SESSION['produktik'] = $values["item_name"] ; ?>
<td><?php echo $values["item_quantity"] ?></td>
<td><?php echo $values["product_price"]; ?>€</td>
<td><?php echo number_format($values["item_quantity"] * $values["product_price"], 2); ?>€</td>
<td><span class="text-danger">X</span></td>
</tr>
and second php file is
<pre>
<?php
session_start();
echo $_SESSION['produktik'];
?>
</pre>
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;
}
}
?>
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();}
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" );
});
});
I am programming a script to output sorted information grabbed from the PubMed Central API. My issue is that I would like to pull the first and second authors as well as the first an second keywords (for the purposes of this question, we can focus on just one). A good example of this is at $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'}->contrib->name->surname. I can, obviously, get the first name to print.
It is my understanding from reading other posts that I need a foreach loop to achieve what I desire. I do not, however, understand how to implement this in this context. My code is as follows:
<?php
$PMCID = 3545513;
$url = 'http://www.pubmedcentral.nih.gov/oai/oai.cgi?verb=GetRecord&identifier=oai:pubmedcentral.nih.gov:'.$PMCID.'&metadataPrefix=pmc_fm';
$xml = new SimpleXMLElement(file_get_contents($url));
?>
<table>
<tr>
<td>Journal Title</td><td>Year</td><td>Issue</td><td>NOC_Country</td><td>State</td><td>City</td><td>Primary Institution</td><td>Secondary Institution</td><td>First Author</td><td>Second Author</td><td>Topic</td><td>Target Behavior 1</td><td>Target Behavior 2</td><td>Population</td><td>Paper</td><td>Status</td>
</tr>
<tr>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'journal-meta'}->{'journal-title-group'}->{'journal-title'};?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'pub-date'}->year;?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->issue;?></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'}->contrib->name->surname;?>, <?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'}->contrib->name->{'given-names'};?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'}->contrib->name->surname;?>, <?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'}->contrib->name->{'given-names'};?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'title-group'}->{'article-title'};?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'kwd-group'}->kwd;?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'kwd-group'}->kwd;?></td>
<td></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'article-categories'}->{'subj-group'}->subject;?></td>
<td></td>
</tr>
</table>
Any help would be greatly appreciated!
Ended up with:
<table>
<tr>
<td><strong>Journal Title</strong></td><td><strong>Year</strong></td><td><strong>Issue</strong></td><td><strong>First Author</strong></td><td><strong>Second Author</strong></td><td><strong>Topic</strong></td><td><strong>Target Behavior 1</strong></td><td><strong>Target Behavior 2</strong></td><td><strong>Paper</strong></td>
</tr>
<?php
$PMCID = $_REQUEST['PMCID'];
$url = 'http://www.pubmedcentral.nih.gov/oai/oai.cgi?verb=GetRecord&identifier=oai:pubmedcentral.nih.gov:'.$PMCID.'&metadataPrefix=pmc_fm';
$xml = new SimpleXMLElement(file_get_contents($url));
?>
<tr>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'journal-meta'}->{'journal-title-group'}->{'journal-title'};?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'pub-date'}->year;?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->issue;?></td>
<?php
$n=0;
foreach($xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'} as $author){
echo "<td>" . $author->contrib->name->surname . ", ";
echo $author->contrib->name->{'given-names'} . "</td>";
$n++;
if($n==2) break;
}
unset($n);
unset($author);
?>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'title-group'}->{'article-title'};?></td>
<?php
$i=0;
foreach($xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'kwd-group'}->kwd as $keyword){
echo "<td>" . $keyword . "</td>";
$i++;
if($i==2) break;
}
unset($i);
unset($keyword);
?>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'article-categories'}->{'subj-group'}->subject;?></td>
</tr>
</table>