Dynamic Conditional String - php

I have the code below taken partially inside a function:
$dynamic_comparison = '';
if(($from == '')&&($to == '')){
$dynamic_comparison = 1;
}else if(($from != '')&&($to != '')){
$dynamic_comparison = '($row >= $from) && ($row <= $to)';
}else if(($from != '')&&($to == '')){
$dynamic_comparison = '($row >= $from)';
}else if(($from == '')&&($to != '')){
$dynamic_comparison = '($row <= $to)';
}
$form, $to and $row are the parameters of the function.
I want to evaluate $dynamic_comparison into something like this:
if($dynamic_comparison){
//A bunch of code here...
}
I tried:
if(eval($dynamic_comparison)){
//A bunch of code here...
}
It throws an error. How to get this right?

eval() is bad idea imho.
You can use anonymous function, like this
$dynamic_comparison = function($row, $to, $from) { return false };
if(($from == '')&&($to == '')){
$dynamic_comparison = function($row, $to, $from) { return true;};
}else if(($from != '')&&($to != '')){
$dynamic_comparison = function($row, $to, $from) { return ($row >= $from) && ($row <= $to);};
}else if(($from != '')&&($to == '')){
$dynamic_comparison = function($row, $to, $from) { return ($row >= $from);};
}else if(($from == '')&&($to != '')){
$dynamic_comparison = function($row, $to, $from) { return ($row <= $to);};
}
And use it like
if($dynamic_comparison($row, $to, $from))...
If you want to use eval anyway:
$dynamic_comparison = 'return false;';
if(($from == '')&&($to == '')){
$dynamic_comparison = 'return true;';
}else if(($from != '')&&($to != '')){
$dynamic_comparison = 'return ($row >= $from) && ($row <= $to);';
}else if(($from != '')&&($to == '')){
$dynamic_comparison = 'return ($row >= $from);';
}else if(($from == '')&&($to != '')){
$dynamic_comparison = 'return ($row <= $to);';
}
if(eval($dynamic_comparison)){//$to,$from,$row must be available in this scope

i think you have to use double quotes insted of single quotes for your string like this :
<?php
$dynamic_comparison = '';
if(($from == '')&&($to == '')){
$dynamic_comparison = 1;
}else if(($from != '')&&($to != '')){
$dynamic_comparison = "($row >= $from) && ($row <= $to)";
}else if(($from != '')&&($to == '')){
$dynamic_comparison = "($row >= $from)";
}else if(($from == '')&&($to != '')){
$dynamic_comparison = "($row <= $to)";
}
if you don't have $row initialezed at this moment , escape $ character :
<?php
$dynamic_comparison = '';
if(($from == '')&&($to == '')){
$dynamic_comparison = 1;
}else if(($from != '')&&($to != '')){
$dynamic_comparison = "(\$row >= $from) && (\$row <= $to)";
}else if(($from != '')&&($to == '')){
$dynamic_comparison = "(\$row >= $from)";
}else if(($from == '')&&($to != '')){
$dynamic_comparison = "(\$row <= $to)";
}
see eval() documentation : http://php.net/manual/en/function.eval.php

Related

In PHP how do you do an if statement within the if of an if statement?

So the question is confusing I know. This is what I am wondering how to do.
I have the following if statement:
if(
(isset($_POST['billing_company']) && $_POST['billing_company'] != "") &&
(isset($_POST['billing_address']) && $_POST['billing_address'] != "") &&
(isset($_POST['billing_city']) && $_POST['billing_city'] != "") &&
(isset($_POST['billing_state']) && $_POST['billing_state'] != "") &&
(isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") &&
(isset($_POST['billing_phone']) && $_POST['billing_phone'] != "") &&
(isset($_POST['location_name']) && $_POST['location_name'] != "") &&
(isset($_POST['location_address']) && $_POST['location_address'] != "") &&
(isset($_POST['location_city']) && $_POST['location_city'] != "") &&
(isset($_POST['location_state']) && $_POST['location_state'] != "") &&
(isset($_POST['location_zip']) && $_POST['location_zip'] != "") &&
(isset($_POST['location_phone']) && $_POST['location_phone'] != "") &&
(isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") &&
(isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") &&
(isset($_POST['user_username']) && $_POST['user_username'] != "") &&
(isset($_POST['user_email']) && $_POST['user_email'] != "") &&
(isset($_POST['user_password']) && $_POST['user_password'] != "") &&
(isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") &&
(isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") &&
(isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") &&
(isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") &&
(isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") &&
(isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") &&
(isset($_POST['terms']) && $_POST['terms'] != "")
){
However, based on some variables, I may no longer need any of the billing information. So I am wondering if I can do an IF inside the IF, so something like this:
if(
($billingRequired == 1){
(isset($_POST['billing_company']) && $_POST['billing_company'] != "") &&
(isset($_POST['billing_address']) && $_POST['billing_address'] != "") &&
(isset($_POST['billing_city']) && $_POST['billing_city'] != "") &&
(isset($_POST['billing_state']) && $_POST['billing_state'] != "") &&
(isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") &&
(isset($_POST['billing_phone']) && $_POST['billing_phone'] != "") &&
}
(isset($_POST['location_name']) && $_POST['location_name'] != "") &&
(isset($_POST['location_address']) && $_POST['location_address'] != "") &&
(isset($_POST['location_city']) && $_POST['location_city'] != "") &&
(isset($_POST['location_state']) && $_POST['location_state'] != "") &&
(isset($_POST['location_zip']) && $_POST['location_zip'] != "") &&
(isset($_POST['location_phone']) && $_POST['location_phone'] != "") &&
(isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") &&
(isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") &&
(isset($_POST['user_username']) && $_POST['user_username'] != "") &&
(isset($_POST['user_email']) && $_POST['user_email'] != "") &&
(isset($_POST['user_password']) && $_POST['user_password'] != "") &&
(isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") &&
(isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") &&
(isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") &&
(isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") &&
(isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") &&
(isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") &&
(isset($_POST['terms']) && $_POST['terms'] != "")
){
I'm pretty sure there isn't but wanted to check with people smarter than me. I know I can do some nesting in side the {} but wanted to not have to check each variable inside a very deep nest.
Thanks,
Just add () around the section you want to combine and it will resolve to a simple boolean which can be included in your if statement
The logic here reads "if (billing is not required or billing fields are all filled out) and all the non-billing fields are filled out, then..."
if(
(($billingRequired != 1) || (
(isset($_POST['billing_company']) && $_POST['billing_company'] != "") &&
(isset($_POST['billing_address']) && $_POST['billing_address'] != "") &&
(isset($_POST['billing_city']) && $_POST['billing_city'] != "") &&
(isset($_POST['billing_state']) && $_POST['billing_state'] != "") &&
(isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") &&
(isset($_POST['billing_phone']) && $_POST['billing_phone'] != "")
))
&&
(
(isset($_POST['location_name']) && $_POST['location_name'] != "") &&
(isset($_POST['location_address']) && $_POST['location_address'] != "") &&
(isset($_POST['location_city']) && $_POST['location_city'] != "") &&
(isset($_POST['location_state']) && $_POST['location_state'] != "") &&
(isset($_POST['location_zip']) && $_POST['location_zip'] != "") &&
(isset($_POST['location_phone']) && $_POST['location_phone'] != "") &&
(isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") &&
(isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") &&
(isset($_POST['user_username']) && $_POST['user_username'] != "") &&
(isset($_POST['user_email']) && $_POST['user_email'] != "") &&
(isset($_POST['user_password']) && $_POST['user_password'] != "") &&
(isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") &&
(isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") &&
(isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") &&
(isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") &&
(isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") &&
(isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") &&
(isset($_POST['terms']) && $_POST['terms'] != "")
)
{
//statements
}
Although I suspect this could be done better within a loop, maybe it's personal preference, but I'd be happier with something like this
$billingRequiredFields = array('billing_company','billing_address','billing_city','billing_state','billing_zip','billing_phone','location_name','location_address','location_city','location_state','location_zip','location_phone','user_firstname','user_lastname','user_username','user_email','user_password','user_mobile','payment_cc_name','payment_cc_number','payment_cc_expo_month','payment_cc_expo_year','payment_cc_code','terms');
$billingNotRequiredFields = array('location_name','location_address','location_city','location_state','location_zip','location_phone','user_firstname','user_lastname','user_username','user_email','user_password','user_mobile','payment_cc_name','payment_cc_number','payment_cc_expo_month','payment_cc_expo_year','payment_cc_code','terms')
$requiredFields = ($billingRequired == 1) ? $billingRequiredFields : $billingNotRequiredFields;
$continue = true;
foreach($requiredFields as $field) {
if (!isset($_POST[$field]) || $_POST[$field] == '') {
$continue = false;
break;
}
}
if ($continue) {
// statements
}
What I would do is create an array at the top of the script that contains a list of all the POSTed variables that are required, then I would just loop through them. Your code would be way smaller. and almost as concise....
//We NEED these fields for the script to work...
$requiredFields = array(
"fname",
"lname",
"phone",
"accredited",
"etc, etc"
);
//IF you require billing,
if($billingRequired){
// Define the billing fields that we expect to be POSTed...
$billingFields = array(
"billing_compnay",
"billing_address",
"etc, etc"
);
// Add the billing fields to the required fields
$requiredFields = array_merge($requiredFields, $billingFileds);
}
// Loop through required fields and check to see if they are all POSTed
foreach($requiredFields as $fieldName){
// IF a required field is not set...
if(empty($_POST[$fieldName])){
// Do stuff, call a function, show an error, etc.
break; // Or redirect, or exit after a JSON response, whatever. Just be sure to end the loop here for efficiency.
}
}
better yet, don't put so many tests in the primary conditional, just make a function to test it, and then test for === true or === false
function validate_input($billingRequired=0){
$b_valid = true;
if( $billingRequired == 1 ){
if (!isset($_POST['billing_company']) || $_POST['billing_company'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['billing_address']) || $_POST['billing_address'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['billing_city']) || $_POST['billing_city'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['billing_state']) || $_POST['billing_state'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['billing_zip']) || $_POST['billing_zip'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['billing_phone']) || $_POST['billing_phone'] == ""){
$b_valid = false;
}
}
if (!isset($_POST['location_name']) || $_POST['location_name'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['location_address']) || $_POST['location_address'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['location_city']) || $_POST['location_city'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['location_state']) || $_POST['location_state'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['location_zip']) || $_POST['location_zip'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['location_phone']) || $_POST['location_phone'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_firstname']) || $_POST['user_firstname'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_lastname']) || $_POST['user_lastname'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_username']) || $_POST['user_username'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_email']) || $_POST['user_email'] != "") == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_password']) || $_POST['user_password'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_mobile']) || $_POST['user_mobile'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['payment_cc_name']) || $_POST['payment_cc_name'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['payment_cc_number']) || $_POST['payment_cc_number'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['payment_cc_expo_month']) || $_POST['payment_cc_expo_month'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['payment_cc_expo_year']) || $_POST['payment_cc_expo_year'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['payment_cc_code']) || $_POST['payment_cc_code'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['terms']) || $_POST['terms'] == "")
$b_valid = false;
}
return $b_valid;
}
now it's easy to modify / read etc. Because empty can be slightly ambiguous I find myself avoiding it as a rule, despite it's stylistic elegance.
To make this even cleaner, I'd probably write it like this:
function validate_input($billingRequired=0){
$b_valid = true;
$a_billing = array('billing_company','billing_address'...);
$a_main = array('billing_address','location_address'...);
if( $billingRequired == 1 ){
$a_main = array_merge($a_billing,$a_main);
}
foreach ($a_main as $test){
if (!isset($_POST[$test]) || trim($_POST[$test]) == "")
$b_valid = false;
break;
}
}
return $b_valid;
}
With the caveat that assumes empty values are "", which wouldn't generally be the case for select lists etc.

Need help to add second condition to existing code

I am trying to add a second condition to existing code but it doesn't seem to be working.
The conditions are:
Compare two strings, from different arrays (working)
And check the value of a third string from a different array (not
working)
Here is the working code without the second condition: http://pastebin.com/bfpNb9zw
Here is my attempt:
Basically, the bit I am trying to get working is this part && ($ca = '') && ($ca = '0') && ($ca = '1') but it seems $ca is not able to be read outside the loop
if(!function_exists('lookup')){
function lookup($chain, $type) {
$cacount = count($chain['tbsCertificate']['extensions']);
for($j = 0; $j < $cacount; $j++) {
$count = count($chain['tbsCertificate'][$type]['rdnSequence']);
$exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString', 'ia5String');
$oid = array('id-at-commonName');
for($i = 0; $i < $count; $i++) {
foreach($exists as $field) {
if(
array_key_exists($field, $chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['value']) &&
in_array($chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['type'], $oid)
) {
$value = $chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['value'][$field];
return $value;
$ca = '';
if(isset($chain['tbsCertificate']['extensions'][$j]['extnValue']['cA'])) {
$ca = $chain['tbsCertificate']['extensions'][$j]['extnValue']['cA'];
}
}
}
}
}
return null;
}
}
if (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '')) {
echo 'end entity';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '0')) {
echo 'secondary ca';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '1')) {
echo 'primary ca';
} else {
echo 'Root';
}
You are using =, which sets the value of $ca. You should be using === to check the value, instead.
Example:
if (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '')) {
echo 'end entity';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '0')) {
echo 'secondary ca';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '1')) {
echo 'primary ca';
} else {
echo 'Root';
}

PHP if condition fails inside a foreach loop

<?php
$key='APS';
$value='A|B|';
if ($key == 'APS'){
$aps = $key;
if (!empty($value)){
if(preg_match("/\|/",$value)){
$elephant = explode('|',$value);
foreach ($elephant as $elekey=>$elevalue){
if($elevalue = 'A'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'B'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'C'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'D'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'E'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
}
}
else{
echo $singlevalue = $value;
}
}
else {
echo $value='NIL';
}
}
?>
The above code may be lengthy but it's a very simple example, where if you execute you can see the if conditions will be failing to escape the loops.
Why in PHP if condition fails inside a foreach loop?
The Problem : You are using assignment operator inside if statements
Replace = with == or === (strict check)
if($elevalue = 'B'){
to
if($elevalue == 'B'){ [or] if($elevalue === 'B'){

How to go to 'else if' loop after 'if' condition is sucessfully entered

Is there any way to go to the else if body after the if condition is sucessfully entered?
Is there any way to go to the else if body after the if condition is sucessfully entered?
if ($axisX == $axisXOne) { /* Main IF Statement */
if($axisY =='0' && $axisYOne == '1') { $second = '2';}
else if($axisY =='0' && $axisYOne == '2') { $second = '1';}
else if($axisY =='1' && $axisYOne == '0') { $second = '2';}
else if($axisY =='1' && $axisYOne == '2') { $second = '0';}
else if($axisY =='2' && $axisYOne == '0') { $second = '1';}
else if($axisY =='2' && $axisYOne == '1') { $second = '0';}
if (($_POST['button'.$axisX.$second] == null) && ($curVal != $axisX.$second)){ /* Inner IF Statement */
echo $axisX.$second;
}
}
else if ($axisY == $axisYOne) { /* Main ELSE IF statement */
if($axisX =='0' && $axisXOne == '1') { $second = '2';}
else if($axisX =='0' && $axisXOne == '2') { $second = '1';}
else if($axisX =='1' && $axisXOne == '0') { $second = '2';}
else if($axisX =='1' && $axisXOne == '2') { $second = '0';}
else if($axisX =='2' && $axisXOne == '0') { $second = '1';}
else if($axisX =='2' && $axisXOne == '1') { $second = '0';}
if($_POST['button'.$second.$axisY] == null) {/* Inner IF Statement */
echo $second.$axisY;
}
}
else if ($xAxis == $yAxis && $xAxisOne == $yAxisOne) { /* other Main ELSE IF Statement*/
if($comVal[0] == '00' && $comVal[1] == '11') { $diagon = '22';}
else if($comVal[0] == '00' && $comVal[1] == '22') { $diagon = '11';}
else if($comVal[0] == '11' && $comVal[1] == '00') { $diagon = '22';}
else if($comVal[0] == '11' && $comVal[1] == '22') { $diagon = '00';}
else if($comVal[0] == '22' && $comVal[1] == '00') { $diagon = '11';}
else if($comVal[0] == '22' && $comVal[1] == '11') { $diagon = '00';}
if($_POST['button'.$diagon] == null) {
echo $diagon;
}
}
If Main IF Statement evaluates true and Inner IF Statement evalautes false then go to Main ELSE IF ladder. If Main IF Statement evaluates true and Inner IF Statement evalautes true then stop loop . If Main IF Statement evaluates false, directly go to Main ELSE IF Statement
If Main ELSE IF Statement evaluates true and Inner IF Statement evalautes false then go to Main ELSE IF ladder. If Main ElSE IF Statement evaluates true and Inner IF Statement evalautes true ,then stop loop .If Main ELSE IF Statement evaluates false, directly go to other Main ELSE IF Statements
Try this:
$success = true;
if($a == $Xaxis && $b == $yaxis) {
$zAxis = $Xaxis + $yaxis;
$success = $Xaxis != $zAxis;
}
if( ($b == $Xaxis && $a == $yaxis) || !$success) {
// do stuff here
}
Or something along those lines.

PHP $_POST issue

I've been wrestling with this problem for an hour or so and it seems basic but I just can't seem to get to the bottom of it. What I'm trying to do here is create a block 5 times with contents from within my mysql database.
That works fine, but my real problem is when I try to echo out error messages. I've got an IF right after FOR that checks if $_POST['champ5v5name[1-5]'] is at its default value. If it is, it should echo out an error message saying "Choose your champions!", yet it doesn't (It just goes through to the next page as if it were a success). I've tried various different methods but none of them have worked, can anybody give me a hand?
for($i=1;$i<=5;$i++) {
$result = mysql_query("SELECT name,health,damage,armour,aspeed FROM champions");
$htmltext .= '<label>Champion '.$i.'</label><br/>';
$htmltext .= '<select name="champ5v5name'.$i.'">';
$htmltext .= '<option value="select'.$i.'">Select champion:</option>';
}
while($rowschamp = mysql_fetch_array($result,MYSQL_NUM)) {
$htmltext .= '<option value="'.$rowschamp[0].'">'.$rowschamp[0].' (HP: '.$rowschamp[1].' DMG: '.$rowschamp[2].' ARMOUR: '.$rowschamp[3].' ASPEED: '.$rowschamp[4].')</option>';
}
$htmltext .= '</select><br/><br/>';
if($_POST['champ5v5name1'] != 'Select champion:' || $_POST['champ5v5name2'] != 'Select champion:' || $_POST['champ5v5name3'] != 'Select champion:' || $_POST['champ5v5name4'] != 'Select champion:' || $_POST['champ5v5name5'] != 'Select champion:') {
if($_POST['champ5v5name1'] == $_POST['champ5v5name2'] || $_POST['champ5v5name1'] == $_POST['champ5v5name3'] || $_POST['champ5v5name1'] == $_POST['champ5v5name4'] || $_POST['champ5v5name1'] == $_POST['champ5v5name5']) $error = 'A champion is repeated.';
if($_POST['champ5v5name2'] == $_POST['champ5v5name3'] || $_POST['champ5v5name2'] == $_POST['champ5v5name4'] || $_POST['champ5v5name2'] == $_POST['champ5v5name5'] || $_POST['champ5v5name2'] == $_POST['champ5v5name1']) $error = 'A champion is repeated.';
if($_POST['champ5v5name3'] == $_POST['champ5v5name2'] || $_POST['champ5v5name3'] == $_POST['champ5v5name4'] || $_POST['champ5v5name3'] == $_POST['champ5v5name5'] || $_POST['champ5v5name3'] == $_POST['champ5v5name1']) $error = 'A champion is repeated.';
if($_POST['champ5v5name4'] == $_POST['champ5v5name2'] || $_POST['champ5v5name4'] == $_POST['champ5v5name3'] || $_POST['champ5v5name4'] == $_POST['champ5v5name5'] || $_POST['champ5v5name4'] == $_POST['champ5v5name1']) $error = 'A champion is repeated.';
if($_POST['champ5v5name5'] == $_POST['champ5v5name2'] || $_POST['champ5v5name5'] == $_POST['champ5v5name3'] || $_POST['champ5v5name5'] == $_POST['champ5v5name4'] || $_POST['champ5v5name5'] == $_POST['champ5v5name1']) $error = 'A champion is repeated.';
}
else {
$error = 'Choose your champions!';
}
Well all you're doing is assigning a string to a variable, and you're setting an error regardless if your condition proves true or false. I see no logic to perform any specific task IF there is an error.
That being said, you really need to simplify things:
try {
$champions = array();
$error = null;
for ($i = 1; $i < 6; $i++){
$champions[] = $_POST['champ5v5name' . $i];
}
$dups = array_count_values($champions);
rsort($dups);
// Check for missed assignments, assuming no champions have the word 'select' in their name
if(in_array('select',$champions)){
$error = 'Choose your champions!';
}
// Check for duplicate champs
if($dups[0] != 1){
$error = 'Champion Repeated!';
}
if($error){
throw new Exception($error);
}
}
catch (Exception $e) {
echo $e->getMessage();
exit(); // Probably don't exit, just show the form again.
}
// Do other stuff, everything is ok if the code gets here...
Are you checking to see if all the champions are at the default, or if any are at the default?
Your condition is:
$_POST['champ5v5name1'] != 'Select champion:' ||
$_POST['champ5v5name2'] != 'Select champion:' ||
$_POST['champ5v5name3'] != 'Select champion:' ||
$_POST['champ5v5name4'] != 'Select champion:' ||
$_POST['champ5v5name5'] != 'Select champion:'
If you're going for any, it should be
$_POST['champ5v5name1'] != 'Select champion:' &&
$_POST['champ5v5name2'] != 'Select champion:' &&
$_POST['champ5v5name3'] != 'Select champion:' &&
$_POST['champ5v5name4'] != 'Select champion:' &&
$_POST['champ5v5name5'] != 'Select champion:'
EDIT:
The issue is that the form submits a value of select$i, not Select champion:
EDIT 2:
You should use the form array feature of PHP:
for($i = 1; $i <= 5; $i++) {
$result = mysql_query("SELECT name,health,damage,armour,aspeed FROM champions");
$htmltext .= '<label>Champion '.$i.'</label><br/>';
$htmltext .= '<select name="champ5v5name[]">';
#Field names ending in `[]` turn into arrays!
$htmltext .= '<option value="">Select champion:</option>';
while($rowschamp = mysql_fetch_array($result, MYSQL_NUM)) {
$htmltext .= '<option value="'.$rowschamp[0].'">'.$rowschamp[0].' (HP: '.$rowschamp[1].' DMG: '.$rowschamp[2].' ARMOUR: '.$rowschamp[3].' ASPEED: '.$rowschamp[4].')</option>';
}
$htmltext .= '</select><br /><br />';
}
$champions = $_POST['champ5v5name'];
$error = "No champions chosen!";
for($champions as $champion) {
if($champion) {
$error = "";
break;
}
}
if(!$error && count($array) != count(array_unique($champions))) {
$error = "A champion is repeated"
}

Categories