简体   繁体   English

如何编写if else动态语句

[英]how to code an if else dynamic statement

I'm having trouble writing an if else statement. 我在编写if else语句时遇到麻烦。 Considering that I've been doing so for years in ColdFusion, this make me feel very stupid. 考虑到我在ColdFusion中已经这样做多年了,这让我感到非常愚蠢。

Here's the task. 这是任务。 I need to pull first name, last name, email, co-chair status from a database and return the results. 我需要从数据库中提取名字,姓氏,电子邮件,共同主席的身份,然后返回结果。 However not everyone has an email, so I need a statement that will include a mailto link for those that have emails, and exclude a mailto link for those that don't. 但是,并不是每个人都有电子邮件,因此我需要一个声明,其中包含那些有电子邮件的人的mailto链接,并排除那些没有电子邮件的人的mailto链接。

Here's the code I'm using that includes the mailto link for all. 这是我使用的代码,其中包含所有代码的mailto链接。

What adjustments do I need to make? 我需要进行哪些调整? thanks, david 谢谢大卫

 <?php do { ?>
  <a href="mailto:<?php echo $row_GetMembers['BACmember_Email']; ?>"><?php echo $row_GetMembers['BACmember_First']; ?> <?php echo $row_GetMembers['BACmember_Last']; ?></a>
  <?php /*START_PHP_SIRFCIT*/ if ($row_GetMembers['BACmember_CoChair']=="Yes"){ ?>
    <strong> Co-Chair</strong>
    <?php } /*END_PHP_SIRFCIT*/ ?><br />
<?php } while ($row_GetMembers = mysql_fetch_assoc($GetMembers)); ?>

This is the line of code you want to optionally display as a link (split for readability): 这是您要选择显示为链接的代码行(为便于阅读而拆分):

<a href="mailto:<?php echo $row_GetMembers['BACmember_Email']; ?>">
    <?php echo $row_GetMembers['BACmember_First']; ?> 
    <?php echo $row_GetMembers['BACmember_Last']; ?>
</a>

You'll want something like this instead: 您将需要这样的东西:

<?php if (!empty($row_GetMembers['BACmember_Email'])): ?>
    <a href='mailto:<?php echo $row_GetMembers['BACmember_Email']?>>
        <?php echo $row_GetMembers['BACmember_First']; ?> 
        <?php echo $row_GetMembers['BACmember_Last']; ?>
    </a>
<?php else: ?>
    <?php echo $row_GetMembers['BACmember_First']; ?> 
    <?php echo $row_GetMembers['BACmember_Last']; ?>
<?php endif; ?>

If the email field isn't empty (the ! negates the result, so we're checking if it isn't empty), it prints the first and last name inside an anchor tag. 如果电子邮件字段不为空( !否定结果,那么我们正在检查它是否为空),它将在锚标记内打印名字和姓氏。 If it is empty, it prints the name without it. 如果空,则打印不带名称的名称。

A more elegant solution may be to provide the email address as a separate field or link, as opposed to having a list of some names that are links and some that aren't. 更为优雅的解决方案可能是将电子邮件地址提供为单独的字段或链接,而不是使用包含链接名称和不包含链接名称的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM