Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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