Hey thanks that worked really well. I've amended the code to go through all the locations in my database and list the salesman that are there but for some reason the spreadsheet is blank when I open it. The only thing that's on it is the headers (Branch, Salesman ID, First Name, Last Name) the rest is blank.
Code: Select all
<?php
$mysqli = mysqli_connect("localhost", "my_username", "my_pass", "my_database");
$i = 1;
for ($i=1; $i <= 23; $i++){
$fout = fopen('test.csv','wb') or die ('Cannot open the file for output');
fwrite($fout,"Branch, Salesman ID, First Name, Last Name\n");
$sql = "SELECT branch.name, salesperson.salesman_no, salesperson.first_name, salesperson.last_name FROM branch
LEFT JOIN (salesperson) ON salesperson.branch_no = branch.branch_no
WHERE salesperson.branch_no = '$i'";
$res = mysqli_query($mysqli, $sql);
while($row = mysqli_fetch_assoc($res)){
fwrite($fout,$row['name'].",".$row['salesman_no'].",".$row['first_name'].",".$row['last_name']."\n");
}
fwrite($fout,"\n");
}
fclose($fout);
?>
I've tested and the variable $i increments up to 24 as it should. Also this code works when you change $i to an actual number such as "1" on line 9.
Not sure why this is happening?
**EDIT** I worked out why this wasn't working. Had to move the part where the file is opened out of the for loop.
Is there anyway of making the text bold or is that pushing the limit on php?
I've heard there is some php library that interacts with excel from php scripts very well but this seems a bit too confusing for someone as new to php as me.