处理大量重复 If 语句检查的 SMART 方法是什么?
What is a SMART WAY to deal with a lot of repeated If statements checks?
在我尝试为我在本科论文中创建的数据库创建搜索工具时,我需要对用户输入的值进行大量检查,并根据这些值,生成并执行适当的 MySQL query
。示例如下:
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>
并且作为相同 table 的另一部分,我有一个按钮允许用户创建第二行输入以进行更具体的搜索,具有 2 个属性,使用 AND 或使用 OR 语句。
<button type="button" id="new_row_btn" onclick="addRow()" name="new_row">Add an extra row</button>
我想保留这个 post 排序以便收到答案,所以我不会包括 addRow() JavaScript 函数,但是如果它是有必要请评论,我会尽快编辑。
我的问题如下,我有这两个输入,我想检查用户是否在第二行输入(在“高级”输入中)输入了任何内容,然后根据新值执行适当的查询。
PHP 代码现在看起来像这样:
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";
}
上面的 if 语句又进行了大约 20 次......每次检查一个属性与另一个属性的关系......例如
Name+newID
或 ID+newName
或 Email+newSex
显然我知道我还没有完成,这些属性有数百种可能的组合,但我想知道是否有某种方法可以避免这一切并使其更容易....
很抱歉这个问题太长了!欢迎任何关于如何使这个问题更好的注释。
提前感谢您的宝贵时间。
也许将相同的 IF 条件组合在一起?
以下是我要推荐的内容:
if ($option == 'Name') {
if ($newoption == 'Email' && !empty($newEmail)) {
//...
}
if ($newoption == 'Age' && !empty($newAge)) {
//...
}
}
重复少一点:
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)) {
//...
}
}
}
在我尝试为我在本科论文中创建的数据库创建搜索工具时,我需要对用户输入的值进行大量检查,并根据这些值,生成并执行适当的 MySQL query
。示例如下:
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>
并且作为相同 table 的另一部分,我有一个按钮允许用户创建第二行输入以进行更具体的搜索,具有 2 个属性,使用 AND 或使用 OR 语句。
<button type="button" id="new_row_btn" onclick="addRow()" name="new_row">Add an extra row</button>
我想保留这个 post 排序以便收到答案,所以我不会包括 addRow() JavaScript 函数,但是如果它是有必要请评论,我会尽快编辑。
我的问题如下,我有这两个输入,我想检查用户是否在第二行输入(在“高级”输入中)输入了任何内容,然后根据新值执行适当的查询。
PHP 代码现在看起来像这样:
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";
}
上面的 if 语句又进行了大约 20 次......每次检查一个属性与另一个属性的关系......例如
Name+newID
或 ID+newName
或 Email+newSex
显然我知道我还没有完成,这些属性有数百种可能的组合,但我想知道是否有某种方法可以避免这一切并使其更容易....
很抱歉这个问题太长了!欢迎任何关于如何使这个问题更好的注释。
提前感谢您的宝贵时间。
也许将相同的 IF 条件组合在一起?
以下是我要推荐的内容:
if ($option == 'Name') {
if ($newoption == 'Email' && !empty($newEmail)) {
//...
}
if ($newoption == 'Age' && !empty($newAge)) {
//...
}
}
重复少一点:
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)) {
//...
}
}
}