cosmic_sniper Posted April 24, 2012 Share Posted April 24, 2012 I would like to have a script that would check certain parameters and if none of them returns TRUE, execute a set of codes. So the idea is somehow like if...else(). <?php // trying to put if() directly within if() if ( // checking of variables if (!isset($transaction)) { echo '<font color="#FF0000">Type of transaction is not set.</font>'. '<br />'; } else { if ($transaction == 'sell') { if (empty($BV)) { echo '<font color="#FF0000">Book value is not set.</font>'. '<br />'; } } } if (empty($stock)) { echo '<font color="#FF0000">Stock symbol is not set.</font>'. '<br />'; } if (empty($price)) { echo '<font color="#FF0000">Price per share is not set.</font>'. '<br />'; } if (empty($volume)) { echo '<font color="#FF0000">Volume of shares is not set.</font>'. '<br />'; } ) { echo 'Go back and fill out necessary fields.'; } // if none of the above parameters returns TRUE else { echo 'Copy and paste the following in the email that you would be submitting.' </tr> <tr> <td> switch ($transaction) { case 'buy': include('calc_buy.php'); break; case 'sell': include('calc_sell.php'); break; default: echo '<font color="#FF0000"><b>If you see this message there is something wrong with your session. Please send us a feedback with the details of your session before seeing this message.</b></font>'; } } ?> This code returns an error and as I understand the error, it is because of if (if ()). How can I make it work? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/ Share on other sites More sharing options...
Muddy_Funster Posted April 24, 2012 Share Posted April 24, 2012 take out the first if. That's not how you nest if's and it's completly redundant. Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/#findComment-1340066 Share on other sites More sharing options...
cpd Posted April 24, 2012 Share Posted April 24, 2012 The way your thinking is actually (and I rarely say this) wrong. Read up on how to use if statements. We could spend hours trying to explain different ways of using them so you may as well just read up on them yourself. Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/#findComment-1340071 Share on other sites More sharing options...
cosmic_sniper Posted April 24, 2012 Author Share Posted April 24, 2012 I do understand that the way I used if() in that code is wrong. If I got it right, the way it should be nested is something like this: if (condition_a) { if (condition_b) { echo 'condition_a and condition_b are TRUE'; } else (condition_c) { echo 'condition_a and condition_c are TRUE'; } } elseif (condition_d) { echo 'condition_a is FALSE and condition_d is TRUE'; } elseif (condition_e) { echo 'condition_a and condition_d are FALSE and condition_e is TRUE'; } else { echo 'condition_a, condition_d, and condition_e is FALSE'; } ?> I'll explain it from the start and how I came up with that code. What I wanted is to check for certain conditions, then if any of those returned TRUE, echo something then end the program. If none of the conditions are true, continue with the next section. With the structure of if...elseif...else(), I can only check for one condition that is TRUE. For nested if(), like the case of condition_b & c, I can only check for them if a is correct. So, to check all the conditions, I used a series of if(). (checking successful ) Here's the problem. With such code (series of if()), even the conditions returned TRUE, the succeeding section would still execute. That's how I came up with if( if() { //code } if() { //code } if() { //code } } { else { //code } Now I know that it would not work. Any suggestion for alternative ways to do it? Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/#findComment-1340170 Share on other sites More sharing options...
xyph Posted April 24, 2012 Share Posted April 24, 2012 You'd have to provide us with a more real-world example I think. I don't really get what you're trying to do. You can use the && (AND) and || (OR) operators to combine multiple conditions into one statement. <?php $conA = TRUE; $conB = FALSE; $conC = TRUE; $conD = FALSE; $conE = FALSE; if( $conA && $conB ) { echo 'conA and conB true'; } elseif( $conA && $conC ) { echo 'conA and conC true'; } elseif( $conA ) { echo 'conA true, conB and conC false'; } elseif( $conD ) { echo 'conD true, conA false'; } elseif( $conE ) { echo 'conE true, conA and conD false'; } ?> Since I don't know exactly what you're trying to do, besides compare a bunch of values, I can't help further. Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/#findComment-1340173 Share on other sites More sharing options...
cosmic_sniper Posted April 24, 2012 Author Share Posted April 24, 2012 I finally got it to work by introducing a new variable. Thanks as well for the comments since it somehow helped me arrive at the solution. Here's the code: <?php // checking of variables if (!isset($transaction)) { echo '<font color="#FF0000">Type of transaction is not set.</font>'. '<br />'; $checkpoint = "true"; } else { if ($transaction == 'sell') { if (empty($BV)) { echo '<font color="#FF0000">Book value is not set.</font>'. '<br />'; $checkpoint = "true"; } } } if (empty($stock)) { echo '<font color="#FF0000">Stock symbol is not set.</font>'. '<br />'; $checkpoint = "true"; } if (empty($price)) { echo '<font color="#FF0000">Price per share is not set.</font>'. '<br />'; $checkpoint = "true"; } if (empty($volume)) { echo '<font color="#FF0000">Volume of shares is not set.</font>'. '<br />'; $checkpoint = "true"; } if ($checkpoint == "true") { echo '<br /><br /> Go back and fill out necessary fields.'; } else { echo 'Copy and paste the following in the email that you would be submitting.'. '<br /><br />'; switch ($transaction) { case 'buy': include('calc_buy.php'); break; case 'sell': include('calc_sell.php'); break; default: echo '<font color="#FF0000"><b>If you see this message there is something wrong with your session. Please send us a feedback with the details of your session before seeing this message.</b></font>'; } } ?> This is already working. Though, there is still another problem regarding checking of values. I'll post it in my next reply. Edit: Included <?php ?> on code. Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/#findComment-1340179 Share on other sites More sharing options...
cosmic_sniper Posted April 24, 2012 Author Share Posted April 24, 2012 I am using that script to check for values entered on a form. Here's the code of the form: <html> <body> <form action="calc.php" method="post"> Date of transaction: <select name="month"> <option>-Month- <option>01 <option>02 <option>03 <option>04 <option>05 <option>06 <option>07 <option>08 <option>09 <option>10 <option>11 <option>12 </select> <select name="day"> <option>-Day- <option>01 <option>02 <option>03 <option>04 <option>05 <option>06 <option>07 <option>08 <option>09 <option>10 <option>11 <option>12 <option>13 <option>14 <option>15 <option>16 <option>17 <option>18 <option>19 <option>20 <option>21 <option>22 <option>23 <option>24 <option>25 <option>26 <option>27 <option>28 <option>29 <option>30 <option>31 </select> Time:<select name="hr"> <option>-hr- <option>01 <option>02 <option>03 <option>04 <option>05 <option>06 <option>07 <option>08 <option>09 <option>10 <option>11 <option>12 </select> <select name="min"> <option>-min- <option>00 <option>01 <option>02 <option>03 <option>04 <option>05 <option>06 <option>07 <option>08 <option>09 <option>10 <option>11 <option>12 <option>13 <option>14 <option>15 <option>16 <option>17 <option>18 <option>19 <option>20 <option>21 <option>22 <option>23 <option>24 <option>25 <option>26 <option>27 <option>28 <option>29 <option>30 <option>31 <option>32 <option>33 <option>34 <option>35 <option>36 <option>37 <option>38 <option>39 <option>40 <option>41 <option>42 <option>43 <option>44 <option>45 <option>46 <option>47 <option>48 <option>49 <option>50 <option>51 <option>52 <option>53 <option>54 <option>55 <option>56 <option>57 <option>58 <option>59 </select> <br> Type of transaction: Buy<input type="radio" name="transaction" value="buy">Sell<input type="radio" name="transaction" value="sell"><br> Stock symbol:<input type="text" name="stock">(e.g. AB, SCC, ABS)<br> Book value of stock:<input type="text" name="BV">(<i><b>*For selling transaction only</b></i>)<br> Price per share:<input type="text" name="price"><br> Volume of shares:<input type="text" name="volume"><br> <input type="submit"> </form> Here are the things I want to check: Are necessary fields filled out? Did the user enter the proper data type? I'm almost done with the first item. The only thing left is to check for the date and time section. I guess (please correct me if I'm wrong), the following code would check if the data passed is a number and not the default "-day-" or "-hr-" <?php if ($month >= 0 && $day >= 0 && $hr >= 0 && $min >= 0) { $checkpoint = 'false'; } else { echo '<font color="#FF0000">Date and time is not properly set.</font>'. '<br />'; $checkpoint = 'true'; } ?> For the second bullet for checking, what code can check if the data entered is a float for field that requires it or long for field that requires it? Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/#findComment-1340188 Share on other sites More sharing options...
xyph Posted April 24, 2012 Share Posted April 24, 2012 You can force a value type by typecasting, but it isn't really a check, per-se. You could use RegEx to check for a float. An expression like /^-?(?:\d*\.?\d+)|(?:\d+\.?\d*)$/ would work. A positive integer can be checked using ctype_digit. A negative can be checked the same way, check if the first character is a '-', and the rest match ctype_digit() Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/#findComment-1340193 Share on other sites More sharing options...
cosmic_sniper Posted April 24, 2012 Author Share Posted April 24, 2012 You can force a value type by typecasting, but it isn't really a check, per-se. If I got it right, type casting would force the system to understand that a certain string / variable is of a certain data type specified. As you've said it's not checking, per-se. Though it would be very helpful if I can make the form accept specific data types. If that would be the case, then there won't be a need for checking in the first place. lol You could use RegEx to check for a float. An expression like /^-?(?:\d*\.?\d+)|(?:\d+\.?\d*)$/ would work. ??? (completely puzzled) I'm sorry xyph but I really didn't get you there. I'm new to programming and self-studying, applying concepts as I learn them. Unfortunately, RegEx is about 8 lessons from where I am now. If it's not too much of a bother, could you help me construct that expression into if()? A positive integer can be checked using ctype_digit(). A negative can be checked the same way, check if the first character is a '-', and the rest match ctype_digit() Finally, something I can work out with my current skills. Thanks for bringing this up but what is the function that I can use to check if the first character is "-"? Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/#findComment-1340232 Share on other sites More sharing options...
Muddy_Funster Posted April 25, 2012 Share Posted April 25, 2012 You can use javascript to create masks for form fields. PHP manual page for preg_match should get you going with the use of RegEx. Something else that you may want a look at since your focusing on if conditionals at the moment is the switch section of the manual. To cheack a charactor you can use the preg_match or use a combination of substr and chr Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/#findComment-1340344 Share on other sites More sharing options...
cpd Posted April 25, 2012 Share Posted April 25, 2012 To validate your floating point variable you can use filter_var. It does however require PHP >= 5.2.x. Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/#findComment-1340537 Share on other sites More sharing options...
cosmic_sniper Posted May 1, 2012 Author Share Posted May 1, 2012 thanks CPD for raising about filter_var I found it in another tutorial and I'm now studying about it. Quote Link to comment https://forums.phpfreaks.com/topic/261516-conditional-execution-of-codes/#findComment-1341846 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.