<?php // Include PEAR::Spreadsheet_Excel_Writer
require_once "Spreadsheet/Excel/Writer.php";
// Create an instance
$xls =& new Spreadsheet_Excel_Writer();
// Send HTTP headers to tell the browser what's coming
$xls->send("test.xls");
// Add a worksheet to the file, returning an object to add data to
$sheet =& $xls->addWorksheet('Books CMPG Library');
//Prapre mySQL Connection
$connection=mysql_connect('webdb:3309', 'cluster', 'fromcluster');
mysql_select_db('booklibrary', $connection);

//Write Legend
$sheet->write(0,0,'Label');
$sheet->write(0,1,'Title');
$sheet->write(0,2,'Authors');
$sheet->write(0,3,'Year');
$sheet->write(0,4,'Publisher');

//Write Books
    $i=0;
    foreach($_SESSION['booklist'] as $key=>$val){
        if(isset($_POST['export'.$key])){
            ++$i;
            $sql="select b.id, b.label, b.title, b.year, p.publisher
                  from book b, publisher p
                  WHERE b.publisher_id=p.id AND b.id=".$val;
            $query=mysql_query($sql,$connection);
            $book=mysql_fetch_object($query);
            $sheet->write($i,0,$book->label);
            $sheet->write($i,1,$book->title);
            $sql="SELECT * from author WHERE book_id=".$book->id." order by id asc";
            $query=mysql_query($sql,$connection);
            $text='';
            while($author=mysql_fetch_object($query)){
                $text=$text.$author->author.';';
            }
            $sheet->write($i,2,$text);
            $sheet->write($i,3,$book->year);
            $sheet->write($i,4,$book->publisher);
        }
    }
    
// Finish the spreadsheet, dumping it to the browser
$xls->close();
?>
