1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help using XPath & PHP to get thumbnails with corresponding links

Discussion in 'PHP' started by Big Fish, May 13, 2010.

  1. #1
    I'm trying to create a PHP script that uses XPath to "scrape" all the thumbnails, with corresponding links, from an HTML document. The script is getting the correct data, it's just not outputting it correctly. I'm not a PHP person, so I was trying to piece things together and I'm not getting anywhere.

    The code below displays all the links and then all the image urls.
    $hrefs = $xpath->query("//a[@class='blah']/@href");
    $thumbnails = $xpath->query("//img[@class='blah']/@src");
    
    foreach ($hrefs as $h) {
    	$url = $h->nodeValue;
    	echo $url;
    }
    foreach ($thumbnails as $t) {
    	$thumb = $t->nodeValue;
    	echo $thumb;
    }
    Code (markup):
    How can I get the output to display like a normal thumbnail for each one?

    I tried the code below, but that doesn't work.
    echo "<a href=" . $url . "><img src=" . $thumb . " /></a><br />";
    Code (markup):
     
    Last edited: May 13, 2010
    Big Fish, May 13, 2010 IP
    Silver89 likes this.
  2. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #2
    We'd need to see the output for this? Are the urls to the exact image file location?
     
    Silver89, May 13, 2010 IP
  3. Big Fish

    Big Fish Member

    Messages:
    23
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #3
    $hrefs = $xpath->query("//a[@class='blah']/@href");
    $thumbnails = $xpath->query("//img[@class='blah']/@src");
    
    foreach ($hrefs as $h) {
    	$url = $h->nodeValue;
    	echo $url;
    }
    foreach ($thumbnails as $t) {
    	$thumb = $t->nodeValue;
    	echo $thumb;
    }
    Code (markup):
    Output:
    http://www.site1.comhttp://www.site2.comhttp://www.site3.comhttp://www.site4.comhttp://www.site5.comhttp://www.site1.com/images/1.jpghttp://www.site2.com/images/2.jpghttp://www.site3.com/images/3.jpghttp://www.site4.com/images/4.jpghttp://www.site5.com/images/5.jpg
    Code (markup):
    Desired Output:
    <a href="http://www.site1.com"><img src="http://www.site1.com/images/1.jpg"></a><br />
    <a href="http://www.site2.com"><img src="http://www.site2.com/images/2.jpg"></a><br />
    ...
    Code (markup):
     
    Big Fish, May 13, 2010 IP
  4. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    I haven debugged this code... But this may work
    <?php
    $url_n_img = array(
        0 => array(
            'url' => '',
            'img' => ''
        )
    );
    $html = file_get_contents('http://www.intechgrity.com/2010/05/single-indexphp-php-script-to-load.html');
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    $hrefs = $xpath->query("//a[@class='blah']/@href");
    $thumbnails = $xpath->query("//img[@class='blah']/@src");
    $i=0; $j=0;
    foreach($hrefs as $h) {
        $url_n_img[$i]['url'] = $h->nodeValue;
        $i++;
    }
    foreach($thumbnails as $t) {
        $url_n_img[$j]['img'] = $t->nodeValue;
        $j++;
    }
    //print_r($url_n_img);
    foreach($url_n_img as $u) {
        echo '<a href="'.$u['url'].'"><img src="'.$u['img'].'" /></a>';
    }
    ?>
    PHP:
    What we do is, store the hrefs and thumbs in another array and fetch the values later with our desired markup! Do let me know if it works!
     
    swashata, May 13, 2010 IP
  5. Big Fish

    Big Fish Member

    Messages:
    23
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #5
    You are a genius!

    Does anyone else have any input? I know sometimes there's a better way to do things. I don't know much about PHP though.
     
    Big Fish, May 13, 2010 IP
  6. Big Fish

    Big Fish Member

    Messages:
    23
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #6
    What would be the best way to limit the script to the first 10 thumbnails?
     
    Big Fish, May 13, 2010 IP
  7. job0107

    job0107 Peon

    Messages:
    23
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes swashata is a genious,
    We can modify the code slightly to display only ten thumbnails.
    Just change the value of $end to the desired number of thumbnails.
    Of course if the value of $end is more than the number of thumbnails then it will only display the total number thumbnails.
    
    <?php
    $url_n_img = array(
        0 => array(
            'url' => '',
            'img' => ''
        )
    );
    $html = file_get_contents('http://www.intechgrity.com/2010/05/single-indexphp-php-script-to-load.html');
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    $hrefs = $xpath->query("//a[@class='blah']/@href");
    $thumbnails = $xpath->query("//img[@class='blah']/@src");
    $i=0; $j=0;
    foreach($hrefs as $h) {
        $url_n_img[$i]['url'] = $h->nodeValue;
        $i++;
    }
    foreach($thumbnails as $t) {
        $url_n_img[$j]['img'] = $t->nodeValue;
        $j++;
    }
    $start=0;
    $end=10; // How many thumbnails do you want? //
    foreach($url_n_img as $u) {
        if($start<$end){echo '<a href="'.$u['url'].'"><img src="'.$u['img'].'" /></a>';}
        $start++;
    }
    ?>
    
    PHP:
     
    job0107, May 13, 2010 IP
  8. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #8
    Oh Thanks :D ... job0107 tells you the way of putting 10 contents only! Btw, do have a look at here
     
    swashata, May 13, 2010 IP
  9. Big Fish

    Big Fish Member

    Messages:
    23
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #9
    That works beautifully!

    Thanks guys! ;)
     
    Big Fish, May 14, 2010 IP