Loop issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

Loop issue

Post by afbase »

I am having a problem inside the while loop of the function "mysql_select_array" in the class shown later in the post. I can call "regex_html" like so with out a hitch, and gives me the array I want.

Code: Select all

$link = "http://devdata.worldbank.org/external/CPProfile.asp?PTYPE=CP&CCODE=BEN";
$pattern = '#face="Verdana,Tahoma,Arial,Helvetica">([^<]*)#';
$mika = new gdp();
$mika->regex_html($link, $pattern);
That little script does wonders for me, it gives me the very large array I wanted. The problem is when I do it inside this loop in "mysql_select_array" function. it returns:
Array ( [0] => Array ( ) [1] => Array ( ) )
from the script:

Code: Select all

$mika = new gdp();
$grr = "gdp";
$mika->mysql_select_array($grr);
Don't mind the connect(), database(), and close() inside the "mysql_select_array" function. I made those to ease my pain and suffering with mysql. this is a clip of what the sql returns:
Array ( [0] => http://devdata.worldbank.org/external/C ... &CCODE=AFG [country_link] => http://devdata.worldbank.org/external/C ... &CCODE=AFG ) Array ( [0] => http://devdata.worldbank.org/external/C ... &CCODE=ALB [country_link] => http://devdata.worldbank.org/external/C ... &CCODE=ALB ) Array ( [0] => http://devdata.worldbank.org/external/C ... &CCODE=DZA [country_link] => http://devdata.worldbank.org/external/C ... &CCODE=DZA ) Array ( [0] => http://devdata.worldbank.org/external/C ... &CCODE=ASM [country_link] => http://devdata.worldbank.org/external/C ... &CCODE=ASM ) Array ( [0] => http://devdata.worldbank.org/external/C ... &CCODE=ADO [country_link] => http://devdata.worldbank.org/external/C ... &CCODE=ADO ) Array ( [0] => http://devdata.worldbank.org/external/C ... &CCODE=AGO [country_link] => http://devdata.worldbank.org/external/C ... &CCODE=AGO )

Code: Select all

class gdp extends databasing {
	var $link;
	var $regex_pattern;
	var $regex_array;
	//---------------------Set F(x)'s---------------//
	function set_link($blink){
		$this->link = $blink;
	}
	function set_regex_pattern($pattern){
		$this->regex_pattern = $pattern;
	}
	function set_regex_array($list){
		$this->regex_array = $list;
	}
	//-----------------------Get F(x)'s--------------//
	function get_regex_array(){
		return $this->regex_array;
	}
	function get_link(){
		return $this->link;
	}
	function get_regex_pattern(){
		return $this->regex_pattern;
	}
//----------------------------------------other stuff
	function regex_html($url,$pattern){
		$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL,$url);
			curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		//	curl_setopt($ch, CURLOPTFOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		$result = curl_exec($ch);
			curl_close($ch);
		//$pattern='!<option value="([^"]*)">([^<]*)</option>!';
		preg_match_all($pattern,$result,$ground);
		//$this->set_regex_array($ground);
		$this->set_regex_array($ground);
		print_r($this->regex_array);
		
}

	function mysql_select_array($database){
		$this->connect();
		$this->database($database);
		$sql_0= "SELECT country_link FROM country_html;";
		$result =mysql_query($sql_0);
		$i=1;
		$pattern = '#face="Verdana,Tahoma,Arial,Helvetica">([^<]*)#';
		$this->set_regex_pattern($pattern);
		while($row = mysql_fetch_array($result)){
			print "<html>";
			$this->set_link($row[0]);
   		    print "this is link number ".$i.": ".$this->get_link()."< br>";
			print "this is the pattern: ".$pattern."<br>";
   		    print "this is the result: ";
   		    $this->regex_html($this->get_link(),$this->regex_pattern);
   		    $i++;
   		    print"<br>";
}
$this->close();
}
}
So yes, the regex patter is the same as it was in the script to run this class as above, the links are virtually the same, but I"m not sure why i'm getting an empty array instead of a very large one when using the function "mysql_select_array". Appreciate any help :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_select_array() returns nothing and does nothing with the data set by regex_html(), which really should probably return the data instead of setting a property.
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

Post by afbase »

feyd wrote:mysql_select_array() returns nothing and does nothing with the data set by regex_html(), which really should probably return the data instead of setting a property.
I am still not following that advice. I took a good look at the code and tried to piece together what was stated. Could you or somebody elaborate.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Okay, each time regex_html() is called it overwrites a property of the class. regex_html() is called in a loop by mysql_select_array(). But mysql_select_array() doesn't do a single thing with the result from regex_html(), so the data is lost.
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

still troubling

Post by afbase »

ok i figured out the problem. If I supply the regex_html() a link like so:

Code: Select all

$link = "http://devdata.worldbank.org/external/CPProfile.asp?PTYPE=CP&CCODE=BEN";
$pattern = '#face="Verdana,Tahoma,Arial,Helvetica">([^<]*)#';
$mika = new gdp();
$mika->regex_html($link, $pattern);
Everything works out fine because the link given to my cURL ()'s in the regex_html() supplies the proper link, with all the data that the page is supposed to give.


Here is a good example what the curl'ed result should look like with the the link supplied in regex_html().


Now the trouble is if I give regex_html() a link from an array or object, the link does not work. I made a quick debug script that prints thelink supplied and spits out the curl'ed result. the curl'ed result prints out an "mdkid error". I

'm not sure what mdkid is but I hope someone could tell me. I don't see any MDKID anywhere in the post values of the link supplied, nor in the header information given in my cookies when opening the link in firefox.[/url]

Code: Select all

$mika = new gdp();

$grr = "gdp";
$mika->mysql_select_array($grr);
Post Reply