in while loop - check if field is null then break?

i have 11 fields of optional data for each row entry.
when i pull out the data I want it to echo xml node only if there is something there.

  $result = mysql_query("SELECT * FROM gamedetails LEFT JOIN gamePrizes ON gameID = gamePrizeID where prizeID is NOT NULL");

what a result as follows (assuming that 2 items are returned, item 1 has something in field 1 and field 2, and item 2 only has something in field 1)

<dataxml>
<row gameID="65">
  <gameName>banana</gameName>
  <prize id="1">another prize 1</prize>
  <prize id="2">another prize 2</prize>
</row>
<row gameID="666">
  <gameName>carrot</gameName>
  <prize id="1">another prize 1</prize>
</row>
</dataxml>

someone suggested //if(isset($_GET['rank5'])){, but that didn't work.

code snippet below might have some syntax errors, stripped out some testing stuff, but it's just to show what i've done.
so if there's only 2 prizes I don't want the empty nodes.

if (mysql_num_rows($result)){
                  	while ($row = mysql_fetch_array($result)) {
						echo '<row gameID="'.$row['gameID'].'">';
						echo "<gameName>".$row['gameName']."</gameName>";
							echo "<prize id='1'>".$row['rank1']."</prize>";
							echo "<prize id='2'>".$row['rank2']."</prize>";
							echo "<prize id='3'>".$row['rank3']."</prize>";
							echo "<prize id='4'>".$row['rank4']."</prize>";
							echo "<prize id='5'>".$row['rank5']."</prize>";
							echo "<prize id='6'>".$row['rank6']."</prize>";
							echo "<prize id='7'>".$row['rank7']."</prize>";
							echo "<prize id='8'>".$row['rank8']."</prize>";
							echo "<prize id='9'>".$row['rank9']."</prize>";
							echo "<prize id='10'>".$row['rank10']."</prize>";
							echo "<prize id='11'>".$row['rank11']."</prize>";
						
						echo "</row>";
                   }
                } else {
                   echo '<error>no results</error>';  
                }
                echo '</dataxml>';
				mysql_free_result($result);

Open in new window

LVL 39
blue-genieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Avinash ZalaWeb ExpertCommented:
if (mysql_num_rows($result)){
                        while ($row = mysql_fetch_array($result)) {
                         
                          if($row['gameID'] =='')
                          {
                                       break;
                            }  
                           else
                           {
                                      // do what you want
                             }
}
0
blueghoztCommented:
not sure of your database settings so the following IF test should cover you, if not I am sure you can see the principle of this approach :)
if (mysql_num_rows($result)){

	//set to the maximum number of prizes
	$max_number_of_prizes = 11;
   
   while ($row = mysql_fetch_array($result)) {
		
		echo '<row gameID="'.$row['gameID'].'">';
		
		echo "<gameName>".$row['gameName']."</gameName>";
		
		for($i = 1; $i < $max_number_of_prizes + 1; $i++){
		
			if($row['rank'.$i] && $row['rank'.$i] != '' && !is_null($row['rank'.$i])) echo "<prize id='".$i."'>".$row['rank'.$i]."</prize>"; else break;
		
	    }
						
		echo "</row>";
    
	}

} else {
        
	echo '<error>no results</error>';  
    
}
    
echo '</dataxml>';
	
mysql_free_result($result);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
blue-genieAuthor Commented:
sheer genius. exactly what I wanted.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.