0

我有一个已转换为 MySQL 的 Access 数据库。我有一个表格,联系人,其中包含“EmailAddress”、“HideEmailAddress”、“FaxNumber”、“HideFaxNumber”、“PhoneNumber”和“HidePhoneNumber”字段。隐藏字段都是复选框,因此值为 1 或 0,其中 1 为 TRUE。

如果隐藏字段的值为 1,我需要在我的 MySQL Select 语句或 PHP 查询中不显示信息。此外,通常只有三个字段中的一个会被标记为隐藏,所以我不能做毯子“如果其中任何一个是 1 隐藏所有这些。” 我需要将查询输出到网页,并根据每列隐藏字段的值显示或隐藏上面的三个字段。我的 SELECT 语句正在检索所有六个值,而 PHP 正在将其转换为数组,但我认为数组比较在这里没有任何帮助。

我只是不知道我应该在这里做什么。我一直在寻找几天,似乎没有什么能接近我需要的。我有一个非常特别的 if-elseif-else 在我的 PHP 循环中运行,以便在截止日期前完成它,但这需要 9 或 12 条不同的语句,我确信有更好的方法来做到这一点。

这是我的代码。我已经从原始查询中删减了很多,但是该查询确实有效,所以如果这里有拼写错误,那就是删减了。我只是不知道如何根据另一个字段的值来抑制一个字段。这也是为什么我在 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.

?>
4

4 回答 4

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>';
    }
}
于 2013-03-27T18:54:47.070 回答
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.
于 2013-03-27T19:00:54.970 回答
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);

这应该让你开始。

于 2013-03-27T19:07:58.130 回答
0

您正在使用 AND 运算符(($row['HidePhoneNumber'] == 1) && ($row['HideFaxNumber'] == 1) 这意味着两个条件都必须为真才能执行条件代码。要解决您的问题,您可以在您的 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";
   }
  }

如果条件返回 1,它将回显关联的数组成员。

于 2013-03-27T19:20:51.150 回答