SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Using OOP

  1. #1
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using OOP

    I am using code from sitepoint article to generate an excel sheet. However, it doesn't work for me.

    Why is this? Tried using very basic example and it works.

    Here's my code. I would really appreciate some help.

    PHP Code:
    <?php
     
    require_once "Spreadsheet/Excel/Writer.php";
     
     
    // Create Workbook
     
    $xls =& new Spreadsheet_Excel_Writer();
     
     
    // Create Worksheet
     
    $cart =& $xls->addWorksheet('list');
     
     
    /* FORMATTING HEADER   */
     
     // title text
     
    $titleText 'M-List '.date('dS M Y');
     
     
    // create format object
     
    $titleFormat =& $xls->addFormat();
     
     
    // set the font family
     
    $titleFormat->setFontFamily('Helvetica');
     
     
    // set the text to bold
     
    $titleFormat->setBold();
     
     
    // set the text size
     
    $titleFormat->setSize('16');
     
     
    // set the text color
     
    $titleFormat->setColor('navy');
     
     
    // set bottom border width to thick
     
    $titleFormat->setBottom(2);
     
     
    // set the color of the bottom border
     
    $titleFormat->setBottomColor('navy');
     
     
    // set the alignment to the special merge value
     
    $titleFormat->setAlign('merge');
     
     
    // add the title to the top left cell of the worksheet,
     // passing it the title string and the form object
     
    $cart->write(0,0,$titleText,$titleFormat);
     
     
    // add empty cells to merge with
     
    $cart->write(0,1,'',$titleFormat);
     
    $cart->write(0,2,'',$titleFormat);
     
    $cart->write(0,3,'',$titleFormat);
     
     
    // the row height
     
    $cart->setRow(0,30);
     
     
    // Set the column width
     
    $cart->setColumn(0,3,15);
     
     
    /*   End Header Formatting   */
     
     
     /*    Column Headings FORMATTING    */
     
     
    $colHeadingFormat =& $xls->addFormat();
     
    $colHeadingFormat->setBold();
     
    $colHeadingFormat->setFontFamily('Helvetica');
     
    $colHeadingFormat->setBold();
     
    $colHeadingFormat->setSize('10');
     
    $colHeadingFormat->setAlign('center');
     
     
    // An array with the data for the column headings
     //$colNames = array('Client', 'Entity', 'Subfund', 'Director', 'Accounting Manager');
     
    $colNames = array('Item''Price''Quantity''Total');
     
     
    // Add all the column headings with a single call
     // leaving a blank row to look nicer
     
    $cart->writeRow(2,0,$colNames,$colHeadingFormat);
     
     
    // Cell group to Freeze
     
    $freeze = array(3,0,4,0);
     
     
    $cart->freezePanes($freeze);
     
     
    /*  End Column Headings Formatting    */
     
     // Create Associative Array
     //$items = array( 'director'=>$director, 'manager'=>$manager, 'client'=>$client);
     
    $items = array (
                  array(
    'description'=>'book''price'=>34.0'quantity'=>1),
                  array(
    'description'=>'cd''price'=>20.0'quantity'=>2),
                  array(
    'description'=>'clothes''price'=>80.0'quantity'=>4),
                  );
     
     
    // tracker
     
    $currentRow 4;
     
     
    // Loop Through Results
     
    foreach ($items as $item)
     {
              
    $cart->writeRow($currentRow,0,$item);
             
    $currentRow++;
     }
     
     
    ?>

  2. #2
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you explain,
    re you getting an error or it doesnot generate the spreadsheet or anything else
    ---------------------------
    Errors = Improved Programming.
    My Site

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it working I had ommitted the close() part.

    Problem now is I have integrated it into other php query code but the result gives these errors:

    Warning: Cannot modify header information - headers already sent by (output started at /home/k28775/public_html/mlist/two.php:9) in /usr/lib/php/Spreadsheet/Excel/Writer.php on line 70

    Why is this?
    ?


    PHP Code:
    <?php
     
     
    require_once "Spreadsheet/Excel/Writer.php";
     include(
    'db_fns.php');
     
     
    // Declare Variables
     
    $client $_POST['clients'];
     
    $director $_POST['directors'];
     
    $entity $_POST['entity'];
     
    $contact $_POST['contacts'];
     
    $manager $_POST['manager'];
     
     
    // Connect to Database
     
    $conn db_connect();
     if (!
    $conn)
          return 
    'Could not connect to Database;';
          
          
     if (!
    $client || $client=="none")
     {
           echo 
    'Please select client';
          exit;
     }
      
     
    // Perform Query
     
    $query="select * 
       from client as c
     inner
       join subfund as s
         on c.client_ID = s.client_ID
     inner
       join director as d
         on s.director_ID = d.director_ID
     inner
       join entity as e
         on s.entity_ID = e.entity_ID
     inner
       join contact as co
         on s.contact_ID = co.contact_ID
     inner
       join manager as m
         on s.manager_ID = m.manager_ID
     inner
       join domicile as dm
         on s.domicile_ID = dm.domicile_ID
      where c.client = '
    $client'";
      
      
     
    // Check what menus selected
     
     
    if($director!="none"
     {
     
    $query .=" and d.director='$director' and d.director_ID=s.director_ID";
     }
     
     if(
    $entity!="none"
     {
     
    $query .=" and e.entity='$entity' and e.entity_ID=s.entity_ID";
     }
     
     if(
    $contact!="none"
     {
     
    $query .=" and co.name='$contact' and co.contact_ID=s.contact_ID";
     }
     
     if(
    $manager!="none"
     {
     
    $query .=" and m.manager='$manager' and m.manager_ID=s.manager_ID";
     }
     
     
    $result mysql_query($query) or die(mysql_error());
     
    $num_results mysql_num_rows($result);
     
     
     
     
    // Create Workbook
     
    $xls =& new Spreadsheet_Excel_Writer();
     
     
    // HTTP Headers
     
    $xls->send("test.xls");
     
     
    // Create Worksheet
     
    $cart =& $xls->addWorksheet('list');
     
     
    /* FORMATTING HEADER   */
     
     // title text
     
    $titleText 'M-List '.date('dS M Y');
     
     
    // create format object
     
    $titleFormat =& $xls->addFormat();
     
     
    // set the font family
     
    $titleFormat->setFontFamily('Helvetica');
     
     
    // set the text to bold
     
    $titleFormat->setBold();
     
     
    // set the text size
     
    $titleFormat->setSize('16');
     
     
    // set the text color
     
    $titleFormat->setColor('navy');
     
     
    // set bottom border width to thick
     
    $titleFormat->setBottom(2);
     
     
    // set the color of the bottom border
     
    $titleFormat->setBottomColor('navy');
     
     
    // set the alignment to the special merge value
     
    $titleFormat->setAlign('merge');
     
     
    // add the title to the top left cell of the worksheet,
     // passing it the title string and the form object
     
    $cart->write(0,0,$titleText,$titleFormat);
     
     
    // add empty cells to merge with
     
    $cart->write(0,1,'',$titleFormat);
     
    $cart->write(0,2,'',$titleFormat);
     
    $cart->write(0,3,'',$titleFormat);
     
     
    // the row height
     
    $cart->setRow(0,30);
     
     
    // Set the column width
     
    $cart->setColumn(0,3,15);
     
     
    /*   End Header Formatting   */
     
     
     /*    Column Headings FORMATTING    */
     
     
    $colHeadingFormat =& $xls->addFormat();
     
    $colHeadingFormat->setBold();
     
    $colHeadingFormat->setFontFamily('Helvetica');
     
    $colHeadingFormat->setBold();
     
    $colHeadingFormat->setSize('10');
     
    $colHeadingFormat->setAlign('center');
     
     
    // An array with the data for the column headings
     //$colNames = array('Client', 'Entity', 'Subfund', 'Director', 'Accounting Manager');
     
    $colNames = array('Client''Entity''Subfund''Director');
     
     
    // Add all the column headings with a single call
     // leaving a blank row to look nicer
     
    $cart->writeRow(2,0,$colNames,$colHeadingFormat);
     
     
    // Cell group to Freeze
     
    $freeze = array(3,0,4,0);
     
     
    $cart->freezePanes($freeze);
     
     
    /*  End Column Headings Formatting    */
     
     
    $items = array();
     
     while (
    $row mysql_fetch_assoc($result))
     {            
    $items[] = array(
                              array(
    'Client'=>$row['client'], 'Entity'=>$row['entity'],'Subfund'=>$row['subfund']),
                              );
     }
     
     
    // tracker
     
    $currentRow 4;
     
     
    // Loop Through Results
     
    foreach ($items as $item)
     {
              
    $cart->writeRow($currentRow,0,$item);
             
    $currentRow++;
     }
     
     if (
    $num_results>0)
     {
      echo 
    '';
     }
     else echo 
    'No Match';
     
     
    $xls->close();
     
    ?>

  4. #4
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Throw an ob_start(); at the top of your script before any output.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •