I am createing a csv file downloaded from mysql database.
I can create the csv file, but it duplicates records. I want to show only one of each record.
I am not sure if it is a looping problem that keeps going back and re-querying the database and therefore gives multiple rows of the same record, but I cannot see anything that I can change on the looping side of the code, so I am looking at array_unique()
I have used array_unique elsewhere in the site, and it works OK, but I cannot get the syntax right in this code to make it function as I want.
when I run the code and create the csv file I get an error message within the downloaded csv file of
<b>Warning</b>: array_unique() [<a href='function.array-unique'>function.array-unique</a>]: The argument should be an array in <b>/home/ejwfletcher/6VWYX645/htdocs/click-charge/test_site/download/readings_status.php</b> on line <b>64</b><br />
(line 64 is $value = array_unique($value);)
so I know this isn't right. I have played around with this, but I just don't know how to write the code to make it work - any help will be appreciated.
the code I have is
header('Content-Type: application/download; name="status.csv"');
header('Content-Disposition:attachment; filename="status.csv"');
header('Content-Transfer-Encoding: binary');
//CREATE STATUS.CSV FILE
$query="SELECT `ConcesRef` FROM `tblUser` WHERE `ULogIn` = '$username'";
$result3 = mysql_query($query)
or die(mysql_error());
$row3 = mysql_fetch_array($result3);
$ConcesRef = $row3["ConcesRef"];
if (isset($ConcesRef))
{
require_once ('../mysql_connect.php'); //connect to the database
$List = array();
$count = 0;
if (sizeof($List) == 0)
{
//START FILLING IN THE FIELDNAME ON TOP OF EXCEL SHEET.
echo "Ref" . "," . "Name" . "," . "Serial" . "," . "Contract Ref" . "," . "Client Ref" . "," . "Company Name" . ",";
echo ("\n");
//START FILLING IN THE ROWS IN EXCEL SHEET.
$query = "SELECT DateLogId FROM tblDateLog WHERE ConcesRef = $ConcesRef ORDER BY EndDate DESC LIMIT 1";
$result5 = mysql_query($query)
or die(mysql_error());
$row5 = mysql_fetch_array($result5);
$DateLogId = $row5["DateLogId"];
$sql = "SELECT tblMachine.MachineId,
tblMachine.CommonName,
tblMachine.SerialNumber,
tblMachine.ContractRef,
tblClient.ClientId,
tblClient.CoName
FROM ((tblClient INNER JOIN tblMachine
ON (tblClient.ConcesRef = tblMachine.ConcesRef)
AND (tblClient.ClientId = tblMachine.ClientRef))
INNER JOIN tblUserMachine
ON (tblUserMachine.ConcesRef = tblMachine.ConcesRef)
AND (tblUserMachine.ClientRef = tblMachine.ClientRef)
LEFT JOIN tblResults
ON (tblMachine.ConcesRef = tblResults.ConcesRef)
AND (tblMachine.ClientRef = tblResults.ClientRef)
AND (tblMachine.ContractRef = tblResults.ContractRef)
AND (tblMachine.MachineId = tblResults.MachineRef)
AND tblResults.DateLogRef = '$DateLogId'
AND tblResults.ResultsId = '0000-00-00 00:00:00'
AND tblClient.ConcesRef = '$ConcesRef')";
$getinfo = mysql_query($sql)
or die (mysql_error());
while($row = mysql_fetch_array($getinfo, MYSQL_ASSOC))
{
$value = array_unique($value);
while (list($key, $value) = each($row))
{
echo ("$value" . ",");
} // close 2nd while
echo ("\n");
} // close 1st while
mysql_close();
} // close 2nd if
else
{
listErrors();
} // close 1st else
} //close 1st if
else
{
echo "please try again";
} // close 2nd else