Home > Software engineering >  What is a SMART WAY to deal with a lot of repeated If statements checks?
What is a SMART WAY to deal with a lot of repeated If statements checks?

Time:05-16

In my attempt to create a searching tool for a database that I created in the context of my undergrad thesis, I am required to make a lot of checks on the values that the user has entered, and according to those values, generate and execute the appropriate MySQL query. An example follows:

HTML Code

(part of the whole table)
<tr id="tablerow" >
              <th id="selectth">
                <select  name="Attributes" id="Attributes">
                  <!-- List of all the available attributes for the user to select for the searching queries -->
                  <option disabled>Options</option>
                  <option value="Name" id="p_Name">Name</option>
                  <option value="ID" id="p_Id">Patient ID</option>
                  <option value="Sex" id="p_Sex">Sex</option>
                  <option value="Email" id="p_Email">Patient Email</option>
                  <option value="Age" id="p_Age">Age ></option>
                  <option value="Agesmaller">Age < </option>
                  <option value="Race" id="p_Race">Race</option>
                  <option value="PhoneNumber" id="p_Phonenum">Phone Number</option>
                  <option value="Comorbidities" id="p_Comorbidities">Comorbidities</option>
                  <option value="EDSS" id="p_eddsscore">EDSS Score</option>
                  <option value="Pregnant" id="p_Pregnant">Is Pregnant</option>
                  <option value="Onsetlocalisation" id="p_Onsetlocalisation">Onset Localisation</option>
                  <option value="Smoker" id="p_Smoker">Is a Smoker</option>
                  <option value="onsetsymptoms" id="p_onsetsymptoms">Onset Symptoms</option>
                  <option value="MRIenhancing" id="p_MRIenhancing">MRI Enhancing Lesions</option>
                  <option value="MRInum" id="p_MRInum">MRI Lesion No.</option>
                  <option value="MRIonsetlocalisation" id="p_MRIonsetlocalisation">MRI Onset Localisation</option>
                </select>
              </th>

and as another part of that same table, I have a button that allows the user to create a second row of input in order to make a more specific search, with 2 attributes, either with AND or with an OR statement. <button type="button" id="new_row_btn" onclick="addRow()" name="new_row">Add an extra row</button>

I would like to keep this post sort in order to receive an answer, so I won't include the addRow() JavaScript function, but if it is necessary please comment it and I will edit this ASAP.

My question is the following, I have those 2 inputs, and I want to check if the user has entered anything on the second row of inputs (in the "advanced" one) and then based on the new value execute the appropriate query.

The PHP code as of now looks something like this:

PHP Code

if($option == 'Name' && $newoption == 'Email' && !empty($newEmail)){
                $sql = "SELECT patients.Patient_id,patients.Patient_name,patients.DOB,patients.Phonenum,patients.Email,MSR.Sex FROM patients,MSR WHERE patients.Patient_id = MSR.NDSnum AND Doctor_ID = $usersid AND patients.Email = '$newEmail' $and_or patients.Patient_name LIKE '%$entry%' ORDER BY Patient_id";
                $result = $pdo->query($sql);
                if ($result->rowCount() > 0) {
                  while ($row = $result->fetch()) { ?>
                    <table id="standard">
                      <tr>
                        <th>Patient ID</th>
                        <th>Name</th>
                        <th>Date of Birth</th>
                        <th>Phone Number</th>
                        <th>Email</th>
                        <th>Sex</th>
                        <th>Previous Visits</th>
                      </tr>
                      <tr>
                        <td><?php echo $row['Patient_id']; ?></td>
                        <td> <?php echo $row['Patient_name']; ?> </td>
                        <td><?php echo $row['DOB']; ?></td>
                        <td><?php echo $row['Phonenum']; ?></td>
                        <td><?php echo $row['Email']; ?></td>
                        <td><?php echo $row['Sex']; ?></td>
                        <td><?php echo "<a href='/application/previousvisit-bootstrap.php?id=" . $row['Patient_id'] . "'>Previous Visits</a>"; ?></td>
                      </tr>
                    </table>
                    <div ></div>
                  <?php }
                 } else {
                  echo "No patient exists with this information. Name Email";
                }
              } if ($option == 'Name' && $newoption == 'Age' && !empty($newAge)){
                $sql = "SELECT * FROM patients WHERE timestampdiff(year,dob,curdate()) > '$entry' $and_or patients.Patient_name LIKE '%$entry%' AND Doctor_ID = $usersid ORDER BY Patient_id";
                $result = $pdo->query($sql);
                if ($result->rowCount() > 0) {
                  while ($row = $result->fetch()) { ?>
                    <table id="standard">
                      <tr>
                        <th>Patient Id</th>
                        <th>Patient Name</th>
                        <th>Date of Birth</th>
                        <th>Phone Number</th>
                        <th>Email</th>
                        <th>Previous Visits</th>
                      </tr>
                      <tr>
                        <td><?php echo $row['Patient_id']; ?></td>
                        <td> <?php echo $row['Patient_name']; ?> </td>
                        <td><?php echo $row['DOB'] ?></td>
                        <td><?php echo $row['Phonenum']; ?></td>
                        <td><?php echo $row['Email']; ?></td>
                        <td><?php echo "<a href='/application/previousvisit-bootstrap.php?id=" . $row['Patient_id'] . "'>Previous Visits</a>"; ?></td>
                      </tr>
                    </table>
                    <div ></div>
                  <?php }
                } else {
                  echo "No patient exists with this information. Name Age";
                }

The above if statement goes on for about 20 more times... each time checks one attribute in relation to another.. for example Name newID or ID newName or Email newSex Obviously I know that Im not done and that there are hundreds of possible combinations with those attributes, but I was wondering if there is some way to avoid all this and make it kinda easier....

Im sorry for the long question! Any notes on how to make this a better question are welcome.

Thank you in advanced for you time.

CodePudding user response:

Maybe group the same IF condition together?

Here is something that I will recommend:

if ($option == 'Name') {
    if ($newoption == 'Email' && !empty($newEmail)) {
        //...
    }
    if ($newoption == 'Age' && !empty($newAge)) {
        //...
    }
}

A little less repetition:

if ($option == 'Name') {
    // use this 'if', if $newoption correlates to which child if is populated...
    if ($newoption == 'Email' || $newoption == 'Age') {
        if (!empty($newEmail)) {
            //...
        }
        if (!empty($newAge)) {
            //...
        }
    }
}
  • Related