0

Im creating a scraping code that scrape each Address in a specific suburbs. but im stuck in this problem; " Use of undefined constant j - assumed 'j'" and it identified in $target_url can anyone help me this problem?

$arr = array("Illawong 2232",
             "Strathfield 2135", 
             "Croydon 2132",
             "Croydon Park 2133",
             "Burwood 2134",
             "Parramatta 2150",
             "Hurtsville 2220",
             "Seven Hills 2153",
             "Blacktown 2148",
             "Toongabie 2146",
             "Winston Hills 2153",
             "Bondi Beach 2026",
             "Bondi Junction 2022",
             "Coogee 2034",
             "Pymble 2073",
             "Miranda 2228",
             "Caringbah 2229",
             "Sylvania 2224",
             "Drummoyne 2047",
             "Concord 2137"
             );
        $counter = count($arr);
for($j=0;$j<$counter; $j++) 
    {

            $arr2 = array("list-1", "list-2", "list-3","list-4", "list-5");
            $count = count($arr2);
            
            for($i=0;$count>$i;$i++)
                {
            
                    //scrapping starts here
                    $target_url = "http://www.xxxxxxxxx.com.au/buy/".$arr[j]."/".$arr2[i]."?includeSurrounding=false";
                    $html = new simple_html_dom();

                

                    $html->load_file($target_url);

                    foreach($html->find('a[class=name]') as $vcard)
                    {
                       echo $vcard. "<br/>"
                    }
                }
    }
zerconis
  • 11
  • 3

3 Answers3

1

It is clear, you don't program in C. PHP's variables start with $ sign. If there is no dollar sign, they are treated as constants.

$arr[j] should be $arr[$j] and $arr2[i] should be $arr2[$i].

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
1

Use This- You are using a variable without $ sign, thats why it is consider as string 'j'

<?php
$arr = array("Illawong 2232",
             "Strathfield 2135", 
             "Croydon 2132",
             "Croydon Park 2133",
             "Burwood 2134",
             "Parramatta 2150",
             "Hurtsville 2220",
             "Seven Hills 2153",
             "Blacktown 2148",
             "Toongabie 2146",
             "Winston Hills 2153",
             "Bondi Beach 2026",
             "Bondi Junction 2022",
             "Coogee 2034",
             "Pymble 2073",
             "Miranda 2228",
             "Caringbah 2229",
             "Sylvania 2224",
             "Drummoyne 2047",
             "Concord 2137"
             );
        $counter = count($arr);
for($j=0;$j<$counter; $j++) 
    {

            $arr2 = array("list-1", "list-2", "list-3","list-4", "list-5");
            $count = count($arr2);

            for($i=0;$count>$i;$i++)
                {

                    //scrapping starts here
                    $target_url = "http://www.xxxxxxxxx.com.au/buy/".$arr[$j]."/".$arr2[$i]."?includeSurrounding=false";
                    $html = new simple_html_dom();



                    $html->load_file($target_url);

                    foreach($html->find('a[class=name]') as $vcard)
                    {
                       echo $vcard. "<br/>"
                    }
                }
    }
Sunil Rajput
  • 960
  • 9
  • 19
1

When you use variables you need to use $ symbol before them otherwise you will get an error for undefined constant, so change here

$target_url = "http://www.xxxxxxxxx.com.au/buy/".$arr[$j]."/".$arr2[$i]."?includeSurrounding=false";
                                                    //^here       //^here
Fabio
  • 23,183
  • 12
  • 55
  • 64