Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

php - Why is "fputcsv" output a single column instead of multiple columns and rows

I am an absolute beginner with PHP. My problem is twofold, first I have a list of ancestor names and each name has 13 key => value pairs. I need my output to have a single row of headers ($keys) and mutiple rows of matching $values. My code at present repeats the header keys with each name and all the output is in one column. I think I need to exclude the tag for the headers and place them separately in the CSV.` I have looked at similar problems in Stackoverflow and tried to adopt their resolutions without success.


          include_once('_inc/simple_html_dom.php');

      //create an array of all URLs we need to target
      $pages = array(  
        "http://fammilytree.loc/surnames/A%20Surnames/Adams/James%20Adams%201864.php",
        "http://familytree.loc/surnames/A%20Surnames/Adams/John%20Adams%201901.php",
        "http://familytree.loc/surnames/A%20Surnames/Anderson/Jeals%20Anderson%201718.php",
        "http://familytree.loc/surnames/A%20Surnames/Arnett/Ann%20Arnett%201781.php");

     //We are initializing an array to hold the values of each <TD> from an ancestor's page
      $ancestor = array();
?>

<html>
    <body>
        <?php
        
        //this loop goes through our "pages" array and then for each page in the array...
        foreach($pages as $page){
            //...calls the function which pulll all of the HTML from the page into an array 
            $html = file_get_html($page);
        
            //I think we need a multi-dimensional array, using name of ancestor as index for next array
            // $ancestor = array()
   
            
            
        // then FOR EACH element in the array of HTML, find the TD value
           foreach($html->find('td') as $element){
           //and put the value in our Ancestor array
           $ancestor[] = $element->innertext.",";   
           }
               
           }
           //display the full Ancestor array to the page
           print_r($ancestor);
           
           
           //OPen the file called FILE.csv with 'w'=write permissions
           $handle = fopen('export.csv', 'w');
           
               foreach ($ancestor as $fields) {
                  
               fputcsv($handle, (array) $fields);
               }
               fclose($handle);          
        ?>
    </body>
</html>```
question from:https://stackoverflow.com/questions/66068651/why-is-fputcsv-output-a-single-column-instead-of-multiple-columns-and-rows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...