Jump to content

DO WHILE ARRAY


pontypete

Recommended Posts

$backlinkSources = array();
$result = mysql_query("SELECT website FROM `free_listings`");

while($row = mysql_fetch_assoc($result))
{
  
  $backlinkSources[] = $row;
}

print_r($backlinkSources);

 

Returns these errors:

 

Array ( [0] => Array ( [website] => http://www.safehandsplans.co.uk ) [1] => Array ( [website] => http://www.google.com ) )

Warning: file_get_contents() expects parameter 1 to be string, array given in /home/content/95/9771895/html/checktest.php on line 39

 

Warning: Illegal offset type in /home/content/95/9771895/html/checktest.php on line 45

 

Warning: file_get_contents() expects parameter 1 to be string, array given in /home/content/95/9771895/html/checktest.php on line 39

 

Warning: Illegal offset type in /home/content/95/9771895/html/checktest.php on line 45

Array ( )

 

Thanks in advance!

Link to comment
Share on other sites

$backlinkSources = array();
$result = mysql_query("SELECT website FROM `free_listings`");

while($row = mysql_fetch_assoc($result))
{
  
  $backlinkSources[] = $row;
}

print_r($backlinkSources);

 

Returns these errors:

 

Array ( [0] => Array ( [website] => http://www.safehandsplans.co.uk ) [1] => Array ( [website] => http://www.google.com ) )

Warning: file_get_contents() expects parameter 1 to be string, array given in /home/content/95/9771895/html/checktest.php on line 39

 

Warning: Illegal offset type in /home/content/95/9771895/html/checktest.php on line 45

 

Warning: file_get_contents() expects parameter 1 to be string, array given in /home/content/95/9771895/html/checktest.php on line 39

 

Warning: Illegal offset type in /home/content/95/9771895/html/checktest.php on line 45

Array ( )

 

Thanks in advance!

I don't see 45 lines, neither the lines that gives the errors.

 

What the code does is looping through the mysql_query's result. What it loops through is arrays of all the columns you've selected in your mysql_query. You are assigning these to a new array. This new array will therefor be an array of arrays.

do this after the loop to see the content of the array $backlinkSources:

echo '<pre>';
print_r($backlinkSources);
echo '</pre>';

You will probably need to do this:

foreach($backlinkSources AS $backlinkSource){
file_get_contents($backlinkSource['website']);
}

Or just do this while you loop through the results:

while($row = mysql_fetch_assoc($result))
{
  
  $backlinkSources[] = $row['website'];
}

The latter is probably the best.

 

Another little thing to note is what would happen if the mysql_query resulted in no rows?

You would never create the array $backlinkSources, and later would try to use it.

Do this before the loop to make sure it exists:

$backlinkSources = array();

Or you could just check if the mysql_query resulted in 0 rows and terminate the execution of the rest of the related code:

if(!mysql_num_rows($result)){
return; //should terminate the function if this is a function
}

Link to comment
Share on other sites

/*

This is the part I need, but is corrupting everything else...

$backlinkSources = array();
$result = mysql_query("SELECT website FROM `free_listings`");

while($row = mysql_fetch_assoc($result))
{  
  $backlinkSources[] = $row;
}

print_r($backlinkSources);*/

$backlinkSources = array ('http://www.safehandsplans.co.uk','http://www.google.com');

$reportArray = array();

$falseFound = false;

foreach ($backlinkSources as $source) {
  
    $html = file_get_contents ( $source );

    $links = ExtractHrefLinks($html);
    
    if (CheckForTargetUrl($links, $url) === false) {
        $falseFound = true;
        $reportArray[$source] = 0;
    } else {
        $reportArray[$source] = 1;
    }
}


print_r($reportArray);


if ($falseFound === true) {
    GenerateReportEmail($email, $fromEmail, $subject, $body, $url, $reportArray);
}

function GenerateReportEmail($email, $fromEmail, $subject, $body, $url, $reportArray) {

    $header = "From: " . $fromEmail . "\r\n"; 

    foreach ($reportArray as $key => $report) {
        if ($report == false) {
            $body .= "Backlink to $url not found on: $key\n";


		//update as link 0

		$sqlUpdate = "UPDATE free_listings SET backlinkactive='0' WHERE website='$key'";		
		mysql_query($sqlUpdate) or die (mysql_error());

        }
    }

    mail($email, $subject, $body, $header);
}


function CheckForTargetUrl($links, $target) {    

    foreach ($links as $link) {
        if (strpos($link, $target) !== false) {
            return true;
        }
    }


    return false;
} 
                          
function ExtractHrefLinks($html) {

    $dom = new DOMDocument;
    $linkUrls = array();

    @$dom->loadHTML($html);
     
   
    $links = $dom->getElementsByTagName('a');
    foreach ($links as $link){
     
        $linkUrls[] = $link->getAttribute('href');
    }
    
    return $linkUrls;
}

Link to comment
Share on other sites

As I said in my earlier post:

$backlinkSources[] = $row['website'];

 

 

Oh and sorry, I didn't see you created the $backlinkSources array before the mysql_query. If it is a function, you may still want to terminate the execution of the rest of the code when there is no results from the mysql_query.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.