SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Format dynamic CSV file

    Hi,

    I am trying to apply basic formatting to a .CSV file which is created dynamically in PHP.

    I am wanting the spreadsheet to look something like this (if you can understand it):

    HTML Code:
    ----------------------------
    Name:|  Blah |      
    ----------------------------
    Age:  |  25   |       
    ----------------------------
    Code:|  123  |        
    ----------------------------
            |         |       
    ----------------------------
    Users:      |              
    ----------------------------
    Joe Bloggs |               
    ----------------------------
    Bill Gates  |                
    ----------------------------
    Mike Myers|                   
            
    I am using this sql to return the data:

    Code:
    // To return a list of users that have the $groupid value in one of thier selections, selections are stored like (12,16,22,55)
    
    "SELECT users.username FROM users WHERE users.selections REGEXP '[[:<:]]".$groupid."[[:>:]]'"
    
    //At the top of the excel file there should be the name of the session, time & venue using this sql
    
    "SELECT sessions.group,title,room,time FROM sessions WHERE groupid = ".$groupid"
    Here is the PHP code i am using to create the csv file:

    PHP Code:
    <?php 
    header
    ("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=SessionUsers.csv");
    header("Pragma: no-cache");
    header("Expires: 0");


    mysql_select_db($database_hwbltaconf$hwbltaconf);
    $query "SELECT users.username FROM users WHERE users.selections REGEXP '[[:<:]]11[[:>:]]'";
    $exp mysql_query($query);
    $count mysql_num_fields($exp);
    for (
    $i 0$i $count$i++) {
    $header .= mysql_field_name($exp$i).",";
    }
    while(
    $row mysql_fetch_row($exp)) {
    $line '';
    foreach(
    $row as $value) {
    if ((!isset(
    $value)) OR ($value == "")) {
    $value "\t";
    } else {
    $value str_replace('"''""'$value);
    $value '"' $value '"' ",";
    }
    $line .= $value;
    }
    $data .= trim($line)."\n";
    }
    $data str_replace("\r"""$data);
    if (
    $data == "") {
    $data "\n(0) Records Found!\n";
    }
    print 
    "$header\n$data";
    exit;

    ?>
    Does anybody have any advice of how i can format the csv like this?

    Thanks for any help with this.

    Dan.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Before formatting your CSV, you need to properly parameterize your SQL to protect against injection. A blank database returns no results.

    Also, why a CSV? All of the data you showed is more easily presented as a flat file.


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
  •