Am trying to export my data along side images from mysql database using php.
The plain data is successfully exported to excel but the images donot display rather display error "the linked image cannot be displayed.The file may have been moved,renamed or deleted.Verify link points to correct file and location".
Output of the code
source code
<?php
// The function header by sending raw excel
header("Content-type: application/vnd-ms-excel");
// Defines the name of the export file "codelution-export.xls"
header("Content-Disposition: attachment; filename=system_users.xls");
// Add data table
include("config/dbconfig.php");
?>
<!-- Image path in subfolder images -->
<img src="images/reph.png" height="115" width="730">
<table width=70% border="1" class="view" align="center">
<tr><th> No</th>
<th> Name</th>
<th>User Role</th>
<th>User Name</th>
<th>Password</th>
</class> </tr><tr>
<?php
$sno=1;
$query=mysql_query("select * from users ");
$num_rows=mysql_num_rows($query);
if($num_rows>0)
{ while ($fields=mysql_fetch_assoc($query))
{ $sno<=$num_rows;
$pid=$fields['user_id'];
$lname=$fields['lname'];
?>
<td> <?php echo "$sno ";
$sno++; ?></td>
<td> <?php echo $fields['lname']." ".$fields['fname'];?></td>
<td><?php // php code that retrieves image path from the database and prints the image
//echo '<img width="120" height="110" SRC="' .$fields['image'] . '" alt="Image failed to load " />';
?></td>
<td> <?php echo $fields['role'];?></td>
<td> <?php echo $fields['username'];?></td>
<td> <?php echo $fields['password'];?></td>
</tr>
<?php
}//closing the while loop
}//closing the if statement
echo '</table>';
?>
Related
I am working on a php code as shown below in which I want to display the file names in separate table rows in php.
$destination = 'outgoing_folder';
$mp3_files = scandir($destination); /* Line #A */
print_r($mp3_files); /* Line #B */
Line#B print the following o/p:
Array ( [0] => . [1] => .. [2] => 36017P.mp3 [3] => 36031P.mp3 [4] => hello.mp3 )
The HTML/PHP code which I have used in order to display the content in table is:
<table width="100%">
<tr>
<!-- This inline css will go inside addiional css -->
<th style="width=8%;" >House#</th>
<th style="width=8%;">MP3</th>
<th style="width=8%;" >Program Name</th>
<th style="width=4%;">Title</th>
<th style="width=8%;" >Description</th>
<th style="width=8%;" >Program Name</th>
<th style="width=4%;" >Title</th>
<th style="width=8%;">Description</th>
</tr>
<tr>
<td style="width:8%;text-align:center;"><?php echo $mp4_files[2];?></td>
<td style="width:8%;text-align:center;"><?php echo $mp3_files[2];?></td> /* Line Z */
<td style="width:8%;"><?php echo $path_program_en[0]->value; ?> </td>
<td style="width:8%;"><?php echo $path_title_en[0]->value; ?></td>
<td style="width:8%;"><?php echo $path_description_en[0]->value; ?></td>
<td style="width:8%;"><?php echo $path_program_fr[0]->value; ?> </td>
<td style="width:8%;"><?php echo $path_title_fr[0]->value; ?></td>
<td style="width:8%;"><?php echo $path_description_fr[0]->value; ?></td>
</tr>
I have used Line #A at Line#Z to print 2nd file name from a directory in a separate table rows in php.
Problem Statement:
I am wondering what changes I need to add at Line#Z (present in the HTML code) so that it print all the file names (36017P.mp3 36031P.mp3 hello.mp3) in separate rows in html.
To do this you will need to loop through each item in the array using a foreach and output them into separate rows for example:
foreach ($mp3_files as $key => $value) { ?>
<tr><td><?php echo $value; ?></td></tr>
<?php
}
?>
Something to keep in mind is that the <tr> tags are the table rows, so to have the data separated by rows you will need output them into different <tr> tags each time, also don't forget to use the <td> tags inside the rows to ensure the table recognises it as table data.
As $mp3_files is an array, you need to iterate on it.
To do so, you can use for or foreach statments.
If you whan to display ALL files in the same TD but on a new line though, you can use the implode function with <br> as the glue (it should do the trick):
<?php echo implode('<br>', $mp3_files); ?>
Or use foreach to display it as a list:
<ul>
<?php foreach($mp3_files as $file) : ?>
<li><?php echo $file; ?></li>
<?php endforeach; ?>
</ul>
If you whan to display EACH file on a new TR, then you will have to use for or foreach (as shown by #Declan) :
<?php foreach($mp3_files as $file) : ?>
<tr>
<!-- other tds here -->
<td><?php echo $file; ?></td>
<!-- other tds here -->
</tr>
<?php endforeach; ?>
But if I'm correctly understand your code, this approach will force you to repeat all other tds datas on each new row (tr).
I am using php and mysql to generate excel file. Every thing works fine. I am using html table code and exporting data to excel file. But the downloaded file is having bigger size for 20k records it is showing nearly 35 MB. Below is my code .
<?php
$generated_date = date("Y-m-d-H-i-s");
$filename = 'order_detail_report'.$generated_date;
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
?>
Html table code
<table width="100%" border="0" cellspacing="0" cellpadding="0" >
<tr>
<th class="excel_style">Order Number</th>
<th class="excel_style">EAN</th>
<th class="excel_style">Article #</th>
<th class="excel_style">Amazon Price</th>
<th class="excel_style">ERP Price</th>
<th class="excel_style">Requested Quantity</th>
<th class="excel_style">Dispatch Quantity</th>
<th class="excel_style">Rejected Quantity</th>
<th class="excel_style">Cancelled Quantity</th>
</tr>
While loop
<?php
$query = "my select query";
$res = mysqli_query($mysqliConn,$query);
while($result= mysqli_fetch_assoc($res)){
?>
<tr>
<td class="tdstyle"><?php echo $result['order_id'];?></td>
<td class="tdstyle"><?php echo $result['ean'];?></td>
<td class="tdstyle"><?php echo $result['article_no'];?></td>
<td class="tdstyle"><?php echo $result['net_price'];?></td>
<td class="tdstyle"><?php echo $result['erp_price'];?></td>
<td class="tdstyle"><?php echo $result['requested_quantity'];?></td>
<td class="tdstyle"><?php echo $result['dispatch_quantity']?></td>
<td class="tdstyle"><?php echo $result['rejected_quanity'];?></td>
<td class="tdstyle"><?php echo $result['cancelled_quantity']?></td>
</tr>
<?php } ?>
</table>
How to solve this size of the file problem? Any help would be greatly appreciated. If I use phpexcel library I am getting some out of memory exception. So client asked me don't use phpexcel library.
It is not clear what is exact problem - the size of result generated file or the memory getting exhausting?
But, lets assume that the problem is memory getting exhausting. Do you really need to generate excel(.xslx) files or CSV format is acceptable? You can generate CSV file with a really low memory usage like this
<?php
$fp = fopen('file.csv', 'w');
$query = "my select query";
$res = mysqli_query($mysqliConn,$query);
while($result= mysqli_fetch_assoc($res)) {
$fields = [
$result['order_id'],
$result['ean'],
$result['article_no'],
$result['net_price'],
$result['erp_price'],
$result['requested_quantity'],
$result['dispatch_quantity'],
$result['rejected_quantity'],
$result['cancelled_quantity']
]
fputcsv($fp, $fields);
}
fclose($fp);
?>
I am having problems trying to get my system to count the number of times a files has been downloaded. The website has download buttons linking to the files which are stored on my database, and I would like to be able to count the amount of downloads per file to display as a statistic, ideally once they click the link to download, the column in the files table should be incremented,but i cant do that. i'm beginner in php. somebody can help me?this is my code to display the file.
<h3 class="box-title">Senarai Borang Cuti</h3>
<p> </p>
</div><!-- /.box-header -->
<div class="box-body">
<!-- Advanced Tables -->
<div class="table-responsive">
<?php
$raw_results = mysql_query("SELECT * FROM document WHERE doc_jenisfail= 'Cuti';");
?>
<table width="98%" class="table table-striped" id="dataTables-example">
<thead>
<table width="600" border="1">
<tr>
<th width="190" bgcolor="#756E37"class="sw" style="text-align: centre" scope="col">Tajuk Borang</th>
<th width="30" bgcolor="#756E37" class="sw" style="text-align: centre" scope="col">Tarikh Upload</th>
<th width="30" bgcolor="#756E37" class="sw" style="text-align: centre" scope="col">Memuat Turun</th>
</tr>
</thead>
<tbody>
<?php
while($results = mysql_fetch_array($raw_results)) { ?>
<tr>
<td align="left"><?php echo $results['nama_file'];?></td>
<td align="center"><?php echo $results['tanggal_upload'];?></td>
<td align="center"><img src="images/download.png?key=<?php $results['nama_file'] ?>" /></td>
</tr>
<?php } ?>
</tbody>
</table>
</p></td>
You can add +1 to downloads in your database and then redirect to file.
In your template use link like download.php?id=1, for downloading file with id 1.
Download.php Code:
<?php
//name of file: download.php
$id = $_GET['id'];
//Check if the get number is really a number
if (ctype_digit($id)) {
//Add +1 to downloads
mysql_query("update `document` set `downloads`=`downloads`+1 where id='$id'") or die(mysql_error());
//Redirect to downloads
$download_file = mysql_fetch_array(mysql_query("select * from document where id='$id'")) or mysql_error();
header("Location: http://example.com/pdfs/$download_file[name].pdf");
} else {
echo 'Wrong link';
}
?>
I use form which one upload file into folder and info(name, size) into database.So i want to make deleting form which one delete file and data from database.
<?php
include '../dbc.php';
$tbl_name="image"; // Table name
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
//detect file
$directory = "../../Upload/";
$images = scandir($directory);
$ignore = Array(".", "..");
?>
<table width="800px" style="background:white;margin:50px auto; font-size:13px;" border="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Delete:</strong> </td>
</tr>
<tr>
<td width="40%" align="center"><strong>Name</strong></td>
<td width="10%" align="center"><strong>Size</strong></td>
<td width="5%" align="center"><strong>Delete</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr id='del'>
<td><?php echo $rows['name']; ?></td>
<td><?php echo number_format($rows['imgSize'], 2) ?> MB</td>
<td><a name="delete" href="delData1.php?id=<?php echo $rows['imgId'];
?>">Delete</a></td>
</tr>
<?php
}
?>
</table>
<?php
mysql_close();
?>
delData1.php
<?php
include '../dbc.php';
$tbl_name="image"; // Table name
// get value of id that sent from address bar
$id=$_GET['id'];
$filename = $_POST['filename'];
$path = $_POST['directory'];
if(file_exists($path."/".$filename)) {
unlink($path."/".$filename); //delete file
}
// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE imgId='$id'";
$result=mysql_query($sql);
// if successfully deleted
if($result){
rmdir('$name');
echo "Deleted";
echo "<BR>";
header("Location:Data.php");
}
else {
echo "ERROR";
}
?>
It's delete data from database, but file stay in folder.
I have a table in my Database containing the data of many items with the name of its image.
From that table I want to display each item data with its image. This is the code I use to display it in PHP:
<table>
<thead>
<tr>
<th style="border-width:medium;" rowspan="2">CODE</th>
<th style="border:medium;" rowspan="2">CLR</th>
<th style="border-width:medium;" rowspan="2">IMAGE</th>
<th style="border-width:medium;" rowspan="2">DESCRIPTION</th>
<th style="border-width:medium;" rowspan="2">QTY.</th>
<th style="border-width:medium;" rowspan="2">PRICE</th>
</tr>
</thead>
<tbody>
<?php foreach($list as $data){?>
<tr>
<td><?=$data->code_caption?></td>
<td style="text-align:center"><?=$data->color?></td>
<td style="width:135px; height:135px; text-align:center;">
<img src="<?=base_url().'media/img/gallery/items/'.$data->image?>TH.jpg" style="margin:5px; text-align:center; vertical-align:middle;" />
</td>
<td><?=$data->desc?></td>
<td style="text-align:center"><?=$data->qty?></td>
<td style="text-align:right"><?=$data->price?></td>
</tr>
<?php }?>
</tbody>
</table>
I want to export the data to an Excel formatted file so it looks like what I display on my PHP page.
Can it be done?
I have tried to use the Excel header but the image becomes a link, it's not the complete image being embedded.
And I also saw the PHPexcel library, but I cannot find a way to write the loop with the image.
Any solution for my problem will be very helpful.
Thanks for Helping me I have found the answer for my own question.
<?php
$rowNumber = 12;
foreach($list as $data){
$this->excel->getActiveSheet()->setCellValue('A'.$rowNumber, $data->code_caption)
->setCellValue('B'.$rowNumber, $data->color);
if(file_exists('./media/img/gallery/items/'.$data->image.'TH.jpg'))
{
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('./media/img/gallery/items/'.$data->image.'TH.jpg');
$objDrawing->setCoordinates('C'.$rowNumber);
$objDrawing->setWorksheet($this->excel->getActiveSheet());
$this->excel->getActiveSheet()->getRowDimension($rowNumber)->setRowHeight(120);
}
else
{
$this->excel->getActiveSheet()->setCellValue('C'.$rowNumber, '');
}
$this->excel->getActiveSheet()->setCellValue('D'.$rowNumber, $data->desc);
$this->excel->getActiveSheet()->setCellValue('E'.$rowNumber, $data->$d_met);
$this->excel->getActiveSheet()->setCellValue('F'.$rowNumber, $data->$w_met);
$this->excel->getActiveSheet()->setCellValue('G'.$rowNumber, $data->$h_met);
$this->excel->getActiveSheet()->setCellValue('H'.$rowNumber, $data->qty);
$this->excel->getActiveSheet()->setCellValue('I'.$rowNumber,$data->$cur_dat);
$this->excel->getActiveSheet()->setCellValue('J'.$rowNumber,$data->$total_price);
$rowNumber++;
}?>