0

Below is my script which showing error as $csvfileoutput is undefined variable. As I have defined this variable.

define('CSV_PATH','/var/www/Scripts/csvfiles/');
$csv_fileoutput = CSV_PATH . "top_selling_Category_output_.csv";
$csvfileoutput  = fopen($csv_fileoutput, 'w');
fputcsv($csvfileoutput, array('Part URL'));

$website= "https://www.website.com";

function scrapurls($val, $tagname, $attribute,$value){
        $html = file_get_contents($val);
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        $links = array();
        $equivalentparts = array();
        $divs = $dom->getElementsByTagName($tagname);
        foreach($divs as $div) {
          $cl = $div->getAttribute($attribute);
          if ($cl == $value) {
                //echo "\nID: ".$cl;
            $hrefs = $div->getElementsByTagName("a");
            foreach ($hrefs as $href){
               $links[] = $href->getAttribute("href");
               $equivalentparts[] = $href->nodeValue;               
               }
          }
        }
       array_walk($links, function (&$value, $key) { $value="https://www.website.com$value"; });
       return(array_unique($links));
}

$urls = array();
$urls = array('https://www.website.com/topsellers/?vertical=3');
foreach ($urls as $val){
        $pageinateurl = array($val);
        findpagesurls($val);
     }

function findpagesurls($val){
         $pageinateurl = array();
         $pageinateurl = scrapurls($val, "div","style", "display: inline-block;");
         echo "\nPage 1: Total Urls - ".count($pageinateurl) . "\n\n"; 
         foreach ($pageinateurl as $produrl){ 
                scrapdata($produrl); 
        }
}
function scrapdata($producturl) {
        $producturl = trim($producturl); 
        echo $producturl . "\n";   
        fputcsv($csvfileoutput, array($producturl));
    }

?>
John
  • 61
  • 8
  • Your variable `$csvfileoutput` is out of scope in the `scrapdata()` function you need to pass it to the function as you did it already with other variables. That is why it is undefined inside the function. – Rizier123 Sep 16 '16 at 08:48

3 Answers3

0

In function scrapdata this variable is not defined. Read about variables scope.

http://php.net/manual/en/language.variables.scope.php

And the best solution will be when you send this variable to this function as parameter

nospor
  • 4,190
  • 1
  • 16
  • 25
0

Take a look at the scope of variable http://php.net/manual/en/language.variables.scope.php

<?php
  $a = 1; /* global scope */ 

  function test()
  { 
    echo $a; /* reference to local scope variable */ 
 } 

 test();
 ?>
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

HotFix, But you should not do it. Access global variable inside function is not good.

function scrapdata($producturl) {
        global $csvfileoutput;
        $producturl = trim($producturl); 
        echo $producturl . "\n";   
        fputcsv($csvfileoutput, array($producturl));
}

It should be.

function scrapdata($producturl,$csvfileoutput) {
        $producturl = trim($producturl); 
        echo $producturl . "\n";   
        fputcsv($csvfileoutput, array($producturl));
}
Ngô Văn Thao
  • 3,671
  • 1
  • 20
  • 24