Jump to content

Conditional Execution of Codes


cosmic_sniper

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :thumb-up: )

 

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

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 "-"?

Link to comment
Share on other sites

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

Link to comment
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.