Jump to content

If ELSE double condition using &&


jonnewbie12

Recommended Posts

Can someone please tell me what i have got wrong here?

 

I am trying to return value C if conditions a AND B are met or return value D.  Not sure if its parenthesis or something else?

 

<?php if (($row_Recordset4['multidirection']=="yes") &&

($_POST["widthcheck"] < $row_Recordset4['drop'])) echo $row_Recordset4['width'];else echo $row_Recordset4['drop'] ?>

 

Thanks

Link to comment
Share on other sites

<?php 
if (($row_Recordset4['multidirection']=="yes") && ($_POST["widthcheck"] < $row_Recordset4['drop'])) 
   echo $row_Recordset4['width'];
else 
   echo $row_Recordset4['drop']; // <== Was missing an ; here

?>

 

not sure what the problem is, do you get a different result than you wanted or?

Link to comment
Share on other sites

Don't bump threads. It's a violation of the rules and TOS.

 

You don't need parentheses around each comparison, and you should format your code in a readable manner, with proper indenting, and braces.

 

<?php
if( $row_Recordset4['multidirection'] == "yes" && $_POST["widthcheck"] < $row_Recordset4['drop'] ) {
     echo $row_Recordset4['width'];
} else {
     echo $row_Recordset4['drop'];
}
?>

 

Or if this is inline with some other html, it might be more readable if ternary syntax was used. For example:

<input value="
<?php echo $row_Recordset4['multidirection'] == "yes" && $_POST["widthcheck"] < $row_Recordset4['drop'] ? $row_Recordset4['width'] : $row_Recordset4['drop']; ?>"
type="text">

Link to comment
Share on other sites

1)  You're not returning, you're echoing

 

2)  A semicolon is not a colon

 

3)  You are not echoing the variables you're comparing so you can't be certain the conditions are or aren't being met.

 

4)  You're using $_POST and $recordset4 in the same line, maybe you're confused about which is which?

 

5)  You most likely have error_reporting turned off.  Turn it on in PHP.ini (and restart the server) to get actual error reports which may help you debug.

Link to comment
Share on other sites

Thanks and sorry for 'bumping'

 

I have echoed the values and they are correct.  The code is in line with some script as follows: I have copied Pickachus code into it and also tried the inline code but to no avail sadly.

 

 

<span id="sprytextfield3">

          <label for="drop"></label>

          <input name="drop" type="text" id="drop" value="<?php echo $_POST["drop1"]; ?>" size="5" maxlength="5" />

          <br />

          <span class="textfieldMinValueMsg">Min Drop = 1cm</span><span class="textfieldMaxValueMsg">Max Drop = <?php

if( $row_Recordset4['multidirection'] == "yes" && $_POST["widthcheck"] < $row_Recordset4['drop'] ) {

    echo $row_Recordset4['width'];

} else {

    echo $row_Recordset4['drop'];

}

?>cm</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span>

 

The script text at the bottom of the page is:

 

<script type="text/javascript">

var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");

 

 

var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "integer", {isRequired:false, minValue:40, maxValue:<?php echo $row_Recordset4['width']?>});

 

 

var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "integer", {isRequired:false, minValue:1, maxValue:<?php

if( $row_Recordset4['multidirection'] == "yes" && $_POST["widthcheck"] < $row_Recordset4['drop'] ) {

    echo $row_Recordset4['width'];

} else {

    echo $row_Recordset4['drop'];

}

?>});

  </script>

 

When the two first conditions are met, namely the "multidirection"=yes and the "widthcheck" is lower than the "drop" (and I have echoed these) it returns the correct value using the "width" field.  However when the "widthcheck: is higher and therefore BOTH conditions are not met it does not trigger the ELSE command and returns the same "width value".

 

Link to comment
Share on other sites

Thanks Dan

 

Guess the colon distinction is quite important in this arena.

 

Appreciate a comparison and echo are different but how else do I know what values are being compared ?

 

Am using Post and recordset in the same line as one variable comes from a from and another comes from the database.

 

The error reporting is within a spry validation script so not sure PHP.ini would make much difference?

Link to comment
Share on other sites

However when the "widthcheck: is higher and therefore BOTH conditions are not met it does not trigger the ELSE command and returns the same "width value".

 

This is impossible. There is some value you haven't checked, or you have a seriously warped version of PHP.

 

<?php 

echo $_POST["drop1"]; // This is redundant

// In order of appearance
$row_Recordset4['multidirection'] = 'yes';
$_POST['widthcheck'] = '15';
$row_Recordset4['drop'] = 10;
$row_Recordset4['width'] = 'foobar';
// End edits

if( $row_Recordset4['multidirection'] == "yes" && $_POST["widthcheck"] < $row_Recordset4['drop'] ) {
     echo $row_Recordset4['width'];
} else {
     echo $row_Recordset4['drop'];
}

echo $row_Recordset4['width'];

if( $row_Recordset4['multidirection'] == "yes" && $_POST["widthcheck"] < $row_Recordset4['drop'] ) {
     echo $row_Recordset4['width'];
} else {
     echo $row_Recordset4['drop'];
}

?>

 

Will output

Notice: Undefined index: drop1 in C:\Apps\wamp\www\examples\temp.php on line 3
10foobar10

which is expected.

 

If the $_POST['widthcheck'] is changed to 5, it returns foobarfoobarfoobar instead. As expected.

 

This is your code, without the markup, copied and pasted (with the commented declaration addition). This should output the same in just about any version of PHP released.

 

The issue isn't your code. You haven't defined some variable you want to check. Verify what you want to check is there before you check it :P

Link to comment
Share on other sites

Thank you. You were right if I posted widthcheck at a certain value it worked. 

 

<?php $_POST['widthcheck'] = '200';?>

 

Problem I have is that widthcheck is based on a form variable that does not post to the database. Ok at the risk of sounding like a total numpty here is the dliemna.

 

1. When the user enters a number in the form in the width field, I want it to post that value to widthcheck.  I can do that using normal html setting the text field but that doesn't seem to send the value to PHP.

2. After the user enters a value in the form in the drop field and presses submit the validation runs  which includes a comparison to the widthcheck field which shows a value in html (by virtue of the set text field html) but presumably has not been posted to PHP?

 

Obviously there is PHP and html wrapped up together here.  I know you will probably tell me to go and read a book but I am at the very end of the site and would really appreciate a pointer.

Link to comment
Share on other sites

Your form isn't set to method="post", your input isn't named 'widthcheck', or your form's action isn't pointed at the script.

 

I'm not your own personal error checker. This is the last of the help you'll get from me, I don't get the feeling you're trying.

Link to comment
Share on other sites

Well thank you for your help!

 

Trying? That's annoying.! Considering I am a full time criminal defence lawyer and designing a site with no training whatsoever, I would consider my efforts to be a little more than "not trying".  But I guess you have all the time in the world!

 

Shame really because everyone has been so accommodating including you until now.  Sets a bad example for the forum to be rude.  You could simply not respond if you felt you were being "used".

Link to comment
Share on other sites

This forum isn't here to help you finish your website faster. I volunteer my time here to help those that want to learn the language. You haven't taken the time to learn the language, and instead want to go directly into production.

 

Would you trust yourself building a house to shelter yourself if you've never built a house to shelter your tools? Think of us as outside consultants. We are not going to build your house for you, nor are we going to sit behind you while you work, pointing out every minor mistake you might make.

 

There are a lot of resources in here and out there if you want to learn the language. Heck, you could even Google working examples/tutorials to figure out what you're doing.

http://www.google.com/search?q=parse+a+form+with+php

http://www.google.com/search?q=compare+values+with+php

 

One person (moderator) even suggested turning on error reporting in PHP.ini (very important for development setups) to which you replied

The error reporting is within a spry validation script so not sure PHP.ini would make much difference?

 

You dismissed help without trying it. I owe this forum nothing, and it owes me nothing. I'm here to help people with PHP issues, and the people that run it seem to think I'm doing a good enough job to give me a title. I responded because I still want you to figure out what you're doing wrong.

Link to comment
Share on other sites

Thank You

 

I will have a look.  I appreciate not understanding why my form doesn't provide a value to a PHP script is probably child's play, but people come to me every day for advice from all walks of life and I try to accommodate as best I can.  When someone is clearly wasting my time because they are too lazy to help themselves I point it out but I don't judge before I know the person and the reason they are asking.

 

I will go and continue browsing the net to find answers as I have done for the most part.  Its' only after hours of frustration that I post a topic in the hope a simple answer will materialise. Even the most basic answers are part of the learning process.

 

Thank you anyway. The forum has actually been very useful.

Link to comment
Share on other sites

Another indicator of you not wanting to learn (and the reason why I have not and probably will not reply to your most recent question) is because you consistently use the wrong words.  You remind me of the people who insist that their screensaver is the thing behind their desktop icons.  It is absolutely imperative that you use the right words when you describe something.  Your most recent post, as worded, is completely meaningless.  Line by line...

 

 

Thank you. You were right if I posted widthcheck at a certain value it worked. 

Posted at a certain value?  I assume you mean that if you put a specific value into the form field and submitted it then...wait a minute

<?php $_POST['widthcheck'] = '200';?>
Nope, not at all what you said or implied.  This is called an assignment, not a post.

 

Problem I have is that widthcheck is based on a form variable that does not post to the database. Ok at the risk of sounding like a total numpty here is the dliemna.
Forms do not contain variables.  Variables do not post to the database.

 

1. When the user enters a number in the form in the width field, I want it to post that value to widthcheck.  I can do that using normal html setting the text field but that doesn't seem to send the value to PHP.
You post data to a web server, more specifically to a specific script.  You cannot post a value to a variable name.  "Setting the text field" has no meaning that I can discern, and at this point in your post I'm already assuming that you know so little about the terminology that I can never know what you actually mean.

2. After the user enters a value in the form in the drop field and presses submit the validation runs  which includes a comparison to the widthcheck field which shows a value in html (by virtue of the set text field html) but presumably has not been posted to PHP?
NONE of the code you've posted (none, zero) has been validation based.  It's all conditional display logic.  This run-on sentence contains a completely meaningless parenthetical.  Set text field HTML?  What?  No such thing exists.  You also assume something has not been posted to PHP, yet you could...include it in the code? 

 

Obviously there is PHP and html wrapped up together here.  I know you will probably tell me to go and read a book but I am at the very end of the site and would really appreciate a pointer.
All PHP is PHP/HTML wrapped together, in some way.  This site you're looking at is a mix of PHP and HTML, though a lot more tightly controlled than your site.  I doubt that you've actually made it to the end of this site.  The errors you're describing stem from fundamental misunderstandings and a lack of error control (which I'll go ahead and point out for what is now the third time: turn on error_reporting).

 

Debugging a PHP script is easy.  Echo the values you're comparing to see if they are what you expect.  The IF condition absolutely works properly, the problem is your variables.  They are not what you expect.  You have not yet followed any instructions about error reporting or printing your variables, so there is nothing more we can do for you.

 

As a lawyer (which doesn't impress us btw) you should be painfully aware of how difficult it is to talk to someone who doesn't know the proper terminology and who wants your professional help without bothering to actually do everything you said or use the proper words.  I'll leave you with a hypothetical conversation between you and someone asking you for pro bono advice:

 

Someone:  So then they told me I wasn't allowed to talk during the movies, don't they know this is America and I have freedom of speech!?

You:  Well that's not what freedom of speech protects you from, did you file a complaint with the management?

Someone:  I don't think that would help, since it was in the mall.  Seriously, I just want them arrested for fraud.

You:  You're not even listening to me.  Nothing you just said made sense.

Someone: YOU ARE A JERK I'M NOT A LAWYER I JUST WANT HELP

 

Link to comment
Share on other sites

Pointless Moderator with pointless 'advice'.  If you want to keep novices from 'bumping up' your posts maybe you should moderate as the title suggests and allow only those with sufficient knowledge to become members, otherwise FRO you patronising little nerd!

 

I said I was lawyer as it may have caused the rude idiot who was calling me lazy to think again. We don't generally fall into the 'cant be bothered' category.

 

As for your joke I am sure you will sleep well in the knowledge that you cause so much laughter to so few!

 

Can't believe you have taken the time to dissect my post line by line without giving a iota of actual advice.  Says it all really.  Get a life!

Link to comment
Share on other sites

Guest
This topic is now 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.