sunil
sunil

Reputation: 3

What is wrong with my script?

I am trying to insert values to a csv file through the following script....the script works fine but in the output, the values are not properly arranged in order..

<?php
require_once '../config.php';
$month = $_GET['month'];
$sq="select * from  employee where month ='$month'";
$sql=mysql_query($sq);

$row = array();
$row[] = 'Employee Code';
$row[] = 'Employee Name';
$row[] = 'DOJ';
$data .= join(',', $row)."\n"; // Join all values without any trailing commas 
$row = array(); // We must clear the previous values
while($rs = mysql_fetch_object($sql))
{
$row[] = $rs->employee_code;
$row[] = $rs->employee_name;
$row[] = $rs->emp_dateofjoin;
$row[] = "\n";
}
$data .= join(',', $row);

// Output the headers to download the file
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=test.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $data;
?>

The values are added in the csv file with one blank cell in the beginning of a row. except the first row.

Upvotes: 0

Views: 86

Answers (2)

classicjonesynz
classicjonesynz

Reputation: 4042

I think you could also look into ORDER BY FIELD() DESC, might also help arrange whatever you want in descending. just a thought.

SELECT * FROM employee WHERE month = '$month' AND order by field(field1,field2,field3,field4) desc

Or you could use a while loop inside of a foreach with regular expression and ereg() to scan through the characters and then set what and when you want something displayed. Another method is to sort the information from the highest populated elements to lower.

Upvotes: 0

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174977

Without even looking at it, what's wrong with the core of your script is that you are using mysql_* functions. So don't. You should instead move to PDO (awesome), or mysqli (good).

other then that, have you considered adding an ORDER BY clause to your query?

Upvotes: 1

Related Questions