KrMa
KrMa

Reputation: 713

How to use a switch case statement in a while loop

I have a function where you can press the button shuffle, and then you get three random rows from MySQL database, with the columnnames id, weekday, description. At the moment I just get random rows without any conditions. Therefore I made a select form, where I can choose to only get printed records, when weekday is equal to wednesday.

I tried to make a switch case statement, but I am really not sure how I have to set that in my while loop?

Shuffle

index.php

<select name="weekday" class="form-control selectpicker" >
        <option value=" ">Select Weekday</option>
        <option value="alldays">All Days</option>
        <option value="wednesday">Wednesday</option>
        <option value="saturday">Saturday</option>
</select>
<div>
    <form method="post">
        <button class="btn btn-warning">Shuffle</button>
    </form>
</div>
<div>
    <h4>Shuffle Result</h4><hr>
    <?php include 'shuffle.php' ?>
</div>

shuffle.php

<?php
    $sql = "SELECT id, description FROM articles ORDER BY RAND ( ) LIMIT 3";
    $res = $mysqli->query($sql);

    //print($res);
    if ($res->num_rows > 0) {
    // output data of each row
        while($row = $res->fetch_assoc()) {
            echo "Number: " . $row["id"]. "<br>" .
                 "Description: " . $row["description"]. "<br><br>".
                 '<div class="btn btn-warning btn-open-modal" data-id="'.$row["id"].'">See Details '.'</div><br><br>';              
        }
    } else {
    echo "0 results";
    }

I tried to make a switch case code for my while loop:

switch($_POST['weekday']){
          case 'alldays':
              echo "Alldays";
          break;
          case 'wednesday':
              echo "Wednesday";
          break;
          case 'saturday':
              echo "Saturday";
          break;
          default:
              echo "Something went wrong";
    }

Upvotes: 1

Views: 1112

Answers (2)

John Carroll
John Carroll

Reputation: 48

If I am understanding your desired outcome properly, the switch would not go in the while loop. You would use it to alter the query. Try something like this:

$sql = "SELECT id, description FROM articles";
switch($_POST['weekday']){
      case 'alldays':
          //you can remove this case because it does change the query
      break;
      case 'wednesday':
          $sql .= " WHERE day LIKE 'wednesday'";
      break;
      case 'saturday':
          $sql .= " WHERE day LIKE 'saturday'";
      break;
}
$sql .= " ORDER BY RAND ( ) LIMIT 3";
$res = $mysqli->query($sql);

Please realize that I do not know your database structure so the column names I used in the WHERE statements may have to be changed.

In response to your comment: The reason why you are getting that undefined index is because your select element is outside of your form. Change it to this:

<form method='post'>
    <select name="weekday" class="form-control selectpicker" >
        <option value=" ">Select Weekday</option>
        <option value="alldays">All Days</option>
        <option value="wednesday">Wednesday</option>
        <option value="saturday">Saturday</option>
    </select>
    <div>
        <button class="btn btn-warning">Shuffle</button>
    </div>
</form>
<div>
    <h4>Shuffle Result</h4><hr>
    <?php include 'shuffle.php' ?>
</div>

For future reference, a quick way to test a page like this is to add:

print_r($_POST);

This will show you what post values actually made it to the page. If you would have done that you would have seen that your POST variable did not have the information you were looking for

Upvotes: 1

Seekcha
Seekcha

Reputation: 17

hello but I think its not possible to have the post echo in index.php

Upvotes: 0

Related Questions