Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

osTicket v1.10 (stable) and Maintenance Release v1.9.15 are now available! Go get it now

CSV Export per client

Hi there,

I just thought i could share this, im not quite sure that it is a breakthrough but i've had some really hard time to find the info over the internet.

Anyways, i'm running osticket as logistics follow up support tool for clients and they really have been buggin' me out for a real time export button on their interface so i did this:

1) create a file in the osticket folder called "exportcsv.php"

<?php/* * PHP code to export MySQL data to CSV * http://salman-w.blogspot.com/2009/07/export-mysql-data-to-csv-using-php.html * * Sends the result of a MySQL query as a CSV file for download *//* * establish database connection */ include('client.inc.php'); //make sure u add thisif(!defined('OSTCLIENTINC') || !is_object($thisclient) || !$thisclient->isValid()) die('Kwaheri'); //... and this$conn = mysql_connect('localhost', 'dbuser', 'dbpass') or die(mysql_error());mysql_select_db('dbname', $conn) or die(mysql_error($conn));/* * execute sql query I totally copy/past this part from the tickets.inc.php, i've added the tables i needed tho' :D */$qwhere =' WHERE email='.db_input($thisclient->getEmail()); // STRICT TO THE USER$qfrom=' FROM '.TICKET_TABLE.' ticket LEFT JOIN '.DEPT_TABLE.' dept ON ticket.dept_id=dept.dept_id ';//Pagenation stuff....wish MYSQL could auto pagenate (something better than limit)$result=mysql_query('SELECT ticket.ticketID , ville , subject,name,email,status,ticket.created,paiement,numcmdmar,nomcli,closed'.$qfrom.' '.$qwhere);//$result1 = mysql_query($qselect,$qfrom,$qwhere, $conn) or die(mysql_error($conn));/* * send response headers to the browser * following headers instruct the browser to treat the data as a csv file called export.csv */header('Content-Type: text/csv');header('Content-Disposition: attachment;filename=export.csv');/* * output header row (if atleast one row exists) */$row = mysql_fetch_assoc($result);if ($row) { echocsv(array_keys($row));}/* * output data rows (if atleast one row exists) */while ($row) { echocsv($row); $row = mysql_fetch_assoc($result);}/* * echo the input array as csv data maintaining consistency with most CSV implementations * - uses double-quotes as enclosure when necessary * - uses double double-quotes to escape double-quotes * - uses CRLF as a line separator */function echocsv($fields){ $separator = ''; foreach ($fields as $field) { if (preg_match('/\\r|\\n|,|\"/', $field)) { $field = '\"' . str_replace('\"', '\"\"', $field) . '\"'; } echo $separator . $field; $separator = ','; } echo \"\r\n\";}?>


Afterwards, all i needed is a button to trigger the download... easy

Open the header.inc.php and add this line next to Home.

[HTML]Export CSV
[/HTML]

And you're done.

Special shout outs to salman-w for his script.

Cheers all,

Mouad

Comments

  • OS: Linux
    osTicket Versionv1.9.8.1 (4752178)
    Web Server SoftwareApache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8
    MySQL Version5.6.24
    PHP Version5.6.8

    Can you guide me  a button to trigger the download ???
    thank you so much :D 

  • This thread is from 2013, I doubt that you will get a response.
  • hm... not work. Any idea?
Sign In or Register to comment.