1

I found my way around web scraping and i'm trying to figure out he proper way to get data from the buffer that is filled with the page.

  • i'm including my variables, that have timezone and simple html dom in it

  • i get the img's and the titles from searching the dom, but can't seem to find a way to have multiple queries with the foreach, i know i'm missing on knowledge here, but i've hit bottom on searching and reading.

  • i get variables from a previous form using get thats why i have the query string on the top.

what i need is :

  • to fetch the data in an array

  • to be able to use them inside a table and such. (image on the left wrapped text on the right and some other data.)

thank you in advance, help is going to be greatly appreciated.

<?php
// includes and variables
include($_SERVER["DOCUMENT_ROOT"] . "/includes/variables.php");                             // all includes
// decleratus
$url = 'http://www.domain.com/file.php?'.$_SERVER['QUERY_STRING'];                  // url+selected from prev
// shd stuff
$html = new simple_html_dom();                                                              // Load shd
$html->load_file($url);                                                                     // Load a file
// 1st array
foreach ($html->find("div.row img") as $varImgStr) 
{           
    $varImg = '<p style="margin-left:25px">' . $varImgStr. '</p>';
    echo $varImg ;
}
// 2nd array
foreach ($html->find("div.row span strong") as $varTitleStr) 
{           
    $varTitle = '<p style="margin-left:25px">' . $varTitleStr. '</p>';
    echo $varTitle ;
}
// does echo full $html output
//echo $html;                                                                               
// clear mem
$html->clear();                                                                             
?>

I'm deploying on my workplace an off-line (no internet needed) app using Php Desktop.

What i'm using :

1 Answer 1

1

Try a multidimensional array:

foreach ($html->find("div.row img") as $varImgStr) 
{           
    $mainArray['varImg'][] = '<p style="margin-left:25px">' . $varImgStr. '</p>';
}

foreach ($html->find("div.row span strong") as $varTitleStr) 
{           
    $mainArray['varTitleStr'][] = '<p style="margin-left:25px">' . $varTitleStr. '</p>';
}

This creates an array with two elements - ['varImg'] and ['varTitleStr']. Each of those elements is an array containing all of the images or titles found. You can print_r($mainArray); to see the structure. Note that this approach depends on finding the same number of images and titles in the same order.

//To print the array in a simple table

echo '<table><thead>
    <th>Image</th><th>Title</th>
    </thead><tbody>';

for ($i=0; $i<count($mainArray['varImg']); $i++) {
     echo '<tr> <td>' . $mainArray['varImg'][$i] . '</td> 
           <td>' . $mainArray['varTitleStr'][$i] . '</tr>';
}

echo '</tbody></table>';

Note that in the for loop, the count is taken from $mainArray['varImg'], not from $mainArray. You can read more about multidimensional arrays here.

Sign up to request clarification or add additional context in comments.

Comments

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.