Jump to content

[SOLVED] Question regarding For Loop


calabrese

Recommended Posts

Hey there, this is my first post, so hello!

 

Now, down to business :-D

 

I have a small script that takes information from my database and writes it to an XML file.  Now - the problem I am facing is that when it writes the database information to the file, it is all the same.  Here is my code...

 

if(isset($_GET['download'])) {
include 'xls.php';
$getusers = mysql_query("SELECT * FROM `members` ORDER BY `id` ASC") or die(mysql_error());
$xls = new XLS();
while ($user = mysql_fetch_array($getusers)) {
for ($counter = 0; $counter <= mysql_fetch_array($getusers); $counter+=1){
$xls->add_cell(0,$counter,$user['username']);
$xls->add_cell(1,$counter,$user['email']);
$xls->add_cell(2,$counter,$user['location']);
}
}
$xls->save_file('download.xls');
echo "<a href='download.xls'>Right Click > Save As</a>";
}

 

It has to be something small, but I am just missing it.  Any help would be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/111962-solved-question-regarding-for-loop/
Share on other sites

You don't need the for loop.  The while loop goes through everything in the database and the for loop was just displaying the same records over and over because it is still on row 1 of the while loop. 

 

if(isset($_GET['download'])) {

include 'xls.php';

$getusers = mysql_query("SELECT * FROM `members` ORDER BY `id` ASC") or die(mysql_error());

$xls = new XLS();

while ($user = mysql_fetch_array($getusers)) {

$xls->add_cell(0,$counter,$user['username']);

$xls->add_cell(1,$counter,$user['email']);

$xls->add_cell(2,$counter,$user['location']);

}

$xls->save_file('download.xls');

echo "<a href='download.xls'>Right Click > Save As</a>";

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.