Creating an object for each element in an array

Discussion in 'PHP' started by jnelson563, Dec 2, 2010.

  1. #1
    Hello.

    I am taking input from a <textarea> and trying to create an object using input from each line in the textarea.

    So far I have

    foreach(array as row_input)
    objectarray = new Class(row_input)

    But, something keeps messing up. I can post the full code need be, but if theres a general solution when it comes to using foreach to create objects accepting each line from a textarea that'd be great!

    I guess, the objects are being created successfully, but the results from scraping Google are not accurate...That's the main issue.

    Thanks
     
    Last edited: Dec 2, 2010
    jnelson563, Dec 2, 2010 IP
  2. Minimal Hank

    Minimal Hank Peon

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Please elaborate - what exactly does not work ?
     
    Minimal Hank, Dec 2, 2010 IP
  3. jnelson563

    jnelson563 Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here is the code that explodes the input from a <textarea> to create an array that i can iterate through and create an object for each line.
    
    <?php
    $seperated_keys = explode("\n", $keys);
    $i = 0;
    $array = array();
    foreach($seperated_keys as $row){
      $array[$i] = new Scraper("$site", $row);
      $i++;
      }
    ?>
    
    PHP:
    Here is the class that is being instantiated

    
    <?php
    class Scraper{
    	protected $domain, $keywords, $singleKeyword;
    	
    	public function __construct($website, $wordarray){
    		$this->domain = $website;
    		$this->keywords = $wordarray;
    		
    		$this->scrape();
    	}
    	 //google needs plus signs in the keyword, so were adding them with this
    	public function addSymbols(){
    		$singleWords = explode(" ", $this->keywords);
    		$i=0;
    		$a="";
    		$found="";
    		reset ($singleWords) ;
    		foreach($singleWords as $line){
    			$a .= $singleWords[$i]."+";
    			$i++;
    		}		
    		$this->singleKeyword = $a;
    
    	}
    	//while an iterator is less than 20, it scrapes google results with 1 result, checks if the site is there, if not then scrapes with 2 results, etc
    	function scrape(){
    		$this->addSymbols();
    		
    		$iterator = 1;
    		$pattern = '/'.$this->domain.'/';
    		while ($iterator < 20){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "http://www.google.com/search?hl=en&pws=0&num=$iterator&q=".$this->singleKeyword );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);     				
    		
    			if (preg_match($pattern, $output)){
    				echo $this->domain . " found at position $iterator for".$this->keywords." <br/>";
    				$iterator = 1;
    				$found = 1;
    				break;
    			}
    			else{
    				$found = 0;		
    
    			}
    
    			$iterator++;
    
    		}//end the loop scanning results
    
    		if($found==0)
    			echo $this->domain . " is not in the top 20 for".$this->keywords."<br/>";
    			$iterator = 1;
    		
    		$this->cleanUp();
    	}
    	//this takes up space
    	function cleanUp(){
    
    	}
    	
    }
    
    ?>
    
    PHP:
     
    Last edited: Dec 2, 2010
    jnelson563, Dec 2, 2010 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Nasty code you wrote.
    Two things 2 say :
    1.You don't know what foreach does....
    2.You do know that keywords can be composed of multiple words and not just one word right ?

    Here is the proper class you wanted :
    
    class Scraper{
        
            protected $domain, $keywords, $singleKeyword;
            
            public function __construct($website, $wordarray){
                $this->domain = $website;
                $this->keywords = $wordarray;
                
                $this->scrape();
            }
            
            public function addSymbols(){
                $singleWords = explode(",", $this->keywords);
                $a="";
                if(is_array($singleWords) && count($singleWords) > 0)
                {
                    foreach($singleWords as $line){
                        $a .= '+'.$line;
                    }
                } else {
                    $a = '+'.$singleWords;
                }       
                $this->singleKeyword = $a;
            }
            
            function scrape(){
                $this->addSymbols();
                        
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, "http://www.google.com/search?hl=en&pws=0&num=20&q=".$this->singleKeyword );
                curl_setopt($ch, CURLOPT_USERAGENT,'Googlebot/2.1 (http://www.googlebot.com/bot.html)');
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $output = curl_exec($ch);
                curl_close($ch);                    
                
                $found = 0;
                
                $dom = new DOMDocument();
                @$dom->loadHTML($output);
                $xpath = new DOMXPath($dom);
                $hrefs = $xpath->evaluate("//div[@id='ires']/ol/li/h3[@class='r']/a");
                for ($i = 0; $i < $hrefs->length; $i++) {
                    $href = $hrefs->item($i);
                    $url = $href->getAttribute('href');
                    
                    if(strpos($url,$this->domain) !== false)
                    {
                        $found=1;
                        echo $this->domain." found at position ".$i." for ".$this->keywords." <br/>";
                        break;
                    }
                }
                
                if($found==0)
                    echo $this->domain . " is not in the top 20 for".$this->keywords."<br/>";
            }
            
            function cleanUp(){
            
            }
        }
    
    PHP:
     
    Last edited: Dec 3, 2010
    tvoodoo, Dec 3, 2010 IP
  5. drctaccess

    drctaccess Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Use this code:
    
    <?php
    $keys = 'google\nyahoo\nfacebook\n';
    $site = 'yahoo.com';
    $array = array();
    require_once('scrapper.php');
    foreach(explode('\n', $keys) as $row)
    {
            if(!empty($row))
            {
              $array[] = new Scraper($site, $row);
            }
    }
    echo '<pre>';
    print_r($array);
    echo '</pre>';
    ?>
    
    Code (markup):
    use the scrapper class provided by tvoodoo
    It will work
     
    Last edited: Dec 3, 2010
    drctaccess, Dec 3, 2010 IP
  6. jnelson563

    jnelson563 Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks! I'll try this out. This was actually the first object oriented thing I've tried in PHP so I appreciate the patience.
     
    jnelson563, Dec 3, 2010 IP
  7. jnelson563

    jnelson563 Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It worked great when I kept the original addSymbols function.

    Thanks for your help!
     
    jnelson563, Dec 3, 2010 IP
  8. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #8
    Well , guess it depends what you needed it for :)

    You're welcome !
     
    tvoodoo, Dec 3, 2010 IP