Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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); 
?>
share|improve this answer
    
It works perfect, thanks a lot! –  nb.ag Sep 26 '13 at 10:35

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

share|improve this answer
    
It's exactly what I'm looking for. Works fine, many thanks! –  nb.ag Sep 26 '13 at 10:57

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

share|improve this answer
    
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". –  nb.ag Sep 26 '13 at 10:45
    
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. –  djot Sep 26 '13 at 12:15
    
Ok, thanks for your advice! –  nb.ag Sep 26 '13 at 12:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.