Jump to content

Hide row if a field is empty


joshuaer
 Share

Recommended Posts

Hello I am trying to figure out what the best way to hide a row when the Nname field is empty currently if the Nname field is empty the row still shows up in my query showing the ID number, but I would like to suppress this row all together Here is the code I currently have

<?phpini_set('display_errors', 1);error_reporting(E_ALL);    $host = '';    $username = '';    $password = '';    $db_name = '';    $tbl_name = '';    mysql_connect($host, $username, $password) or die('cannot connect');    mysql_select_db($db_name) or die('cannot select DB');       include("auth.php");       $UID = $_SESSION['UID'];$result = mysql_query("SELECT * FROM users where UID='$UID'")or die(mysql_error());//   $sql = 'SELECT * FROM `'.$tbl_name.'`';//   $result = mysql_query($sql);?><table width="400" border="0" cellspacing="1" cellpadding="0">    <tr>	    <td>		    <form name="form1" method="post" action="">		    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">		    <tr>			    <td bgcolor="#FFFFFF"> </td>		    </tr>		    <tr>			    <td align="center" bgcolor="#FFFFFF">#</td>			    <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>			    <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>			    <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>			    <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>		    </tr><?php while ($rows = mysql_fetch_array($result, MYSQL_ASSOC)): ?>		    <tr>			    <td align="center" bgcolor="#FFFFFF"><input name="need_delete[<? echo $rows['ID']; ?>]" type="checkbox" id="checkbox[<? echo $rows['ID']; ?>]" value="<? echo $rows['ID']; ?>"></td>			    <td bgcolor="#FFFFFF"><? echo $rows['ID']; ?></td>			    <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['Nname']); ?></td>			    <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['address']); ?></td>			    <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['iChore1']); ?></td>  		    </tr><?php endwhile; ?>		    <tr>			    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>		    </tr><?php				 			    if ( ! empty($_POST['delete'])) {				    foreach ($_POST['need_delete'] as $id => $value) {					    $sql = 'DELETE FROM `'.$tbl_name.'` WHERE `id`='.(int)$id;					    mysql_query($sql);				    }				    //header('Location: ichores.php'); exit();			    }			    mysql_close();			  ?>		    </table>		    </form>	    </td>    </tr></table>

Link to comment
Share on other sites

you can use empty() to check the field empty or not. if it is not empty print it. be cautious which values are considered empty. here is the link of manualhttp://php.net/empty

Link to comment
Share on other sites

Ok so I am trying the if empty command but it is not showing any results at all now, $emptyNname='Nname';

<?php if (empty($emptyNname)):?><?php while ($rows = mysql_fetch_array($result, MYSQL_ASSOC)):?>		    <tr>			    <td align="center" bgcolor="#FFFFFF"><input name="need_delete[<? echo $rows['ID']; ?>]" type="checkbox" id="checkbox[<? echo $rows['ID']; ?>]" value="<? echo $rows['ID']; ?>"></td>			    <td bgcolor="#FFFFFF"><? echo $rows['ID']; ?></td>			    <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['Nname']); ?></td>			    <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['address']); ?></td>			    <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['iChore1']); ?></td>		    </tr><?php endwhile; ?><?php endif; ?>

Link to comment
Share on other sites

<?php while ($rows = mysql_fetch_array($result, MYSQL_ASSOC)):?> 					<tr>							<td align="center" bgcolor="#FFFFFF"><input name="need_delete[<? echo $rows['ID']; ?>]" type="checkbox" id="checkbox[<? echo $rows['ID']; ?>]" value="<? echo $rows['ID']; ?>"></td>							<td bgcolor="#FFFFFF"><? echo $rows['ID']; ?></td>													   <?php if(empty($rows['Nname'])) ?>						   <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['Nname']); ?></td>							 						   <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['address']); ?></td>							<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['iChore1']); ?></td>					</tr><?php endwhile; ?> 

Edited by birbal
Link to comment
Share on other sites

I tried this alsocame up with the same results

<?php while ($rows = mysql_fetch_array($result, MYSQL_ASSOC)):?>										<tr>														<td align="center" bgcolor="#FFFFFF"><input name="need_delete[<? echo $rows['ID']; ?>]" type="checkbox" id="checkbox[<? echo $rows['ID']; ?>]" value="<? echo $rows['ID']; ?>"></td>														<td bgcolor="#FFFFFF"><? echo $rows['ID']; ?></td>													  												   <?php if('Nname'== "NULL") unset($rows); ?>												   <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['Nname']); ?></td>													  												   <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['address']); ?></td>														<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['iChore1']); ?></td>										</tr><?php endwhile; ?>

Edited by joshuaer
Link to comment
Share on other sites

If you don't want to do anything with those records at all then the correct solution would be to exclude them from the SQL query. There's no reason to use the resources to retrieve and return records that you're not going to do anything with.

Link to comment
Share on other sites

ok I changed my select to $result = mysql_query("SELECT Nname,address,iChore1 FROM users where UID='$UID'")or die(mysql_error());and it seems to work but I still get a blank row, but it does not allow me to delete that empty row now so it looks like that should work for what I need Thanks allot

Link to comment
Share on other sites

You're already telling it to only get rows where the UID matches a certain condition, you can add any other conditions to that list. The query I posted in post 3 adds another condition for the Nname field, to only get rows where that field is not empty. You can add any number of conditions you want to have it filter the rows based on whatever you're looking for.

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

but in mysql the $row['somename'] could return boolean value =false@ when it has no value i tried before but i am not sure

Edited by Net123
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...