0

i'm working on a parser to scrap all the prices of a categorie.(i work with simple_html_dom lib) I want to show the result as:

ProductName1 Price


ProductName2 Price


etc...

I have this:

        $prices = $html->find('.price_container');

        $titles = $html->find('.s_title_block');
        
        foreach ($prices as $price) {
            echo 'Precio <br>';
            echo $price->innertext;
            echo '<hr><br>';
        }
        
        foreach ($titles as $title) {
            echo 'Nombre Producto <br>';
            echo $title->innertext;
            echo '<hr><br>';
        }
        
        $products = $title . $price;

        foreach ($products as $product) {
        echo $product->innertext;
        }

It's okay, i have the prices, and the titles, but... It show separately, and i need to show it together..

The result i have with the code i have pasted below is something like that:

Precio 22,50 €

Precio 24,00 €

Precio 27,00 €

Precio 27,00 €

Precio 27,00 €

Precio 19,50 €

Precio 27,00 €

Precio 24,00 €

Precio 22,50 €

Precio 24,00 €

Precio 24,00 €

Precio 27,00 €

Precio 25,50 €

Precio 24,00 €

Precio 24,00 €

Precio 27,01 €

Precio 22,50 €

Precio 30,00 €

Precio 27,00 €

Precio 22,50 €

Precio 25,50 €

Precio 24,00 €

Precio 19,00 €

Nombre Producto ProductName1

Nombre Producto ProductName2

Nombre Producto ProductName3

Nombre Producto ProductName4

Nombre Producto ProductName5

Nombre Producto ProductName6


As i have asked, i need to show like that:

ProductName1 HisPrice

ProductName2 HisPrice

ProductName3 HisPrice

Thank you for your help it will be very apreciate.

Best regards

1 Answers1

0

You can save the data in temporary variables and then use them, for example:

<?php
$tmpPr = [];
$tmpTi = [];

$prices = $html->find('.price_container');
$titles = $html->find('.s_title_block');

foreach ($prices as $price)
    $tmpPr[] = $price;
foreach ($titles as $title)
    $tmpTi[] = $title;

// let's suppose that the amount of prices and titles are the same
$cant = count($tmpPr);

for($i=0; $i<$count; $i++)
    echo $tmpTi[$i],' ',$tmpPr[$i];
// NombreProducto.' '.Precio
?>
  • Really good idea, but when i run it it show me an error: Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\prueba.php on line 29 – A. Sanchez Jun 26 '17 at 09:14
  • Ahh my mistake, I forgot the `;` at the end of the line – Matías Pizarro Jun 26 '17 at 09:34
  • Hello Matías, it's almost done. The result with your advise is this one: ProductName Notice: Array to string conversion in C:\xampp\htdocs\prueba.php on line 30 Array What can i do to solve the error with the string conversion? Thank you so much! – A. Sanchez Jun 26 '17 at 10:57
  • Hello and sorry again, my mistake, I forgot to put the `[$i]` at `$tmpPr[$i]`, tell me if it does work now – Matías Pizarro Jun 26 '17 at 19:43
  • Hello Matias, is working good, the only error is picking and associate the wrong's prices to products. – A. Sanchez Jun 28 '17 at 06:58