Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts

    records not looping properly in export to csv

    first I want to thank missing-score for his fine tutorial in the php faq on exporting to csv - I wouldn't have gotten this far without it!

    there are actually 2 things that I'd like to do with this export...first and foremost would be that it at least displays the correct records; the second would be rather than have it display them at all have the csv prompt for a download to save a step of copying and pasting

    but on to what I have so far...when I use the following code it will echo the correct # of records...when it moves on to the actual .csv only 1 record is displaying - this code is the closest I've come even if it only displays one record on the .csv

    PHP Code:
    <?
        $query
    ="SELECT name, value, maincategory FROM domains WHERE maincategory='" $_GET['valuebycategory'] . "' ORDER BY name ASC";
        
    $result=mysql_query($query) or die(mysql_error());
        
    $row=mysql_fetch_array($result);
        
    $numrows=mysql_num_rows($result);
        echo 
    $numrows ' records have been found.';

        
    $fp=fopen('newappraisals.csv','w');

        while(
    $row=mysql_fetch_array($result))
          {
            
    $string='"' $row['name'] . '","' $row['value'] . '"' "\n";
          }

        
    flock($fp,LOCK_EX);
        
    fputs($fp,$string);
        
    flock($fp,LOCK_UN);
        
    fclose($fp);
    ?>
    as for bypassing viewing the .csv and having it prompt for a download instead - I had very little luck in finding the appropriate header() to use...if someone could point me in the right direction it would be much appreciated

    thanks for the help!
    Last edited by harlequin2k5; 02-14-2008 at 03:43 AM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    use fputcsv to format lines:

    http://www.php.net/manual/en/function.fputcsv.php

    but I think your problem is in query not in how you build csv.

    use mysql_num_rows to see how many rows the query returns and echo/print or print_r each row to see results

    I don't understand the part with header, give more details.

    best regards

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Your fputs() command must be inside your "while" loop. As oesxyl mentioned, you should use fputscsv to properly format the fields with quotes and commas.

    You don't seem to understand how your data is retrieved from your database. You immediately fetch one row right after you build the resultset, but then your "while" loop fetches again before you've done anything with the first row you fetched.

    1. Build the text of the query and assign to variable (as you've done properly)

    2. Open the query for fetching using mysql_query() (as you've done properly)

    3. Use a looping mechanism (while, for, etc) to fetch all rows into an array, or process each row as you fetch it (as you've not done properly)

  • Users who have thanked Fumigator for this post:

    harlequin2k5 (02-14-2008)

  • #4
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts
    thank you fumigator - that helps a great deal

    about the echo before the while loop - I was just looking - the echo wasn't really there to server any real purpose and I should have removed it before posting here

    I will take a run at correcting my current while statement

    thanks!

  • #5
    Regular Coder harlequin2k5's Avatar
    Join Date
    Sep 2005
    Location
    Holiday, FL
    Posts
    635
    Thanks
    18
    Thanked 0 Times in 0 Posts
    I did not use the fputcsv but this provided exactly what I wanted...

    PHP Code:
    <?
        $query
    ="SELECT name, value, maincategory FROM domains WHERE maincategory='" $_GET['valuebycategory'] . "' ORDER BY name ASC";
        
    $result=mysql_query($query) or die(mysql_error());

        
    $fp=fopen('newappraisals.csv','w');

        while(
    $row=mysql_fetch_array($result))
          {
            
    $string='"' $row['name'] . '","' $row['value'] . '"' "\n";

        
    flock($fp,LOCK_EX);
        
    fputs($fp,$string);
        
    flock($fp,LOCK_UN);
          }
        
    fclose($fp);
    ?>
    to revisit the 2nd thing I'd like to do with this csv is rather than have it update the file on the server (the user will not have access to the server files) it would be preferable that after the file is updated the user then either is prompted to download the file or have the file download from a link...this way they can have the file local on their own machine
    Last edited by harlequin2k5; 02-14-2008 at 06:04 AM.

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by harlequin2k5 View Post
    as for bypassing viewing the .csv and having it prompt for a download instead - I had very little luck in finding the appropriate header() to use...if someone could point me in the right direction it would be much appreciated
    The manual page on header() has all the information you need if you read the comments.


  •  

    Posting Permissions

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