qlooney Posted August 8, 2009 Share Posted August 8, 2009 Hello everyone, i'm don't have a programming background, but am learning php-mysql, and i need help on some things. 1. if there was an empty database record, is it possible for me to skip it? for example, my database contain some records like "Real Name", "Nickname", "Age" etc. but not all the data have "Nickname". suppose it's empty, when i'm displaying the database, is it possible to skip the empty nickname like this: Real name : Jack Nickname : Rabbit Age : 22 ----------- Real name : Bob Nickname : Cat Age : 23 ----------- Real name : Emma Age : 22 ----------- as you can see, "Emma" doesn't have a nickname, so i wanted it to skip the "Nickname" row. i've tried doing something like this (and some other trial and error) : <?php echo "<div class='smallfont'><strong>Nickname : </strong><br />"; echo "$nickname"; echo "</div>"; if($nickname=="") echo ""; ?> 2. i made something like this (the staff table is separated from the movie table because i wanted to be able to add more different staff later on): <? //query the database $query="SELECT * from tbl_staff where id_movie='$id' order by id_staff"; $result = mysql_query($query) or die ('Failed to execute ' . $query . ' due to ' . mysql_error()); echo <h3>The Staffs</h3> while ($row = mysql_fetch_array($result)) { $id=$row['id_movie']; $idstaff=$row['id_staff']; $position=$row['position']; $names=$row['names']; echo "<p><div class=\"smallfont\"><strong>".$position."</strong></div>"; echo "<div class=\"smallfont\">".$names."</div></p>"; } the results is something like this: The Staffs Director($position) : abcde efgh($names) Script writer : ijkl mnop Producer : qrst uvwxyz suppose i haven't input anything to the staff table, is it possible to skip the entire loop including the <h3> title ("The Staff")? i tried using "if (idstaff==0)" but it didn't remove the title, only the loop. can anybody help? thanks. Link to comment https://forums.phpfreaks.com/topic/169340-how-do-i-skip-empty-database-record/ Share on other sites More sharing options...
wildteen88 Posted August 8, 2009 Share Posted August 8, 2009 as you can see, "Emma" doesn't have a nickname, so i wanted it to skip the "Nickname" row. i've tried doing something like this (and some other trial and error) : <?php echo "<div class='smallfont'><strong>Nickname : </strong><br />"; echo "$nickname"; echo "</div>"; if($nickname=="") echo ""; ?> You should do this if($nickname != '') { echo "<div class='smallfont'><strong>Nickname : </strong><br />"; echo "$nickname"; echo "</div>"; } 2. i made something like this (the staff table is separated from the movie table because i wanted to be able to add more different staff later on): <? //query the database $query="SELECT * from tbl_staff where id_movie='$id' order by id_staff"; $result = mysql_query($query) or die ('Failed to execute ' . $query . ' due to ' . mysql_error()); echo <h3>The Staffs</h3> while ($row = mysql_fetch_array($result)) { $id=$row['id_movie']; $idstaff=$row['id_staff']; $position=$row['position']; $names=$row['names']; echo "<p><div class=\"smallfont\"><strong>".$position."</strong></div>"; echo "<div class=\"smallfont\">".$names."</div></p>"; } suppose i haven't input anything to the staff table, is it possible to skip the entire loop including the <h3> title ("The Staff")? i tried using "if (idstaff==0)" but it didn't remove the title, only the loop. <? //query the database $query="SELECT * from tbl_staff where id_movie='$id' order by id_staff"; $result = mysql_query($query) or die ('Failed to execute ' . $query . ' due to ' . mysql_error()); if(mysql_num_rows($result ) > 0) { echo <h3>The Staffs</h3> while ($row = mysql_fetch_array($result)) { $id=$row['id_movie']; $idstaff=$row['id_staff']; $position=$row['position']; $names=$row['names']; echo "<p><div class=\"smallfont\"><strong>".$position."</strong></div>"; echo "<div class=\"smallfont\">".$names."</div></p>"; } } Link to comment https://forums.phpfreaks.com/topic/169340-how-do-i-skip-empty-database-record/#findComment-893562 Share on other sites More sharing options...
qlooney Posted August 8, 2009 Author Share Posted August 8, 2009 wow, thanks for the quick reply wildteen88! tried it and works great! Link to comment https://forums.phpfreaks.com/topic/169340-how-do-i-skip-empty-database-record/#findComment-893578 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.