我有一个已转换为MySQL的Access数据库。我有一个表,联系人,字段'EmailAddress','HideEmailAddress','FaxNumber','HideFaxNumber','PhoneNumber'和'HidePhoneNumber'。隐藏字段都是复选框,因此值为1或0,其中1为TRUE。
我需要在我的MySQL Select语句或我的PHP查询中,如果隐藏字段的值为1,则无法显示信息。此外,三个字段中只有一个是非常常见的标记为隐藏,所以我不能做毯子“如果其中任何一个都隐藏了所有这些。”我需要将查询输出到网页,并显示或隐藏上面的三个字段,具体取决于每列的隐藏字段的值。我的SELECT语句检索所有六个值,PHP将其转换为数组,但我不认为阵列比较会有所帮助。
我只是不知道我应该在这做什么。我一直在寻找几天,似乎没有什么能接近我所需要的。我有一个非常特别的if-elseif-else在我的PHP循环中运行,以便在截止日期前完成,但这需要9或12个不同的语句,我相信有更好的方法来做到这一点。 / p>
这是我的代码。我从原始查询中删除了很多,但该查询确实有效,所以如果这里有拼写错误,那就是来自切入。我根本无法弄清楚如何根据另一个字段的值来抑制字段。这也是我在SELECT语句中有两个AS语句的原因 - 在原始查询中,有来自五个不同表的信息,两个表中都有“FaxNumber”和“EmailAddress”。我离开了AS,以防它改变了一切。
<?php
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error connecting to MySQL server.');
$query = 'SELECT Contact.CompanyID, Contact.WebContact, Contact.FirstName, Contact.LastName, Contact.WorkPhone, Contact.HidePhoneNumber, Contact.FaxNumber AS ConFaxNumber, Contact.HideFaxNumber, Contact.EmailAddress AS ConEmailAddress, Contact.HideEmailAddress FROM Contact WHERE TDAT_Contact.WebContact = "1"';
mysqli_query($dbc, $query);
if($r = mysqli_query($dbc, $query)) {
while ($row = mysqli_fetch_array($r)) {
if(($row['HidePhoneNumber'] == 1) && ($row['HideFaxNumber'] == 1)) {
echo '<hr><div align="query"><span class="strong">Contact:</span> ' . $row['FirstName'] . ' ' . $row['LastName'] . '<br /><br /><span class="strong">Direct Email Address: </span>' . $row['ConEmailAddress'] . '</div>';
} elseif($row['HidePhoneNumber'] == 0) && ($row['HideFaxNumber'] == 0) {
echo '<hr><div align="query"><span class="strong">Contact:</span> ' . $row['FirstName'] . ' ' . $row['LastName'] . '<br /><span class="strong">Direct Telephone: </span>' . $row['WorkPhone'] . '<br /><span class="strong">Direct Fax: </span>' . $row['ConFaxNumber'] . '<br /><span class="strong">Direct Email Address: </span>' . $row['ConEmailAddress'] . '</div>';}
}
...
else { // Query didn't run.
echo '<p style="color: red;">Could not retrieve the data because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
}
}}
mysqli_close($dbc); // Close the connection.
?>
答案 0 :(得分:1)
你可以在mysql中做到并避免额外的PHP编码
假设隐藏字段是布尔值,这样的事情
$query = 'SELECT Contact.CompanyID, Contact.WebContact, Contact.FirstName,Contact.LastName,
case when Contact.HidePhoneNumber then '' else Contact.WorkPhone end as WorkPhone,
case when Contact.HideFaxNumber then '' else Contact.FaxNumber end AS ConFaxNumber,
case when Contact.HideEmailAddress then '' else Contact.EmailAddress and AS ConEmailAddress FROM Contact WHERE TDAT_Contact.WebContact = "1"';
然后在php上你只是
if($r = mysqli_query($dbc, $query)) {
while ($row = mysqli_fetch_array($r)) {
echo '<hr><div align="query"><span class="strong">Contact:</span> ' . $row['FirstName'] . ' ' . $row['LastName'] . '<br /><span class="strong">Direct Telephone: </span>' . $row['WorkPhone'] . '<br /><span class="strong">Direct Fax: </span>' . $row['ConFaxNumber'] . '<br /><span class="strong">Direct Email Address: </span>' . $row['ConEmailAddress'] . '</div>';
}
}
答案 1 :(得分:1)
<?php
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error connecting to MySQL server.');
$query = 'SELECT Contact.CompanyID, Contact.WebContact, Contact.FirstName, Contact.LastName, Contact.WorkPhone, Contact.HidePhoneNumber, Contact.FaxNumber AS ConFaxNumber, Contact.HideFaxNumber, Contact.EmailAddress AS ConEmailAddress, Contact.HideEmailAddress FROM Contact WHERE TDAT_Contact.WebContact = "1"';
mysqli_query($dbc, $query);
if($r = mysqli_query($dbc, $query)) {
while ($row = mysqli_fetch_array($r)) {
echo '<hr><div align="query"><span class="strong">Contact:</span> ' . $row['FirstName'] . ' ' . $row['LastName'] . '<br />';
if ($row['HidePhoneNumber'] == 0) {
echo '<span class="strong">Direct Telephone: </span>' . $row['WorkPhone'] . '<br />';
}
if ($row['HideEmailAddress'] == 0) {
echo '<span class="strong">Direct Email Address: </span>' . $row['ConEmailAddress'] . '<br />';
}
if ($row['HideFaxNumber'] == 0) {
echo '<span class="strong">Direct Fax: </span>' . $row['ConFaxNumber'];
}
echo '</div>';
}
} else { // Query didn't run.
echo '<p style="color: red;">Could not retrieve the data because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
}
mysqli_close($dbc); // Close the connection.
答案 2 :(得分:0)
将html输出分成小块,观察常见模式,并将其分解为函数:
function hide($field, $label, $row){
if ($row['Hide'.$field] == 0)
echo '<span class="strong">'.$label.'</span>'.$row[$field].'<br/>';
}
$hiddenfields = array ('EmailAddress' => 'Email Address', 'PhoneNumber' => 'Direct Telephone' /* etc. */);
foreach($hiddenfields as $field => $label)
hide($field,$label,$row);
这应该让你开始。
答案 3 :(得分:0)
您正在使用AND运算符(($ row ['HidePhoneNumber'] == 1)&amp;&amp;($ row ['HideFaxNumber'] == 1)这意味着两个条件必须为true才能执行条件代码。要解决您的问题,可以在while循环中使用“嵌套循环”并检查表达式,例如:
$conditions=array("0"=>"phonenumber","1"=>"fax","2"=>"email") //associative array
for($i=0;$i<count($conditions);$i++){
if($row[$conditions[$i]]==1){
echo $row.[$conditions[$i]];
}
else{
echo "user doesn't want to share this information";
}
}
如果condition返回1,它将回显关联的数组成员。