Results 1 to 5 of 5

Thread: export to cvs questions [php]

  1. #1

    export to cvs questions [php]

    Hello all

    I have created a page for my site that will allow me to export certain data to cvs file. Now this works and i can down load it off my server and all is fine there.

    Here comes the question.

    I have an array that i fill from my database and that works. But when i do pull that data it repeats the same information twice.

    for example

    1,1,firstname,firstname,lastname,lastname

    and so on.

    here is my code

    PHP Code:
    // Create an instance of DbConnector and Validator
    $connector = new DbConnector();
    $result $connector->query('SELECT ID,user,thegroup,firstname,surname,enabled,created,logged_in,log_time,last_log FROM cmsusers ORDER BY ID');
    while (
    $row $connector->fetchArray($result)){
    //create array of data from table
    $usertrack[] = $row;
    }


    function 
    makeCSV($data$sep ','){
        if(!
    is_array($data)){
            return 
    'no data';
        }
        
    $ret '';
        foreach(
    $data as $record){
            if(
    is_array($record)){
                
    $ret .= join($sep$record) . "\n";
            }
        }
        return 
    $ret;
    }

    echo 
    makeCSV($usertrack); 
    Now the second part is this. I've seen it done and found a page on google that told me how to do this but i lost it and for the life of me can't find it again.

    how can i get the title of each element from that array?

    so if something is added in under firstname then the title of that part of the array is firstname.

    If that doesn't make sense then please don't hesitate to ask.

  2. #2

  3. #3
    Cheers.

    Yeah its bugging why its duplicating it twice.

    Thanks for that.
    Last edited by deletedUser2352352; January 10th, 2007 at 12:44 PM.

  4. #4
    oh, i'm an idiot. here's what it is: basically, when you query using mysql_fetch_array (which your fetchArray method is probably doing), by default it will return both column names and auto generated array keys. so your row array actually looked like this:
    Code:
    Array
    (
        [ID] => 1
        [0]    => 1
        [user] => uservalue
        [1] => uservalue
        [thegroup] => groupvalue
        [2] => groupvalue
    ...
    )
    so you need to set your fetchArray to MYSQL_ASSOC so that you don't return the auto generated numeric keys. that leaves you with just this:
    Code:
    Array
    (
        [ID] => 1
        [user] => uservalue
        [thegroup] => groupvalue
    ...
    )
    then you won't have more duplicate data. i figured this out by inserting a print_r in the loop to see the array contents:
    PHP Code:
    foreach($data as $record){
            if(
    is_array($record)){
                
    print_r$record );
                
    $ret .= join($sep$record) . "\n";
            }
        } 

  5. #5
    Now i see wicked cheers for that mate.

    Wicked

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012