PHP - 如果数据库字段为空,如何删除逗号或图像

时间:2016-03-24 11:57:14

标签: php

我正在试图找出如何删除逗号,比如说 [' address_2']字段,如果这是空的,如果没有输入,也不会显示[' tel']电话号码的图标,这似乎很难在回声中工作。

          $numrows = mysql_num_rows($query);
                if ($numrows > 0){

                    while ($row = mysql_fetch_assoc($query)) {
                        echo "<div class='result-container'>

                                  <div class='result-wrap'>

                                        <div class='leftbox'> 

                                            <div class='titlebox'>
                                                <h2>" . $row['company_name'] . "</h2>
                                                <p><strong>" . $row['company_services'] . "</strong></p>
                                            </div>

                                            <div class='box'><strong> Address: </strong>" . $row['address_1'] . ",&nbsp" . $row['address_2'] . ",&nbsp" . $row['town'] . ",&nbsp" . $row['county'] . ",&nbsp" . $row['postcode'] . "</div>

                                            <div class='box'><strong> Telephone: </strong>" . $row['tel'] ."<strong> Mobile: </strong>". $row['mob'] . "</div>

                                                <div class='iconbox'>
                                                    <a href='" . $row['tel'] . "'><img src='img/icons/phone.png' class='hvr-push'></a>
                                                    <a href='" . $row['mob'] . "'><img src='img/icons/mobile.png' class='hvr-push'></a>
                                                    <a href='mailto:" . $row['email'] . "'><img src='img/icons/email.png' class='hvr-push'></a>
                                                    <a href='" . $row['web_url'] . "'><img src='img/icons/web.png' class='hvr-push'></a>
                                                </div>

                                            <div class='buttonwrap'><div class='buttonbox'><p>View Profile</p></div></div>          

                                        </div> 

                                        <div class='rightbox'>

                                        <div class='service-pic'>
                                                <img src='img/image1.png'>
                                            </div>

                                            <div class='mapbox'>
                                                " . $row['gmap_url'] . "
                                            </div>



                                       </div>

                        </div>
                        <br/>"; 

                    }

                }

1 个答案:

答案 0 :(得分:0)

    $numrows = mysql_num_rows($query);
                if ($numrows > 0){

                    while ($row = mysql_fetch_assoc($query)) {
                        ?>
<div class='result-container'>
<div class='result-wrap'>
<div class='leftbox'> 

<div class='titlebox'>
                                                
<h2><?=$row['company_name']?></h2>
                                                
<p><strong><?=$row['company_services']?></strong></p>
</div>

                                            <div class='box'><strong> Address: </strong><?=$row['address_1']?>,

    <?php
    if(!empty($row['address_2'])){
    ?>
    &nbsp <?=$row['address_2']?>,
    <?php
    }
    ?>
    &nbsp <?=$row['town']?>,&nbsp <?=$row['county']?>,&nbsp <?=$row['post code']?></div>
 <div class='box'><strong> Telephone: </strong><?=$row['tel']?><strong> Mobile: </strong><?=$row['mob']?></div><div class='iconbox'>
                                                        <a href='
    <?=$row['tel']?>'><img src='img/icons/phone.png' class='hvr-push'></a>
                                                        <a href='
    <?=$row['mob']?>'><img src='img/icons/mobile.png' class='hvr-push'></a>
                                                        <a href='mailto:"
    <?=$row['email']?>'><img src='img/icons/email.png' class='hvr-push'></a>
                                                        <a href='"
    <?=$row['web_url']?>'><img src='img/icons/web.png' class='hvr-push'></a>
                                                    </div>
                                                <div class='buttonwrap'><div class='buttonbox'><p>View Profile</p></div></div>          
                                            </div> 
                                            <div class='rightbox'>
                                            <div class='service-pic'>
                                                    <img src='img/image1.png'>
                                                </div>
                                                <div class='mapbox'>
                                                    <?=$row['map_url']?>
                                                </div>
                                           </div>

您可以使用Mysql,请在此处查看:http://php.net/manual/es/book.mysqli.php 最后一个提示:'不要将HTML输入回声'