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!

3 Answers 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); 
?>
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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])

2 Comments

Exactly, I thought make a collection array like this $books[]; I tried yet and it doesn’t work (I suppose I have to make a foreach for the collection's array). Anyway, thanks for idea of "append"; I'll look at manual for "file_put_contents".
For a small amount of books, it would be ok to store them all in an array and then write this array to a file. BUT even with few data, more to say with thousands of books, you'd better go the way without collecting everything in an array, and instead write (here: append) every single book directly to the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.