Wednesday, April 17, 2013

Php Joomla Export users in Excel sheet as .csv format

Hi Friends

      You can easily export your users data in Excel sheet in .csv format

       <?php

 ini_set('session.gc_maxlifetime', '28800');
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
    include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__FILE__));
    require_once JPATH_BASE.'/includes/defines.php';
}

require_once JPATH_BASE.'/includes/framework.php';

// Mark afterLoad in the profiler.
JDEBUG ? $_PROFILER->mark('afterLoad') : null;

// Instantiate the application.

$app = JFactory::getApplication('site');
/**************************************************/
// Your code starts here...
/**************************************************/


$joomla_table = 'lightboxuser'; //Edit for one simple table export (no need table prefix)
$query = ""; // Edit here if you want COMPLEX selects from table(s)/especific fields
//TODO: make this code a bit more 'Joomla Framework Like'
//$app = JFactory::getApplication();
$table = $app->getCfg('dbprefix') . $joomla_table;
//echo $table; exit;
if (!$query) {
$query = "select u_name as name,u_email as email from $table";
}
$host = $app->getCfg('localhost');
$db = $app->getCfg('databasename');
$user = $app->getCfg('root');
$password = $app->getCfg('root');
$myquery = mysql_query($query);
$fields_cnt = mysql_num_fields($myquery);
$time = date('m.d.y-H.i.s');
$filenameoutput = "Php-shiners-{$joomla_table}_{$time}";
//Output CSV Options
$line_terminated = "\n";
$field_terminated = ",";
$enclosed = '"';
$escaped = '\\';
$export_schema = '';

for ($i = 0; $i < $fields_cnt; $i++) {
$l = $enclosed . str_replace($enclosed, $escaped . $enclosed, stripslashes(mysql_field_name($myquery, $i))) . $enclosed;
$export_schema .= $l;
$export_schema .= $field_terminated;
}
$output = trim(substr($export_schema, 0, -1));
$output .= $line_terminated;
while ($row = mysql_fetch_array($myquery)) {
$export_schema = '';
for ($j = 0; $j < $fields_cnt; $j++) {
if ($row[$j] == '0' || $row[$j] != '') {
if ($enclosed == '') {
$export_schema .= $row[$j];
} else {
$export_schema .= $enclosed .
str_replace($enclosed, $escaped . $enclosed, $row[$j]) . $enclosed;
}
} else {
$export_schema .= '';
}

if ($j < $fields_cnt - 1) {
$export_schema .= $field_terminated;
}
}
$output .= $export_schema;
$output .= $line_terminated;
}
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($output));
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename={$filenameoutput}.csv");
echo $output;
exit;
$app->close();
?>

Please Refer the screen shot

Note : you can use this script in any php cms

Enjoy .....



No comments:

Post a Comment