Jump to content

if else display


widget

Recommended Posts

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/252015-if-else-display/
Share on other sites

<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

 

Link to comment
https://forums.phpfreaks.com/topic/252015-if-else-display/#findComment-1292079
Share on other sites

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...

}
}

Link to comment
https://forums.phpfreaks.com/topic/252015-if-else-display/#findComment-1292082
Share on other sites

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);
}

Link to comment
https://forums.phpfreaks.com/topic/252015-if-else-display/#findComment-1292087
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.