SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach question

    Okay, I've made the following function and it appears that the foreach loop argument is no good. Why?

    Code:
    function Checker($xml){
    	if(isset($vkey)){
    				
    		print "Vote History was not in query. <br/>";
    	}
    		
    	else{
    	
    		echo "Roll Call Votes (if they exist) -<br/>";
    		foreach($xml->voting_history->roll_call_vote as $roll_call_vote){
    				echo "<br/><br/>Vote Key: ", $roll_call_vote->meta_data->roll_call_vote_key, "<br/>";
    				echo "Description: ", $roll_call_vote->meta_data->description, "<br/>"; 
    				echo "Question: ", $roll_call_vote->meta_data->question, "<br/>";
    				echo "Date: ", $roll_call_vote->meta_data->date, "<br/>";
    				echo "Result: ", $roll_call_vote->meta_data->result, "<br/>";
    				echo "Total Yeas: ", $roll_call_vote->meta_data->total_yeas, "<br/>";
    				echo "Total Nays: ", $roll_call_vote->meta_data->total_nays, "<br/>";
    				echo "Total Present: ", $roll_call_vote->meta_data->total_present_votes, "<br/>";
    				echo "Total No Votes: ", $roll_call_vote->meta_data->total_no_vote, "<br/><br/>";
    				echo "Democrat Yeas: ", $roll_call_vote->meta_data->democratic_yeas, "<br/>";
    				echo "Democrat Nays: ", $roll_call_vote->meta_data->democratic_nays, "<br/>";
    				echo "Democrat Present Votes: ", $roll_call_vote->meta_data->democratic_present_votes, "<br/>";
    				echo "Democrat No Votes: ", $roll_call_vote->meta_data->democratic_no_vote, "<br/><br/>";
    				echo "Republican Yeas: ", $roll_call_vote->meta_data->republican_yeas, "<br/>";
    				echo "Republican Nays: ", $roll_call_vote->meta_data->republican_nays, "<br/>";
    				echo "Republican Present Votes: ", $roll_call_vote->meta_data->republican_present_votes, "<br/>";
    				echo "Republican No Votes: ", $roll_call_vote->meta_data->republican_no_vote, "<br/><br/>";
    				echo "Independent Yeas: ", $roll_call_vote->meta_data->independent_yeas, "<br/>";
    				echo "Independent Nays: ", $roll_call_vote->meta_data->independent_nays, "<br/>";
    				echo "Independent Present Votes: ", $roll_call_vote->meta_data->independent_present_votes, "<br/>";
    				echo "Independent No Votes: ", $roll_call_vote->meta_data->independent_no_vote, "<br/><br/>";
    			
    		
    		
    				if($ivkey == false){
    				print "working <br/>";
    				
    				}
    			
    		
    				else{
    					print "Individual Votes are not in query<br/>";
    				}
    		}
    			
    	}
    }
    This foreach loop does work outside of the function and I've checked to see that everything exists. What exactly is happening? Is it a variable scope issue?

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when coding or debugging, turn up error reporting.
    PHP Code:
    // top of scripts
    ini_set('display_errors'1);
    error_reporting(E_ALL); 

    var_dump() and print_r() are handy for inspecting the values of your variables.

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That doesn't tell me anything more than the error message I already have.

    After playing around some I discovered that the problem has nothing to do with the custom function, since it's still borked when I tested it outside the custom function. I still don't know why the argument is invalid. It will probably come to me when I'm doing something completely different.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if it doesnt tell you anything more, then you didnt try.

    var_dump() the invalid argument and see WHY its invalid?

    obviously, foreach() expects an array (objects allowed too in php5).


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
  •