While loop?

Hi guys,

I am trying to do a loop that looks through certain files in a directory, determines if it is the correct file for the selected page you’re on, and if so, displays the right content.

The xml files in the directory all have filenames of xt.somenumberstring-standings.xml (like xt.12449221-standings.xml and xt.12449220-standings.xml).

The problem is that the loop I’m working with doesn’t seem to be going back to the top if the correct file is not found.

I’ve told it to look in the file, and if the information is not correct, subtract 1 from the number string, and try again. But, to no avail.

Any thoughts? I’ve tried a number of different options, and am hoping I’m just missing something simple here.

// determine standings file to use
        $fileToUse = '';
        $highestId = 0;
        
        if ($handle = opendir($XMLPath)) 
		{			
            while ($thisFilename = readdir($handle)) 
			{
                $fileParts = explode('.', $thisFilename);
                
				if (end($fileParts) != 'xml')
				{
					continue;
				}
				
                $fileParts = explode('-', $fileParts[1]);
                
				if (end($fileParts) == 'standings') // filetype validation 
				{                   
					
					$xmla = new SimpleXMLElement(file_get_contents($XMLPath . $thisFilename));
 				    
 				    foreach ($xmla->{'sports-metadata'}->{'sports-content-codes'}->{'sports-content-code'} as $thisCodea) {
                		if ($thisCodea['code-type'] == 'conference') {
                    		$theconferenceName = $thisCodea['code-key'];
                    	break;
                		}
            		
						if ($theConferenceName != strtolower($selectedTeam['conference-key']))
						{
						
							$highestId = intval($fileParts[0]); $prevId = $highestId - 1; 
							$thisFilename = 'xt.'.$prevId. '-standings.xml';
						}
							elseif ($theConferenceName == strtolower($selectedTeam['conference-key'])) {
							
							if (intval($fileParts[0]) > $highestId) // time validation
							{ 
								$fileToUse = $thisFilename;
								$highestId = intval($fileParts[0]);
								break; //terminate foreach content code tag loop
							}
							
						}
										
					
					}		
					
				}	
				
			}	
			
		}
        if ($fileToUse != '') 
		{
            $fullStandingsXML = file_get_contents($XMLPath . $fileToUse);
        }          

Try again? Really? Where? How? At the start of the next looping of the while loop, readdir returns the name of the next file in the directory, and overrides the value you gave to $thisFilename.

Ah, guido, good find.

What do you think - create a second while loop after the readdir?

Is it not sufficient to run the while loop until it finds the file that matches your criteria, or exhausts all of the files? Like guido said, assigning to $thisFilename inside the loop doesn’t make sense and isn’t necessary to process all of the files.

The problem is that this code is on a template page where $thisFilename is different for different versions of the template page.

So, different pages require different files. If it doesn’t find the right file, then it needs to keep looking. Does that make sense?

I’m afraid I don’t really understand the intended logic of your script. You’re assigning a different value to $thisFilename at the beginning of each iteration of the while loop, then if certain conditions are met you assign a value to it within the loop, which accomplishes nothing because you don’t break the loop, so it starts over and assigns to $thisFilename again.

Your while loop should run into the correct file is found, based on your criteria. If the loop is not iterating the way you expect it to, I suggest you take everything out of it, insert an echo or var_dump() and see if it iterates over all of the expected files.

Here’s where the issue lies – the loop needs to start by defining $thisFilename, but if certain conditions are not correct (if $theConferenceName =!strtolower($selectedTeam[‘conference-key’]), I need to redefine $thisFilename and go through the exploding and SimpleXMLelement again.

So it sounds like it would be a second while loop, inside the first while loop.

Does that make sense?

You want to process every file in a directory until you find one that matches certain criteria, right?

the loop needs to start by defining $thisFilename, but if certain conditions are not correct…I need to redefine $thisFilename and go through the exploding and SimpleXMLelement again.

Do you want to start by defining $thisFilename as one of the files in the directory $XMLPath? If so, then the while loop does what you described and there’s no need to assign a value to $thisFilename inside the loop.

The while loop is setup to iterate over every file in your $XMLPath directory. Each iteration of the loop assigns a new value to $thisFilename and then performs the exploding and SimpleXMLElement instantiation.

Inside the loop you just need to test for the criteria you’re looking for and then break the loop when you find a file that matches it. If the criteria doesn’t match, and there are more files remaining in the directory, the loop will start a new iteration, assign a new value to $thisFilename, perform the exploding…

$selectedTeam is defined somewhere outside of the code you posted?

This is where the issue is. If the correct file is not found, then it doesn’t keep looking for the right file. It just displays nothing.

That’s why I have the

if $theConferenceName =!strtolower($selectedTeam['conference-key'])

to try and give it some direction. When I take that out (and adjust if’s and else’s) nothing displays if it’s not the right file.

Now we’re speaking the same language.

What displays nothing? There’s nothing in the code you posted that would generate output.

What do you believe that is accomplishing?

If what isn’t the right file? Nothing should be displayed for the files that don’t match the criteria, right? The loop should just iterate until it finds the file that matches, then display output from that, right?

It’s never finding the right file. The loop isn’t going back and looking again, or there’s something preventing it from finding the right file. So nothing ever displays.

What do you believe that is accomplishing?

Each team has a defined conference-key. If the conference key of the team that is selected is not equal to the $theConferenceName in the XML file is being read from the directory ($xmlpath), then …

If what isn’t the right file? Nothing should be displayed for the files that don’t match the criteria, right? The loop should just iterate until it finds the file that matches, then display output from that, right?

Yes. Either the loop is not iterating or something is preventing the right file from being found.

Well, the first thing is that you need to do a little debugging and see if your loop is iterating over all of the files you expect it to or not. If not, you need to figure out why.

Then what? What is …? My question is, what do you believe this code will accomplish:



                        if ($theConferenceName != strtolower($selectedTeam['conference-key'])) 
                        { 
                         
                            $highestId = intval($fileParts[0]); $prevId = $highestId - 1;  
                            $thisFilename = 'xt.'.$prevId. '-standings.xml'; 
                        }

I don’t see anywhere that the value you assign to $thisFilename would be read. If your loop is working properly, then after that code executes the loop will start a new iteration and overwrite the value that you assigned to $thisFilename.

Ok, I suggest you take everything out of the while loop expect for some echo or var_dump() calls to debug what is happening. Make sure your loop is working then start adding your other code back in. I don’t see a reason why you would want to assign to $thisFilename inside the loop.

Just a quick update that I was able to figure this out. The first “break” was ending the loop before it was doing enough digging to find the right file.

Thanks for helping me walk through it, Beaumont! :slight_smile: