0

how to write the code of SWITCH case to give x and y numerical values? x and y are in an array. Data in array came from the data base.

<?php
        $main_link = mysqli_connect('localhost', 'root', '','WMYC');
        $a1 = mysqli_fetch_array(mysqli_query($main_link, "SELECT * FROM teams WHERE team='t1' AND round='r1'"));
        $b1 = mysqli_fetch_array(mysqli_query($main_link, "SELECT * FROM teams WHERE team='t2' AND round='r1'"));
        $c1 = mysqli_fetch_array(mysqli_query($main_link, "SELECT * FROM teams WHERE team='t3' AND round='r1'"));
        $d1 = mysqli_fetch_array(mysqli_query($main_link, "SELECT * FROM teams WHERE team='t4' AND round='r1'"));
        $array = array($a1['xy'], $b1['xy'], $c1['xy'], $d1['xy']);
        print_r($array); // will get Array ( [0] => x [1] => y [2] => x [3] => x ) 
        print_r(array_count_values($array)); // will get Array ( [x] => 3 [y] => 1 )

    switch(isset($_POST['round1']))
    {
            case //array_count_values($array) == ( [x] => 3 [y] => 1 ):
          //value of x = 1 and value of y= -3
            break;
            case //array_count_values($array) == ( [x] => 2 [y] => 2 ):
          //value of x = 2 and value of y= -2
            break;
            case //array_count_values($array) == ( [x] => 1 [y] => 3 ):
          //value of x = 3 and value of y= -1
            break;
            case //array_count_values($array) == ( [x] => 4 ):
          //value of x = -1
            break;
            case //array_count_values($array) == ( [y] => 4 ):
          //value of y = 1
    }
        ?>

if there is a better and easier way other than switch case please advice

7
  • switch/case is used when you want to compare a single expression to different values. Use if/else if for more complex conditions. Commented Jan 2, 2020 at 18:09
  • Checkout this official documentation about control structure otherwise, you have to show us what is your problem exactly. i.e to be specific and show what do you have tried. Commented Jan 2, 2020 at 18:09
  • What does $_POST['round1'] have to do with the different cases? Commented Jan 2, 2020 at 18:11
  • if ($array['x'] == 3 && $array['y'] == 1) { ... } Commented Jan 2, 2020 at 18:15
  • @SaidbakR i want to post in a web page the value of x and y after i retrieve the cases above from the data base as value of x and y changes based on how many x and y are chosen from the 4 users Commented Jan 2, 2020 at 18:19

2 Answers 2

1

Since the counts will always add up to 4, you don't need to compare the whole array, just get the count for x and use that in the switch statement.

$counts = array_count_values($array);
if (isset($_POST['round1'])) {
    switch (@$counts['x']) {
    case 3:
        // do something for x=3 y=1
        break;
    case 2:
        // do something for x=2 y=2
        break;
    case 1:
        // do something for x=1 y=3
        break;
    default:
        // do something for x=0 y=4
}

The @ before $counts['x'] suppresses a warning when the count is 0 (since there won't be an x element in the array); we go into the default: case then.

Sign up to request clarification or add additional context in comments.

5 Comments

I think that using @ prefixing $counts in the switch to void any error when $counts['x'] has no index and then go to the default condition.
Imho $counts['x'] ?? 0 would look a bit better.
@Jeto Yeah, but it also requires a very recent PHP version, I'd prefer my answer to be more backward compatible.
Well PHP 7 was released 4 years ago, wouldn't call that "very recent" considering how fast programming languages grow :) Also, everything pre 7.2 has been EOL'd... I'd argue that taking this into account might be better in the long run for SO and encourage more people to upgrade; though it's definitely debatable since (way too) many companies are, sadly, still using PHP 5.
@Jeto Like my company. We have a mix from 5.4 to 7.3. :)
0

You are using the switch statement incorrectly.

Switch says, based on the the value in switch() execute some code based on its value (the case statements).

So what you are saying via: switch(isset($_POST['round1'])) is based on the value of $_POST['round1'] existing, do something. isset returns a true/false. If $_POST['round1'] exists it will return true, otherwhise it will return false.

You don't want to use a switch statement here, you want to use if / else.

if (array_count_values($array) == ( [x] => 3 [y] => 1 ) {
  //do something
}
elseif (array_count_values($array) == ( [x] => 2 [y] => 2 ) {
  //do something
}

Whether or not $_POST['round1'] isset does not appear to have any impact on your code, but if it is important you can do something like:

if (isset($_POST['round1'])) {
  if (array_count_values($array) == ( [x] => 3 [y] => 1 ) {
    //do something
  }
  elseif (array_count_values($array) == ( [x] => 2 [y] => 2 ) {
    //do something
  }
}
else {
  //handle $_POST['round1'] not being set
}

4 Comments

( [x] => 3 [y] => 1 ) is not valid syntax.
@Barmar, it sure isn't. I am assuming OP is using psuedo code and just targeting the switch issue.
@Barmar thank you so much i know this is not a valid syntax ( [x] => 3 [y] => 1 ) i am asking how to write it i just gave the logic thinking but dont know how to implement kind a i am new :)
@barmar last thing and thank you in advance how to set a variable for if ($array[0] == 'x') { echo $x; } elseif ($array[0] == 'y') { echo $y; } to not repeat them several times in the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.