简体   繁体   中英

php –multidimensional array and looping

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!

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); 
?>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM