Looking for an efficient way to handle a large number of repeated If statement checks
P粉421119778
P粉421119778 2024-04-03 19:20:08
0
1
448

When I was trying to create a search tool for a database I created for my undergraduate thesis, I needed to do a lot of checking of user-entered values ​​and generate and execute appropriate MySQL queries based on those values. Examples are as follows:

HTML code

(part of the whole table)
<tr id="tablerow" class="">
              <th id="selectth">
                <select class="selection" 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>

As another part of the same table, I have a button that allows the user to create a second line of input for a more specific search, with 2 attributes, using an AND or OR statement. <button type="button" id="new_row_btn" onclick="addRow()" name="new_row">Add additional rows</button>

I'd like to keep this post sorted to receive answers, so I won't include the addRow() JavaScript function, but if necessary please comment it and I will edit it as soon as possible.

My problem is as follows, I have these two inputs and I want to check if the user entered anything on the second line (in the "Advanced" input) and then perform the appropriate action query based on the new value.

The current PHP code looks 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 class="line"></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 class="line"></div>
                  <?php }
                } else {
                  echo "No patient exists with this information. Name+Age";
                }

The above if statement will last about 20 times...each time it will check the relationship of one attribute to another attribute..for example Name newID or ID newName or Email newSex Obviously I know I'm not done yet and there are hundreds of possible combinations of these properties, but I'm wondering if there's some way to avoid all this and make it easier...

Sorry for asking such a long question! Any comments on how to make this question better are welcome.

Thank you in advance for your time.

P粉421119778
P粉421119778

reply all(1)
P粉239089443

Perhaps it is possible to combine the same IF conditions together?

Here’s what I recommend:

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

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)) {
            //...
        }
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!