[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2010-03-26
3
Medium Priority
?
228 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:blue-genie
3 Comments
 
LVL 12

Expert Comment

by:Avinash Zala
ID: 28672481
if (mysql_num_rows($result)){
                        while ($row = mysql_fetch_array($result)) {
                         
                          if($row['gameID'] =='')
                          {
                                       break;
                            }  
                           else
                           {
                                      // do what you want
                             }
}
0
 
LVL 6

Accepted Solution

by:
blueghozt earned 2000 total points
ID: 28675179
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
 
LVL 39

Author Closing Comment

by:blue-genie
ID: 31707471
sheer genius. exactly what I wanted.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question