How to Multiple Arrays (+formation) using PHP and Simple HTML DOM

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 :

Answers


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.


Need Your Help

Different .gitconfig for a given subdirectory?

git email git-config

I use two different git emails, one for work and one for public projects. Initially I thought that I could create a separate .gitconfig with a different email in a folder where all my public repos ...

CMutablePointer - how to access it?

swift

Can anybody explain how to access CMutablePointer&lt;CGPoint&gt; presented below? I can't find the syntax for it. It used to be -&gt; in Objective-C, but here none of my solutions works. The solution