Вопрос

I am new to PHP and got an assignment seems bit tough. I need to extract the price table from this pagehttp://www.kieskeurig.nl/spiegelreflexcamera/nikon/d3200_body/prijzen/bezorgen/1052716#prijzen

So far I have done the code:

<?php
include('simple_html_dom.php');
$url = 'http://www.kieskeurig.nl/spiegelreflexcamera/nikon/d3200_body/prijzen/bezorgen/1052716#prijzen';

$html = file_get_html($url);

foreach($html->find('table[id="priceTable"]') as $data) {
    foreach($data->find('img[width="150"]') as $d) {
        echo $d-> alt. "<br/>";
    }
}
foreach($html->find('div[class="il"]') as $d1) {
    foreach($d1->find('a[rel="nofollow"]') as $d2) {
        echo $d2-> innertext. "<br/>" ;
    }
}
?>

It gives me the result but I want shop name and respective price on the same row. It should look like the following:

Digitalstreet  € 332,00 

Cameratop      € 332,00

But am getting the output as follow:

Digitalstreet
Cameratop 
€ 332,00
€ 332,00

Can anybody please guide me with this.

Это было полезно?

Решение

I would suggest grabbing the table row for row, and then finding the name and price per row. This way (if something is missing), you are sure the name and price belong together.

<?php
include('simple_html_dom.php');
$url = 'http://www.kieskeurig.nl/spiegelreflexcamera/nikon/d3200_body/prijzen/bezorgen/1052716#prijzen';

$html = file_get_html($url);

foreach($html->find('table[id="priceTable"]') as $data) {
    foreach($html->find('tr') as $rowdata) {
        $name = 'unknown';
        $price = '-';
        foreach($rowdata->find('img[width="150"]') as $namedata) {
            $name = $namedata->alt;
        }
        foreach($rowdata->find('div[class="il"]') as $d) {
            foreach($d1->find('a[rel="nofollow"]') as $d2) {
                $price = $d2->innertext;
            }
        }

        echo $name.' '.$price.'<br/>';
    }
}
?>

Off course you can add everything to an array instead of echoing it, and the 'default' values can be changed as well according to your needs

Другие советы

I'm guessing the first loop loops over the prices and the second loop loops over the names. Assuming both lists are always the same size you can easily store the information in 1 or more arrays and loop over them later.

<?php
include('simple_html_dom.php');
$url = 'http://www.kieskeurig.nl/spiegelreflexcamera/nikon/d3200_body/prijzen/bezorgen/1052716#prijzen';

$html = file_get_html($url);

$prices = array();
$names = array();

foreach($html->find('table[id="priceTable"]') as $data) {
    foreach($data->find('img[width="150"]') as $d) {
        $prices[] = $d->alt;
    }
}
foreach($html->find('div[class="il"]') as $d1) {
    foreach($d1->find('a[rel="nofollow"]') as $d2) {
        $names[] = $d->alt;
    }
}

foreach ($names as $key => $name)
{
    echo "$name: " . $prices[$key];
}

I can't test if this works, but it should help you to get the right idea.

I don't know how the array looks like. What you can do is add both the results in an array and loop any one of the array.

For eg:

foreach($html->find('table[id="priceTable"]') as $data) {
foreach($data->find('img[width="150"]') as $d){
    $price[] = $d->alt; 
}}
foreach($html->find('div[class="il"]') as $d1){
foreach($d1->find('a[rel="nofollow"]') as $d2){
   $camName[] = $d2->innertext;
}
}
// now loop through the array
echo "<table>"
foreach($camname as $key=>$name){
   echo "<tr><td>{{$name}}</td><td>{{$price[$key]}}</td></tr>";
}
echo "</table>";
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top