Error occurs while importing excel file into MYSQL in php - php

I am new to php. I need to import excel details into MySQL database in php. I downloaded 2 files reader.php & oleread.php and save that under wamp/www/folder. When I execute the following code, the server throw a error like
The filename samp.xls is not readable
Give the solution to recover this problem. my code is:
<html>
<head>
<title>Save Excel file details to the database</title>
</head>
<body>
<?php
include 'db_connection.php';
include 'reader.php';
$excel = new Spreadsheet_Excel_Reader();
?>
<table border="1">
<?php
$excel->read('samp.xls');
$x=2;
while($x<=$excel->sheets[0]['numRows']) {
$id = isset($excel->sheets[0]['cells'][$x][1]) ? $excel->sheets[0]['cells'][$x][1] : '';
$name = isset($excel->sheets[0]['cells'][$x][2]) ? $excel->sheets[0]['cells'][$x][2] : '';
// Save details
$sql_insert="INSERT INTO students (sid,name) VALUES ('$id','$name')";
$result_insert = mysql_query($sql_insert) or die(mysql_error());
$x++;
}
?>
</table>
</body>
</html>

The filename samp.xls is not readable
First thing to come to mind is that your php server cannot read your excel file ..
WAMP is on Windows, so Right-Click your excel file, and add read property to Everyone

why not import it directly into phpmyadmin?? click here

Related

How to display data in ckeditor from mysql in PHP

I am new in this field, I used ckeditor but I don't know how to display data in ckeditor through mysql..
My code is
$CKeditor = new CKeditor();
$CKeditor->BasePath = 'manage-site/';
$CKeditor->config['filebrowserBrowseUrl'] = 'ckfinder/ckfinder.html';
$CKeditor->config['filebrowserImageBrowseUrl'] = 'browser/browser.php?type=Images';
$CKeditor->config['filebrowserFlashBrowseUrl'] = 'ckfinder/ckfinder.html?type=Flash';
$CKeditor->config['filebrowserUploadUrl'] = 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';
$CKeditor->config['filebrowserImageUploadUrl'] = 'uploader/uploader.php?type=Images';
$CKeditor->config['filebrowserFlashUploadUrl'] = 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash';
$CKeditor->editor('page_desc','**page_desc**');
For display content I use
<?php echo stripslashes($details[0]["page_desc"]); ?>
I wanted to display on the place of Page_desc(last Line of Code). How to do that?
You can simply download the ckeditor package and do this simple 2 step
Step 1 : Include the ckeditor.js from your file
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
Step 2 : Include the class name as ckeditor to your textarea
Then you can see the ckeditor with simple step
Note : Make sure the ckeditor.js is in the proper path
Hope this helps you

how to put jpgraph into PDF using dompdf-0.5.1

It took me 12 hours still no improvement. I try displaying different images from my computer stored in the server and the result was successful. however when displaying the reports pie graph, it wont read every time the system tries to convert to pdf. It gives a blank pdf file. On the other hand, I can view the pie graph I created by using echo''; in the reports.php. I used the same concept in dompdf file but its not working.
DOMPDF
<html>
<head>
<title></title>
</head>
<body>
<?php echo'<img src="reports-display.php"/>';?>
</body>
</html>
<?php
$html = ob_get_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
JPGRAPH Drawing
<?php
require('dbcon.php');
require_once('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_pie.php');
require_once ('jpgraph/src/jpgraph_pie3d.php');
//LEGEND
//YELLOW=LIVE BLUE=WAITING GREEN=DONE
//sql query for live
$live = mysql_query("Select count(*) as count1 from tbl_display_ads where status LIKE '%Live%'") or die(mysql_error());
//sql query for waiting
$waiting = mysql_query("Select count(*) as count2 from tbl_display_ads where status LIKE '%Waiting%'") or die(mysql_error());
//sql query for done/posted advertisement
$done = mysql_query("Select count(*) as count3 from tbl_display_ads where status LIKE '%Done%'") or die(mysql_error());
//While loop for live
while($resultlive = mysql_fetch_array($live))
{
$totallive = $resultlive['count1'];
}
//While loop for waiting
while($resultwaiting = mysql_fetch_array($waiting))
{
$totalwaiting = $resultwaiting['count2'];
}
//While loop for done
while($resultdone = mysql_fetch_array($done))
{
$totaldone = $resultdone['count3'];
}
// Some data
$data = array($totallive,$totalwaiting,$totaldone);
// Create the Pie Graph.
$graph = new PieGraph(500,450);
$theme_class= new VividTheme;
$graph->SetTheme($theme_class);
// Set A title for the plot
$graph->title->Set("Figure 1.1: Totality of Display Advertisement");
// Create
$p1 = new PiePlot3D($data);
$p1->SetCenter(0.5,0.55);
$p1->SetLegends(array("Live","Waiting","Done"));
$graph->legend->SetPos(0.5,0.100,'center','bottom');
$graph->Add($p1);
$p1->ShowBorder();
$p1->SetColor('black');
$p1->ExplodeSlice(1);
$graph->Stroke();
// Get the handler to prevent the library from sending the
// image to the browser
$gdImgHandler = $graph->Stroke(_IMG_HANDLER);
// Stroke image to a file and browser
// Default is PNG so use ".png" as suffix
$fileName = "/tmp/imagefile.png";
$graph->img->Stream($fileName);
// Send it back to browser
$graph->img->Headers();
$graph->img->Stream();
?>
I finally found out the solution. in the report-display.php I set the extension name of the graph to .png and save to the directory folder for reports.
DEFINE("DEFAULT_GFORMAT","auto");
$graph->img->SetImgFormat("png");
if(file_exists("Reports/reports-display.png")) unlink("Reports/reports-display.png");
$graph->Stroke("Reports/reports-display.png");
The problem is that you're essentially asking dompdf to grab an image file called "reports-display.php" from the local filesystem. When you use $dompdf->load_html() dompdf has no idea where the content arrives from. Any resource references in the HTML that lack a full URL are pulled in via the local filesystem. Since dompdf does not parse the PHP the source will be read in, which is obviously not a valid image document.
You're found a valid solution in saving the file locally. There are two other possibilities:
1) Point to the jpgraph script through your web server.
<html>
<head>
<title></title>
</head>
<body>
<img src="http://example.com/reports-display.php"/>
</body>
</html>
2) Capture the jpgraph output and insert into the document as a data-uri.
<html>
<head>
<title></title>
</head>
<body>
<img src="data:image/png;base64,<?php echo base64_encode(include_once('reports-display.php');?>"/>
</body>
</html>
With this method reports-deplay.php would have to be updated to return the image rather than stream it. Something like:
$graph = new PieGraph(500,450);
// snip steps that generate the content
return $graph->Stroke();

fpassthru acting weird (sending the page html rather then the file I want)

I'm really really new to PHP, so if you can explain it to me what my code is actually doing and why the result is what it is I would appreciate very much. I'm probably screwing up something very simple.
Basically I want to query a MySQL database, create a csv with the data, and download the csv. Pretty simple. Here is my code:
<?php
include("Includes/PHPheader.php");
$query_string = $_SERVER['QUERY_STRING'];
parse_str($query_string);
$sql = "SELECT many_columns_i_removed_from_this_sample_code FROM table WHERE id = '".$id."'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$f = fopen("csv/tmp.csv", "w");
fputcsv($f, array_keys($row),';');
fputcsv($f,$row,';');
rewind($f);
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="tmp.csv"');
fpassthru($f);
fclose($f);
?>
There are some HTML code below it that shouldn't affect anything, but just in case here it is.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
</body>
</html>
Well, I thought this would download my csv with no problem. If I go to the csv folder, there it is, the tmp.csv file I created, with the proper header and data.
But when I open the tmp.csv file I downloaded, it is actually the html code of the page, and not the data I expected.
What is going on?
In case it helps, I'm using WebMatrix 3.0.
There are two things going on, probably. First, You are trying to read (fpassthru) from a file opened for writing (fopen(..., "w")), so You are not able to read anything from the file. Then, after that "reading nothing" goes Your HTML code, which naturally appends to Your output. Try this:
$f = fopen("csv/tmp.csv", "w+");
...
fclose($f);
exit;
Could you please try?
header("Content-type: application/vnd.ms-excel");
instead of
header('Content-Type: application/csv');
I have a test code of CSV output, have a look - https://foowms.googlecode.com/svn/trunk/stockincsv.php
A very informative thread of Stack Overflow
Setting mime type for excel document
==============================================
If csv file open, write and save, then you should do as follows-
$list = array ("Peter,Griffin,Oslo,Norway");
$file = fopen("csv/tmp.csv","w");
foreach ($list as $line) {
fputcsv($file,explode(',',$line));
}
fclose($file);
==============================================
You also could try this
fputcsv($fp, array_values($list), ';', ' ');
instead of
fputcsv($f, array_keys($row),';');

how to export fusionchart in PHP

I want to export fusionchart to images or pdf , what should I add? without using a third party
<?php
*//koneksi database*
include "config/koneksi.php";
error_reporting(E_ALL ^ (E_NOTICE | E_DEPRECATED));
include "config/koneksi.php";
include("config/class/FusionCharts_Gen.php");
$pencarian=$_POST['pencarian'];
$bulan=$_POST['bulan'];
$barang_aset=$_POST['barang_aset'];
?>
<html>
<head>
<title>First Chart Using FusionCharts PHP Class</title>
<script language='javascript' src='assets/js/FusionCharts.js'></script>
</head>
<body>
<?php
# Include FusionCharts PHP Class
# Create object for Column 3D chart
$FC = new FusionCharts("column3d","900","450");
# Setting Relative Path of chart swf file.
$FC->setSwfPath("Charts/");
# Store chart attributes in a variable
$strParam="caption=Grafik Jumlah Inventaris dan Aset Berdasarkan Kategori";
# Set chart attributes
$FC->setChartParams($strParam);
$kategori = mysql_query("SELECT id_kategori, nama_kategori FROM kategori");
//$tracking = mysql_query("SELECT Nama_Karyawan FROM master_karyawan WHERE Kode_Nama_Cabang='SRJ' AND Category_Tracking='sales'");
while ($r_kat = mysql_fetch_array($kategori)){
$id_kat = $r_kat['id_kategori'];
$kat = $r_kat['nama_kategori'];
$counter1 = 0;`enter code here`
**What do I need to add to add exportEnabled in this fusionchart**
//$total = mysql_num_rows(mysql_query("SELECT IdKat,TglTerjual FROM penjualan_buku WHERE IdKat='$kat' AND LEFT(TglTerjual,4)='2012' AND MID(TglTerjual,6,2)='02'"));
$total = mysql_query("SELECT id_kategori, nama_barang FROM barang WHERE id_kategori='$id_kat' and nama_barang like '%$_POST[barang_aset]%'and MONTH(tanggal_status) like '%$_POST[bulan]%' and status like '%$_POST[pencarian]%' ");
$counter1++;
//$persentase = ($total!=0 || $review !=0)?($review / $total) *100:0;
$total = mysql_num_rows($total);
# add chart values and category names
$FC->addChartData("$total","name=<a href='jmlaset.php?id=$id_kat'>$kat</a>");
}
# Render Chart
$FC->renderChart();
?>
</body>
</html>
How do I add an export function in this fusionchart?
Your php wrapper class for FusionCharts is not validating with the latest wrapper class please see the link
you can also export chart through FusionCharts export server by setting the attribute exportEnabled to 1. This will use their export server and will download the image at your end.
If you want to create your own export server, you can use the FusionCharts' distributable export handler. You can get useful information from this link but it uses two third party api ImageMagick and InkScape to convert SVG to required image formats. You need to install this dependencies at your end.

Create XLS file from MySQL database using PHP script

I want to generate XLS files with a PHP script from a MySQL database. The big problem here is that when I open the new exported .xls file, I see the values OK (i.e. it is correctly formatted) but the colour of the field changed to white. However, I need the colour unchanged, as is by default in Excel.
Here is the PHP script that I use to extract data from my database.
<?php
include 'connect.php';
$result = mysql_query('SELECT * FROM projects2');
?>
<center><h1>Lista valorilor din tabela</h1>
<h2>Exporta lista Clienti</h2></center>
<?php
include_once 'tabel_clientipm.php';
?>
The PHP file used to generate the XLS file is:
<?php
include 'connect.php';
$result = mysql_query('SELECT * FROM projects2');
if (isset($_GET['exporta_lista_clienti'])) {
$filename = 'raportnou.xls';
header("Content-type: application/ms-excel");
header("Content-Disposition: attachment; filename=$filename");
include_once 'tabel_clientipm.php';
exit();
}
?>
I added the tabel_clientipm.php:
<center>
<table border="1">
<tr>
<th>surname</th>
<th>name</th>
<th>age</th>
</tr>
<?php
while ($client = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $client['surname'];?></td>
<td><?php echo $client['name'];?></td>
<td><?php echo $client['age'];?></td>
</tr>
<?php
}
?>
</table>
</center>
If you want to create real .xls file NOT csv or html hidden in .xls extension use PHPExcel or
It supports the following formats.
BIFF 8 (.xls) Excel 95 and above
Office Open XML (.xlsx) Excel 2007 and above
If PHPExcel is slow for you check these alternatives (provided by an author of PHPExcel. All of them are faster than PHPExcel
If you export as a csv, you can probably import the data as colorless.

Categories