3

I have an array full of sub-arrays, which in turn are full of source names and their website URLs, such as The New York Times and http://newyorktimes.com.

I'm displaying a list of links where the title links to the url and I would like to show the website's favicon in front of each link - a simple idea in theory.

Right now the link list works fine, the code that pulls the favicon is returning its correct URL, and basically everything is working fine, except that it only works for the first link and the page gets messed up thereafter. I finally realized that since the code needs to be run for each url in the foreach loop, there must be some variable conflict going on when the loop runs the second time.

The problem is that my list is dynamically populated (in WordPress) so I never know the number of links in the array, nor what they are, so what I'm wondering is: Is there a way I can dynamically number or otherwise change the variables each time the foreach cycles around so that there aren't multiple instances of the same variables?

Here's the part of my code that's the issue:

echo '<ul class="ll_list">';
    foreach ( $ll_entries as $ll_entry ) {

        /*----- GET FAVICON -----*/
        /* This code needs to run for each url in the array, pull the
        favicon, and then the favicon's url needs to be echoed down
        below. */

        $url = esc_attr( $ll_entry['ll_url'] );
        $doc = new DOMDocument();
        $doc->strictErrorChecking = FALSE;
        $doc->loadHTML(file_get_contents($url));
        $xml = simplexml_import_dom($doc);
        $arr = $xml->xpath('//link[@rel="shortcut icon"]');
        $favicon = $arr[0]['href'];

        /*-----------------------*/

        echo '<li class="ll_list_item">';

        if (file_exists($favicon)) {
           echo '<img class="ll_logo_img" src="'.$favicon.'" alt="'. esc_attr( $ll_entry['ll_title'] ).'" /> '; 
        } else {
           echo '<span class="dashicons dashicons-arrow-right"></span> ';
        }

        echo '<a href="' . esc_attr( $ll_entry['ll_url'] ) . '" title="Visit ' . esc_attr( $ll_entry['ll_title'] ) . ' &raquo;" target="_blank">';
        echo esc_attr( $ll_entry['ll_title'] );
        echo '</a>';
        echo '</li>';
     }
echo '</ul>';

The correct variable would then also have to be called to the <img> tag below, which is only echoed if the favicon file exists... so that maybe throws a wrench in the works too?

I've searched around a lot about this and I've found a bit of documentation about "variable variables." I'm not entirely sure how to use them here, but there's nothing in the array that I could use as a variable anyway, unless there's some way to strip the special characters from the url and use that.

Any help would be much appreciated, thank you! If there's an easier way to accomplish my goal I'm all ears.

akmozo
  • 9,829
  • 3
  • 28
  • 44
Shoelaced
  • 846
  • 5
  • 27
  • Did you got some errors (take a look on your server / PHP logs ) ? – akmozo Jan 10 '16 at 01:24
  • You are giving too much information. It sounds like all you need to do is do a for loop with count(array) as the number you are counting d own from, or just append a count to the variable you are working with. But it's hard to tell because you've just dumped so much info, isolate it more. – Andrew Jan 10 '16 at 01:27
  • @Shoelaced BTW, your posted code is fine, I tested it with 3 URL (NYTimes, SO and google) and it's working well, so try to verify other points ... – akmozo Jan 10 '16 at 01:35

1 Answers1

1

If you suspect there's a variable naming conflict in your get_favicon code, put it into a function. Variables in a function are (by default) local to that function and can't mess up outer scope.

function get_favicon($ll_entry) {
    $url = esc_attr( $ll_entry['ll_url'] );
    $doc = new DOMDocument();
    $doc->strictErrorChecking = FALSE;
    $doc->loadHTML(file_get_contents($url));
    $xml = simplexml_import_dom($doc);
    $arr = $xml->xpath('//link[@rel="shortcut icon"]');
    $favicon = $arr[0]['href'];
    return $favicon;        
}

Then use this function to populate your $favicon variable:

$favicon = get_favicon($ll_entry);
// use it as before

Make sure you don't define your function in a loop. Function definition should be outside of any loops you may have.

weirdan
  • 2,499
  • 23
  • 27
  • Wow, I just spent the _strangest_ half hour trying to figure out what on earth is going on... I'm still not sure I know but I've tested everything and I've finally concluded that the function must be pulling some kind of script from my second link instead of the icon because without that link it works. It's also getting the relative path for some of them and I need the absolute path. I'll post a new question about how to fix that but I'm going to give the check to you because you answered my question about how to prevent variable conflicts. And it did help in figuring it out, so thanks!! – Shoelaced Jan 10 '16 at 21:05