0

I have a MySQL database storing some basic user info, so: name, email, and two phone numbers. I want to make a page with dynamic php text showing the users info, but in the database only one phone number is required. On the page I have:

<p id="first"> Some non-dynamic text </p>

<p> <?php echo $row_rsCustomer['first_name']; ?> </p>
<p> <?php echo $row_rsCustomer['email']; ?> </p>
<p> <?php echo $row_rsCustomer['phone_one']; ?> </p>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>

<p id="second"> Some non-dynamic text </p>

I dont want the #second to be pushed down if the #phonetwo is empty. I was thinking of using some javascript like this:

if( #phonetwo.innerhtml == ""){
    #phonetwo.style.display="none";

But I was wondering if there's a way to do this using php? The javascript solution will work I suppose, but I'm pretty sure I've seen somewhere a more "correct" way to do this, I just don't remember what it was nor can I find it anywhere.

4

5 回答 5

1

只需将段落包装在条件中

<?php if(isset($row_rsCustomer['phone_two']) && $row_rsCustomer['phone_two']):?>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>
<?php endif?>
于 2013-09-06T18:38:43.750 回答
0

每当您需要对 PHP 隐藏 HTML 时,只需遵循此模式即可:

<? if (your condition) { ?>
Html
<? } ?>

这比在 JS 中执行要好得多,因为它不会向浏览器发送任何数据。

于 2013-09-06T18:42:36.027 回答
0

检查是否$row_rsCustomer['phone_two']不为空,然后显示您的<p>

<?php if (isset($row_rsCustomer['phone_two'])) { ?>
   <p id="phonetwo"><?php echo $row_rsCustomer['phone_two'] ?></p>
<?php } ?>
于 2013-09-06T18:48:27.530 回答
0

首先检查它是否为空

<?php if(!empty($row_rsCustomer['phone_two'])) { ?>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>
<?php } ?>
于 2013-09-06T18:39:48.570 回答
0
<?php if (!empty($row_rsCustomer['phone_two'])) { ?>

<p id="phonetwo"><?php echo $row_rsCustomer['phone_two'] ?></p>

<?php } ?>
于 2013-09-06T18:39:50.077 回答