What is an easier way to parse this XML without so many if's

Discussion in 'Programming' started by danramosd, Jun 2, 2010.

  1. #1
    This tends to confuse me so Ill try and explain it as simple as possible. I have a list of questions that (currently in an XML file but maybe it should be in a DB?) and these questions get displayed to the user depending on conditions. For instance if a coach is a head coach the user will see a managerial question, but if hes an assistant he wont see one. Also if a coach is coaching at a school the user will see an academic question, but the user wont see one if the coach is a travel coach. I think you get the idea. My question is, is there a better way to determine which questions get displayed other than these complicated if statements? Or is there a way to make my code more concise and readable?
    Here is my PHP code:
    	$xml = simplexml_load_file('surveys.xml');
    	foreach($xml->question as $question)
    		{
    			if($question['name'] == 'personality' || $question['name'] == 'coachingskills')
    			{
    				echo "<tr><td colspan='11' class='question'>".$question->text."</td></tr>";
    				ratings($question['name']);
    				
    			}
    			
    			if($question['name']=='school'){
    				if(($coachtype!="private") && ($coachtype != "private") ){
    				echo "<tr><td colspan='11' class='question'>".$question->text."</td></tr>";
    				ratings($question['name']);
    				}
    			
    			}
    			
    			if($question['name']=='head')
    			{
    				if($position=='head')
    				{
    					echo "<tr><td colspan='11' class='question'>".$question->text."</td></tr>";
    					ratings($question['name']);
    					
    				}
    				
    				
    			}
    			
    			if($survey=='athlete')
    			{
    				if($question['name']=='myathletic'){	
    				echo "<tr><td colspan='11' class='question' >".$question->text."</td></tr>";
    				ratings($question['name']);
    				}
    				
    				if(($question['name']=='myacademic')){	
    				if( ($coachtype!='private') &&  ($coachtype!='travel')){
    					
    				echo "<tr><td colspan='11' class='question' >".$question->text."</td></tr>";
    				foreach($question->option as $option)
    				{
    					options($question['name'],$option);	
    				}
    				}
    				}
    				
    				if(($coachtype!='private') && ($coachtype!='travel')){
    				if($question['name']=='academicvsperformance'){	
    				echo "<tr><td colspan='11' class='question'>".$question->text."</td></tr>";
    					foreach($question->option as $option)
    				{
    					options($question['name'],$option);	
    				}
    				}
    				}
    				
    				
    				
    			}
    			
    			if($question['name'] == 'personalityislike')
    			{
    				echo "<tr><td colspan='11' class='question' >".$question->text."</td></tr>";
    				foreach($question->option as $option)
    				{
    					
    					options($question['name'],$option);	
    				}
    				
    			}
    			
    			
    			if(($question['name'] == 'attendcollege') && ($coachtype=='college'))
    			{
    				if($survey=='athlete'){
    				echo "<tr><td colspan='11' class='question'>".$question->text."</td></tr>";
    				foreach($question->option as $option)
    				{
    					options($question['name'],$option);	
    				}
    				}
    				
    			}
    			
    			
    			
    			
    			
    		}//foreach loops through all questions
    Code (markup):
    And here is my XML file (still not that good w/ XML):
    <?xml version="1.0"?>
    
    <questions>
    		<question name="personality">
    			<text>I am satisfied with the coachs personality:</text>
    		</question>
    		<question name="coachingskills">
    			<text>I am satisfied with the coachs coaching skills:</text>
    		</question>
    
    		<question name="school">
    			<text>I am satisfied with the coachs emphasis on academics:</text>
    		</question>
    			
    		<question name="head">
    			<text>I am satisfied with the coachs managerial skills:</text>
    		</question>
    		
    		<question name="myathletic">
    			<text>I am satisfied with my athletic performance:</text>
    			
    		</question>
    		
    		<question name="myacademic">
    			<text>I am satisfied with my academic performance:</text>
    			<option>True</option>
    			<option>False</option>
    		</question>
    			
    		<question name="personalityislike">
    			<text>My coachs personality is most like:</text>
    			<option>Director</option>
    			<option>Teacher</option>
    			<option>Supporter</option>
    			<option>Supervisor</option>
    		</question>
    		
    		<question name="academicvsperformance">
    			<text>I am more concerned with my academics, than with my athletic perfomance:</text>
    			<option>True</option>
    			<option>False</option>
    		</question>
    		
    		<question name="attendcollege">
    			<text>The surveyors purpose in attending college:</text>
    			<option>Enjoying the experiences</option>
    			<option>Building future relationships</option>
    			<option>Acquiring knowlege</option>
    			<option>Excelling in athletics</option>
    		</question>
    
    	
    
    </questions>
    
    Code (markup):

     
    danramosd, Jun 2, 2010 IP
  2. mc2fred

    mc2fred Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    To be honest, I'm more of a database person... and judging by your needs, I would choose that option too.
     
    mc2fred, Jun 2, 2010 IP
  3. danramosd

    danramosd Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    83
    #3
    How would you go about setting this up with in a database? What would be the best way to structure it?
     
    danramosd, Jun 2, 2010 IP
  4. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #4
    ccoonen, Jun 2, 2010 IP