SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating A Function

    For some strange reason, I am having the worst time converting this select of code into a function. This code parses some XML code from a site. What it should do is return a multi-dimensional array so that I can work with it.

    When I use the code AS-IS, it works, but when I attempt to put it into a function, it doesn't return an array. However when I run it as straight code, it returns the array.

    Code:
    function findWhat($search) {
    
    	$mt = rawurlencode($search);
    			
    	$aff_id = 47183;
    		
    	$ip_addr = getenv("REMOTE_ADDR");
    			
    	$url = "http://findwhat.com/bin/findwhat.dll?getresults&dc=25&mt=$mt&base=0&aff_id=$aff_id&ip_addr=$ip_addr";
    		
    	if (!($fp = fopen($url,"r"))) die("Couldn't open XML file!");
    			
    	$item_counter = 0;
    			
    	$in_item_tag = 0;
    			
    	$fm_current_tag_state = '';
    			
    	$fm_headline_data = array();
    		
    	if (!($xml_parser = xml_parser_create())) die("Couldn't create XML parser!");
    		
    	xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");
    		
    	xml_set_character_data_handler( $xml_parser , "characterDataHandler" );
    		
    	while ($data = fread($fp, 4096)) {
    		
    		if (!xml_parse($xml_parser, $data, feof($fp))) {
    		
    			break; // get out of while loop if we're done with the file
    		
    		}
    		
    	}
    			
    	xml_parser_free($xml_parser);
    		
    	for ($key = 0; $key < $item_counter; $key++) {
    		
    		$findwhat[] = array("title" => $fm_headline_data[$key][title],
    							"url" => $fm_headline_data[$key][url],
    							"description" => $fm_headline_data[$key][description],
    							"bidprice" => $fm_headline_data[$key][bidprice],
    							"clickurl" => $fm_headline_data[$key][clickurl]);
    				
    	}
    
    	return $findwhat;
    	
    }
    You can see the code working here:
    http://www.sohh.com/search_/parse.php?search=PHP
    <///////~

    www.sohh.com

  2. #2
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think your problem is here;

    Code:
    	for ($key = 0; $key < $item_counter; $key++) {
    		
    		$findwhat[] = array("title" =>... etc.
    Change it to;

    Code:
    		$findwhat = array("title" =>... etc.
    and it should work. Not sure what the impact of that multi dimensional array will be though.

  3. #3
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didn't work....

    Last edited by ibeblunt; Feb 15, 2002 at 12:18.
    <///////~

    www.sohh.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •