widget Posted November 29, 2011 Share Posted November 29, 2011 I have written a bra calculator, when the user enters their measurements their bra size is displayed however as the code I have used to do this came from a help tutorial I do not understand how it was constructed as I am quite a newb. The form works perfectly when returning data however if there is no size for a particular measurement it just displays nothing. I would like it to display N/A I have tried adding various if elses but without success. Any help is much appreciated <FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD=get> <input type="text" name="band" value="<?php echo $_GET['band']; ?>" /> <input type="text" name="bust" value="<?php echo $_GET['bust']; ?>" /> <INPUT TYPE=submit VALUE="Calculate"> </FORM> <?php $band = $_GET['band']; $bust = $_GET['bust']; $query = sprintf("SELECT size FROM bra_calculator WHERE band='%s' AND bust='%s'", mysql_real_escape_string($band), mysql_real_escape_string($bust)); $result = mysql_query($query); if (!$result) { echo "no"; } else { while ($row = mysql_fetch_assoc($result)) { echo $row['size']; } } mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252015-if-else-display/ Share on other sites More sharing options...
Spring Posted November 29, 2011 Share Posted November 29, 2011 <FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD=get> <input type="text" name="band" value="<?php echo $_GET['band']; ?>" /> <input type="text" name="bust" value="<?php echo $_GET['bust']; ?>" /> <INPUT TYPE=submit VALUE="Calculate"> </FORM> <?php if(isset( $_GET['band']) && isset($_GET['bust']) ){ $band = $_GET['band']; $bust = $_GET['bust']; $query = sprintf("SELECT size FROM bra_calculator WHERE band='%s' AND bust='%s'", mysql_real_escape_string($band), mysql_real_escape_string($bust)); $result = mysql_query($query); if (!$result) { echo "no"; } else { while ($row = mysql_fetch_assoc($result)) { echo $row['size']; } } mysql_free_result($result); } else{ echo "N/A"; } ?> http://us3.php.net/isset Quote Link to comment https://forums.phpfreaks.com/topic/252015-if-else-display/#findComment-1292079 Share on other sites More sharing options...
widget Posted November 29, 2011 Author Share Posted November 29, 2011 Sadly that just displays N/A before submitting the form but thank you for your help Quote Link to comment https://forums.phpfreaks.com/topic/252015-if-else-display/#findComment-1292080 Share on other sites More sharing options...
PFMaBiSmAd Posted November 29, 2011 Share Posted November 29, 2011 Your logic that is testing - if (!$result) { is only testing if the query produced an error (i.e. the query could not be executed at all due to a connection problem, a sql syntax error...) That does not mean that the query matched zero rows. To test how many rows the query matched, use mysql_num_rows Edit: here's the typical logic you should use for every SELECT/SHOW query (queries that return a result set, even if there are zero rows in the result set) - <?php $query = .... your query statement .... if(!$result = mysql_query($query)){ // query failed due to an error, handle that here... } else { // query executed w/o any errors, test if there are any rows in the result set if(!mysql_num_rows($result)){ // no rows in the result set, handle that case here... } else { // one or more matching rows in the result set, handle that case here... } } Quote Link to comment https://forums.phpfreaks.com/topic/252015-if-else-display/#findComment-1292082 Share on other sites More sharing options...
Spring Posted November 29, 2011 Share Posted November 29, 2011 My bad, I didn't read the question properly, sorry! Quote Link to comment https://forums.phpfreaks.com/topic/252015-if-else-display/#findComment-1292083 Share on other sites More sharing options...
widget Posted November 29, 2011 Author Share Posted November 29, 2011 Thank you so much num rows worked a treat Just in case anyone else has a similar problem here tis the code <FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD=get> <input type="text" name="band" value="<?php echo $_GET['band']; ?>" /> <input type="text" name="bust" value="<?php echo $_GET['bust']; ?>" /> <INPUT TYPE=submit VALUE="Calculate"> </FORM> <?php if(isset( $_GET['band']) && isset($_GET['bust']) ){ $band = $_GET['band']; $bust = $_GET['bust']; $query = sprintf("SELECT size FROM bra_calculator WHERE band='%s' AND bust='%s'", mysql_real_escape_string($band), mysql_real_escape_string($bust)); $result = mysql_query($query); $num_rows = mysql_num_rows($result); if (!$num_rows) { echo "N/A"; } else { while ($row = mysql_fetch_assoc($result)) { echo "$row[size]"; } } mysql_free_result($result); } Quote Link to comment https://forums.phpfreaks.com/topic/252015-if-else-display/#findComment-1292087 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.