I've been trying to troubleshoot a program I'm trying to build and I've managed to figure out what is causing the problem, however to fix it I have been unsuccessful.
I am using the document object model function in php to take an array of links from a database and process them one by one, however it appears that in the @$doc->loadHTMLFile("$alllinks"); line there is a problem with this function accepting array values from the database.
what I find puzzling however is if I create a general array it works, but from the database (or a few other methods) it simply fails.
here is the version that works:
<?php session_start();
// EDIT: Use a custom function to do the
// reverse of SORT_NUMERIC with asort
function height_compare($a1, $b1)
{
if ($a1 == $b1) {
return 0;
}
return ($a1 > $b1) ? -1 : 1;
}
$all_links = array('http://example.com/klvvobj','http://example.net/s/109228626');
foreach($all_links as $alllinks){
$doc = new DOMDocument();
// Okay this is HTML is kind of screwy
// So we're going to suppress errors
@$doc->loadHTMLFile("$alllinks");
// Get all images
$images_list = $doc->getElementsByTagName('img');
$images = array();
foreach($images_list as $image) {
// Get the src attribute
$image_source = $image->getAttribute('src');
if (substr($image_source,0,7)=="http://"){
$image_size_info = getimagesize($image_source);
$images[$image_source] = $image_size_info[1];
print_r($images);
}
}
// Do a numeric sort on the height
uasort($images, "height_compare");
$tallest_image = array_slice($images, 0,1);
$mainimg = key($tallest_image);
echo "<img src='$mainimg' />";
}
print_r($images);
?>
and here is the version that fails:
<?php session_start();
include 'functions.php';
$query = mysql_query("SELECT * FROM links");
function height_compare($a1, $b1)
{
if ($a1 == $b1) {
return 0;
}
return ($a1 > $b1) ? -1 : 1;
}
while($result = mysql_fetch_array($query)) {
$link = $result['link'];
$doc = new DOMDocument();
// Okay this is HTML is kind of screwy
// So we're going to suppress errors
@$doc->loadHTMLFile($link);
// Get all images
$images_list = $doc->getElementsByTagName('img');
$images = array();
foreach($images_list as $image) {
// Get the src attribute
$image_source = $image->getAttribute('src');
if (substr($image_source,0,7)=="http://"){
$image_size_info = getimagesize($image_source);
$images[$image_source] = $image_size_info[1];
print_r($images);
}
}
// Do a numeric sort on the height
uasort($images, "height_compare");
$tallest_image = array_slice($images, 0,1);
$mainimg = key($tallest_image);
echo "<img src='$mainimg' />";
}
echo '<a href="logout.php">Log out</a>';
?>