Jump to content

PHP error, please help ( SOLVED )

BakaPetra
Go to solution Solved by BakaPetra,

I fixed it, my dumbass forgot to put an if before the   foreach (str_get_html($table)->find('tr') as $tr)  line so if the $table variable is empty the function returns FALSE...

 

Fix line:   if ($table != null ){

Hello there, I'm writing a PHP program that scrapes the Mitas's website and matches the tire pressure tables information with my companies one. I'm using simple html dom.

I'm getting the following error every single time:

 

Fatal error: Uncaught Error: Call to a member function find() on bool in C:\xampp\htdocs\mitas\scraper.php:465 Stack trace: #0 C:\xampp\htdocs\mitas\scraper.php(650): parse_MITAS_html_table(NULL) #1 C:\xampp\htdocs\mitas\generate_MITAS_final_table.php(10): generate_MITAS_final_table('mitas-dump.csv', 'final_MITAS_tab...') #2 {main} thrown in C:\xampp\htdocs\mitas\scraper.php on line 465

 

My code at line 465 is the following: 

 

/******************************************************************************
>> parse_MITAS_html_table
 
Extract data from HTML table
 
INPUT: string containing HTML table
RETURNS: array with parsed data
******************************************************************************/
function parse_MITAS_html_table($table) {
    $ret = array();
    $retkey = 0;
    $header = array();
    foreach (str_get_html($table)->find('tr') as $tr) {
        if (!isset($tr->attr['class'])) {
            foreach(str_get_html($tr)->find('th') as $th) {
                $header[] = $th->plaintext;
            }
        }
    }
    
    $headerlength = count($header);
 
        
    foreach(str_get_html($table)->find('tr') as $rowkey => $row) {
        $rowval = array();
        foreach($row->find('td') as $colkey => $col) {
            $rowval[] = $col->innertext;
        }
        
        if (!empty($rowval)) {
            for ($i = 0; $i<$headerlength; $i++) {
                $ret[$retkey]['Sebesség'] = $rowval[$headerlength];
                $ret[$retkey]['Nyomás'] = $header[$i];
                $ret[$retkey]['Súly'] = $rowval[$i];
                $retkey++;
            }
        }
    }
    
    return $ret;
}
 
This is how I call the function:
 
/*****************************************************************************************
 
>> generate_MITAS_final_table
 
Generate table with the product code, speed, pressure and weight values.
    
INPUT: string csv input filename, string csv output filename
RETURNS: TRUE upon success
    
******************************************************************************************/
 
function generate_MITAS_final_table($infile, $outfile) {
    $csv = csv_to_array($infile,';');
    $dataarray = array();
    $result = array();
    $resultcounter = 0;
    if (!empty($csv)) {
        foreach ($csv as $csvkey => $csvitem) {
            print $csvitem['Cikknév'];
            $dataarray = parse_MITAS_html_table($csvitem['markup']);
            
            if (!empty($dataarray)) {
                $datalength = count($dataarray);
                for ($c=0; $c<$datalength; $c++) {
                    $result[$resultcounter]['Cikkszám'] = $csvitem['Cikkszám'];
                    $result[$resultcounter]['Sebesség'] = $dataarray[$c]['Sebesség'];
                    $result[$resultcounter]['Nyomás'] = $dataarray[$c]['Nyomás'];
                    $result[$resultcounter]['Súly'] = $dataarray[$c]['Súly'];       
                    //$result[$resultcounter]['EAN'] = $csvitem['EAN'];
                    //$result[$resultcounter]['Sebesség index'] = $dataarray[$c]['Sebesség'];
                    //$result[$resultcounter]['Load Range / Ply'] = $dataarray[$c]['Load Range / Ply'];             
                    $resultcounter++;
                }
            }
        }
    }
    
    var_dump($result);
    array_to_csv($result, $outfile);
    
    return TRUE;
}
 
 
What can cause this issue? I can't find anything on the internet that helps.

system out println("Please don't drop it Linus!");

Setup:

Thinkpad W530 😕 

 

sus not cis

Some comic sans because why not

 

Link to comment
Share on other sites

Link to post
Share on other sites

I fixed it, my dumbass forgot to put an if before the   foreach (str_get_html($table)->find('tr') as $tr)  line so if the $table variable is empty the function returns FALSE...

 

Fix line:   if ($table != null ){

system out println("Please don't drop it Linus!");

Setup:

Thinkpad W530 😕 

 

sus not cis

Some comic sans because why not

 

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, Megumin4317 said:

I fixed it, my dumbass forgot to put an if before the   foreach (str_get_html($table)->find('tr') as $tr)  line so if the $table variable is empty the function returns FALSE...

 

Fix line:   if ($table != null ){

Mark your own post (or mine ?) as the correct answer so that the post appears solved (even tho you have changed the title). Just press the little tick.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×