具体情况后如何显示表格?

时间:2014-02-25 06:19:22

标签: php mysql

我已编码,在上传excel文件时,excel中的数据列表将显示在表格中。

现在问题在于删除数据,单独显示以下特定行。

<tr>
    <th>Title</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Email</th>
    <th>Phone</th>
    <th>Options</th>
    <th><input type="checkbox"  name="delete_all" class="" value='' onClick="deleteAll()"  /></th>
</tr>  

我希望只有当有一行时才会显示整个表格。请帮帮我。

<form name="del_functionality" method="post" action="<?php echo $_SERVER['PHP_SELF'];     ?>">
    <div class="control-group">
        <div class="controls" align="right">        
            <button type="submit" id="deletes" name="delete" value="delete"  data-loading-text="Loading..." onClick="return deleteSelected()">Delete </button>
        </div>
    </div>

    <table class="table">
        <tr>
            <th>Title</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Options</th>
            <th><input type="checkbox"  name="delete_all" class="" value='' onClick="deleteAll()"  /></th>
        </tr>
    <?php
        $res=mysql_query("SELECT * FROM dealer_tbl");
        while($dealer_row=mysql_fetch_array($res))
        {
        ?>
            <tr>

                <td><?php echo $dealer_row['title']; ?></td>
                <td><?php echo $dealer_row['firstname']; ?></td>
                <td><?php echo $dealer_row['lastname']; ?></td>
                <td><?php echo $dealer_row['email']; ?></td>
                <td><?php echo $dealer_row['phone']; ?></td>
                <td align="center"><a href='dealer_edit.php?did=<?=$dealer_row['uid']?>'>Edit</a> </td>
                <td>
                    <input type="checkbox"  name="checkbox_del[]" class="deleteCheckbox" value="<?php echo $dealer_row['uid']; ?>"  />
                    <input type='hidden' name='deleteConfirm' value='0' />
                </td> 
            </tr>
        <?php
        }
    ?>
    </table>
</form> 

2 个答案:

答案 0 :(得分:0)

更改查询位置,如下所示:

<?php 
      $res=mysql_query("SELECT * FROM dealer_tbl");
      $count =mysql_num_rows($res);
 ?>
 <table class="table" <?php if($count == 0) { ?> style="display:none;" <?php }?>>
 ...
 </table>

答案 1 :(得分:0)

试试这个。您可以在form之外的查询中获取行数,如果是greater than 0则可以显示结果。

<?php
$res=mysql_query("SELECT * FROM dealer_tbl");
$num_rows = mysql_num_rows($res);
if($num_rows>0)
{
?>
<form name="del_functionality" method="post" action="<?php echo $_SERVER['PHP_SELF'];     ?>">
    <div class="control-group">
        <div class="controls" align="right">        
            <button type="submit" id="deletes" name="delete" value="delete"  data-loading-text="Loading..." onClick="return deleteSelected()">Delete </button>
        </div>
    </div>

    <table class="table">
        <tr>
            <th>Title</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Options</th>
            <th><input type="checkbox"  name="delete_all" class="" value='' onClick="deleteAll()"  /></th>
        </tr>
    <?php

        while($dealer_row=mysql_fetch_array($res))
        {
        ?>
            <tr>

                <td><?php echo $dealer_row['title']; ?></td>
                <td><?php echo $dealer_row['firstname']; ?></td>
                <td><?php echo $dealer_row['lastname']; ?></td>
                <td><?php echo $dealer_row['email']; ?></td>
                <td><?php echo $dealer_row['phone']; ?></td>
                <td align="center"><a href='dealer_edit.php?did=<?=$dealer_row['uid']?>'>Edit</a> </td>
                <td>
                    <input type="checkbox"  name="checkbox_del[]" class="deleteCheckbox" value="<?php echo $dealer_row['uid']; ?>"  />
                    <input type='hidden' name='deleteConfirm' value='0' />
                </td> 
            </tr>
        <?php
        }
    ?>
    </table>
</form>  
<?php
}
else
{
        echo "No result Found !! ";
}
?>

注意: mysql_*现已弃用,请使用mysqli_*PDO