1

I have a site with the collection of books; each book have a its own information page (like http://mybookscollection.com/book.php?iditem=2).

<html>
<body>
<div class="book">
<div id="author">Author</div>
<div id="title">Title</div>
<div id="date">Date</div>
</div>
</body>
</html>

I want to extract all books information and put this in a txt file like this.

1; Author1; Title1; Date1
2; Author2; Title2; Date2
3; Author3; Title3; Date3

To parse my html page I use "PHP Simple HTML DOM Parser" from http://simplehtmldom.sourceforge.net/

My php code is

<?php 
//  include HTML DOM Parser from http://simplehtmldom.sourceforge.net/ 
include ('simple_html_dom.php');

// make a loop for looking the first ten books
for ($i=1; $i<=10; $i++)
{

// look a book page by id   
$url = 'http://mybookscollection.com/book.php?iditem='.$i;
$html = file_get_html($url);

// parse the htmls content to get a data
foreach($html->find('div[id=author]') as $element) 
{$author = $element->plaintext;}
foreach($html->find('div[id=title]') as $element)
{$title = $element->src;}
foreach($html->find('div[id=date]') as $element)
{$date = $element->plaintext;}

// put all one book's data in the array "book"
$book = implode(";", array($i, $author, $title, $date));

// HERE NEED TO CREAT A NEW ARRAY "COLLECTION" WHERE PUT ALL "BOOK"'S ARRAY

//use echo to print data in the browser
//echo '<div>'. $book .'</div>';

//write a file with all data
$fp = fopen('result.txt', 'w');
// NORMALY HAVE TO PUT HERE "$COLLECTION" NOT JUST ONE "$BOOK"
fwrite($fp, $book);
fclose($fp); 
}
?>

It works fine and I get all my data. My problem is that I can't put tougher (in the same array "collection") each book's array to print all books information in the same txt file. I know it's a noobie question about a two multidimensional array but I can't figure it out. Many thanks for your help!

4

3 回答 3

2

You are opening the file for every book. Just open it before the loop and close it after the loop.

<?php 
//  include HTML DOM Parser from http://simplehtmldom.sourceforge.net/ 
include ('simple_html_dom.php');

// make a loop for looking the first ten books
$fp = fopen('result.txt', 'w');
for ($i=1; $i<=10; $i++)
{

    // look a book page by id   
    $url = 'http://mybookscollection.com/book.php?iditem='.$i;
    $html = file_get_html($url);

    // parse the htmls content to get a data
    foreach($html->find('div[id=author]') as $element) 
    {$author = $element->plaintext;}
    foreach($html->find('div[id=title]') as $element)
    {$title = $element->src;}
    foreach($html->find('div[id=date]') as $element)
    {$date = $element->plaintext;}

    // put all one book's data in the array "book"
    $book = implode(";", array($i, $author, $title, $date));

    fwrite($fp, $book);
    fwrite($fp, "\n"); # write a newline
}
fclose($fp); 
?>
于 2013-09-26T09:59:13.167 回答
1

Initialize an array

$collection=array();

Then within your loop

array_push($collection,$book);

and you will have an array of books :)

http://php.net/manual/en/function.array-push.php

于 2013-09-26T10:03:39.640 回答
0

So you mean:

// collection of all books
// you have to put this line OUTSIDE your loop, so in front of it
$books = array();
//
//add the next book to books collection
// $books[] = $book;

results in:

// your current book code
$book = implode(";", array($i, $author, $title, $date));
//
// add book to books collection
$books[] = $book;

to be corrected:

You should now write your file (in one go) outside the loop!

or ... just go with what you have without a collection array and "append" data to your file.

(For this see http://php.net/manual/en/function.fopen.php with "mode" option 'a' [plus b for windows])

于 2013-09-26T10:02:45.467 回答