joshuaer Posted July 17, 2012 Share Posted July 17, 2012 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 More sharing options...
birbal Posted July 17, 2012 Share Posted July 17, 2012 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 More sharing options...
justsomeguy Posted July 17, 2012 Share Posted July 17, 2012 You can also select only the rows that you want to display. mysql_query("SELECT * FROM users where UID='$UID' AND Nname != ''") Link to comment Share on other sites More sharing options...
joshuaer Posted July 17, 2012 Author Share Posted July 17, 2012 Justsomeguy.... Can I add more fields I want to include by adding AND?UID=$UID AND Nname AND address AND Cname Link to comment Share on other sites More sharing options...
justsomeguy Posted July 18, 2012 Share Posted July 18, 2012 You can add other conditions, not fields, to the WHERE clause if you want additional filtering. http://www.w3schools.com/sql/sql_where.asp Link to comment Share on other sites More sharing options...
joshuaer Posted July 18, 2012 Author Share Posted July 18, 2012 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 More sharing options...
birbal Posted July 18, 2012 Share Posted July 18, 2012 (edited) <?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 July 18, 2012 by birbal Link to comment Share on other sites More sharing options...
joshuaer Posted July 18, 2012 Author Share Posted July 18, 2012 Thanks birbal it is now showing data but still showing the empty row Link to comment Share on other sites More sharing options...
joshuaer Posted July 18, 2012 Author Share Posted July 18, 2012 (edited) 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 July 18, 2012 by joshuaer Link to comment Share on other sites More sharing options...
justsomeguy Posted July 18, 2012 Share Posted July 18, 2012 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 More sharing options...
joshuaer Posted July 18, 2012 Author Share Posted July 18, 2012 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 More sharing options...
justsomeguy Posted July 18, 2012 Share Posted July 18, 2012 Like I showed before, you can add additional conditions to the WHERE clause to exclude the rows you don't want to get. Link to comment Share on other sites More sharing options...
joshuaer Posted July 18, 2012 Author Share Posted July 18, 2012 I could not figure out how to exclude a row using the WHERE function Link to comment Share on other sites More sharing options...
justsomeguy Posted July 18, 2012 Share Posted July 18, 2012 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. 1 Link to comment Share on other sites More sharing options...
joshuaer Posted July 18, 2012 Author Share Posted July 18, 2012 OOOOOOO.....light bulb! Thanks I finally understand Link to comment Share on other sites More sharing options...
callten061 Posted August 9, 2012 Share Posted August 9, 2012 same problem Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2012 Share Posted August 9, 2012 same answer Link to comment Share on other sites More sharing options...
boen_robot Posted August 9, 2012 Share Posted August 9, 2012 same problemMaybe you haven't written the query correctly? How does it look like? Link to comment Share on other sites More sharing options...
buitrongkhiem Posted August 11, 2012 Share Posted August 11, 2012 thank u so much, it really helpful for me Link to comment Share on other sites More sharing options...
Net123 Posted August 12, 2012 Share Posted August 12, 2012 (edited) 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 August 12, 2012 by Net123 Link to comment Share on other sites More sharing options...
justsomeguy Posted August 13, 2012 Share Posted August 13, 2012 Values from the mysql extension are always returned as strings, regardless of what they are in the database. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now