Tuesday, 2 August 2016

PhpToExcel EXPORT CODING EXAMPLE

03:36

(1)write code for connection.php   file:

<?php

                $hostname = "localhost";
                $username = "root";
                $password = "";
                $database = "test";


                 $conn = mysql_connect("$hostname","$username","$password") or die(mysql_error());
                mysql_select_db("$database", $conn) or die(mysql_error());

?>
(2)write code for ExportToExcel.php file:

<?php
function ExportExcel($table)
{

$filename = "uploads/".strtotime("now").'.csv';

$sql = mysql_query("SELECT * FROM $table") or die(mysql_error());

$num_rows = mysql_num_rows($sql);
if($num_rows >= 1)
{
$row = mysql_fetch_assoc($sql);
$fp = fopen($filename, "w");
$seperator = "";
$comma = "";

foreach ($row as $name => $value)
{
$seperator .= $comma . '' .str_replace('', '""', $name);
$comma = ",";
}

$seperator .= "\n";
fputs($fp, $seperator);

mysql_data_seek($sql, 0);
while($row = mysql_fetch_assoc($sql))
{
$seperator = "";
$comma = "";

foreach ($row as $name => $value)
{
$seperator .= $comma . '' .str_replace('', '""', $value);
$comma = ",";
}

$seperator .= "\n";
fputs($fp, $seperator);
}

fclose($fp);
echo "Your file is ready. You can download it from <a href='$filename'>here!</a>";
}
else
{
echo "There is no record in your Database";
}


}
?>

(3)write  code for index.php   file:

<?php

include ("connection.php");
include ("ExportToExcel.php");

if(isset($_POST["submit"]))
{
ExportExcel("myrecord");
  }

?>

<html>
<head>
<style type="text/css">
body
{
margin: 0;
padding: 0;
background-color:#D6F5F5;
text-align:center;
}
.top-bar
{
width: 100%;
height: auto;
text-align: center;
background-color:#FFF;
border-bottom: 1px solid #000;
margin-bottom: 20px;
}
.inside-top-bar
{
margin-top: 5px;
margin-bottom: 5px;
}
.link
{
font-size: 18px;
text-decoration: none;
background-color: #000;
color: #FFF;
padding: 5px;
}
.link:hover
{
background-color: #9688B2;
}
</style>

</head>

<body>

<form name="export" method="post">
    <input type="submit" value="Click Me!" name="submit">
    </form>
   
<hr style="margin-top:300px;" />
   
    <div align="center" style="font-size:18px;"><a href="http://www.phpdevelopmenttricks.blogspot.com">&copy;om sir</a></div>

</body>
</html>




Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

3 comments:

  1. I like the way you write. Awesome, keep it up.
    PHP Training

    ReplyDelete
  2. I really enjoyed while reading your article, the information you have mentioned in this post was damn good. Keep sharing your blog with updated and useful information.
    Read more about php training in delhi

    ReplyDelete

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top