如果文件不存在,则回显员工姓名

时间:2016-11-10 20:41:43

标签: php mysql

我使用以下代码尝试显示没有个人资料图片的员工记录。配置文件图片全部存储在标记为id_number.jpg(例如4567.jpg)的图片/文件夹中。如果找不到图片,我想回复员工的姓名。

$ID = $row_Recordset7['ID'];
$image = 'pics/' . $ID . '.jpg';

if (!file_exists($image)) {
echo $row_Recordset7['Employee_Name'];
}

目前,所有员工姓名都在回应。任何帮助将不胜感激。

mysql_select_db($database_schedule, $schedule); 
$query_Recordset7 = "SELECT DISTINCT ID, Employee_Name, Department 
                    FROM unit 
                    ORDER BY Department, Employee_Name"; 
$Recordset7 = mysql_query($query_Recordset7, $schedule) or die(mysql_error()); 
$row_Recordset7 = mysql_fetch_assoc($Recordset7); 
$totalRows_Recordset7 = mysql_num_rows($Recordset7);

完整代码:

mysql_select_db($database_schedule, $schedule);
$query_Recordset7 = "SELECT DISTINCT schedule.ID, schedule.Employee_Name, schedule.Department FROM schedule ORDER BY schedule.Employee_Name";
$Recordset7 = mysql_query($query_Recordset7, $schedule) or die(mysql_error());
$row_Recordset7 = mysql_fetch_assoc($Recordset7);
$totalRows_Recordset7 = mysql_num_rows($Recordset7);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Lookup: Missing Pics</title>
    <style type="text/css">
    <!--
    body,td,th {
        font-size: 30px;
    }
    -->
    </style>
</head>

<body>
<table width="100%" border="0" align="center">
    <tr>
        <td colspan="3" align="center">
            <div align="left">
                <a href="../.">Home</a>
                &gt;Missing Pics
            </div>
        </td>
    </tr>
    <tr>
        <td align="center">
            <div align="left">
                <strong>Name </strong>(<?php echo $totalRows_Recordset7 ?>)
            </div>
        </td>
        <td align="center">
            <strong>ID</strong>
        </td>

    </tr>
    <?php do { ?>
        <tr bgcolor="<?php
            if($SSAdv_m1%$SSAdv_change_every1==0 && $SSAdv_m1>0){
                $SSAdv_k1++;
            }
            print $SSAdv_colors1[$SSAdv_k1%count($SSAdv_colors1)];
            $SSAdv_m1++;
        ?>">
            <td align="center">
                <div align="left">
                    <a href="../report.php?recordID=<?php echo $row_Recordset7['ID']; ?>"> 
                        <?php
                        $ID = $row_Recordset7['ID'];
                        $image = '../pics/' . $ID . '.jpg';

                        if (!file_exists($image)) {
                            echo $row_Recordset7['Employee_Name'];
                        }else{
                        }
                        ?>
                    </a>
                </div>
            </td>
            <td align="center">&nbsp;</td>
        </tr>
    <?php } while ($row_Recordset7 = mysql_fetch_assoc($Recordset7)); ?>
</table>
<br />
<?php echo $totalRows_Recordset7 ?>   Total
</body>
</html>

2 个答案:

答案 0 :(得分:3)

由于您提供给我和RiggsFolly的所有信息,您必须循环SQL结果以获取您想要的数据然后应用条件:

mysql_select_db($database_schedule, $schedule); 
$query_Recordset7 = "SELECT DISTINCT ID, Employee_Name, Department 
                    FROM unit 
                    ORDER BY Department, Employee_Name"; 
$Recordset7 = mysql_query($query_Recordset7, $schedule) or die(mysql_error());     
$totalRows_Recordset7 = mysql_num_rows($Recordset7);

while ($row_Recordset7 = mysql_fetch_assoc($Recordset7)) {
    $ID = $row_Recordset7['ID'];
    $image = '../pics/' . $ID . '.jpg';

    if (!file_exists($image)) {
        echo $row_Recordset7['Employee_Name'];
    }
}

希望它有所帮助。

PS:请记住mysql_是不安全的,你最好用准备好的陈述来学习PDOmysqli_

答案 1 :(得分:0)

不完全明白你要做什么,但显然它是这样的:

$ID = $row_Recordset7['ID'];
$image = 'pics/' . $ID . '.jpg';

if (file_exists($image)) {
    // if you have a file (it's a photo of an employee?) - show it
    echo '<img src="' . $image . '" title="" alt="">';
} else {
    // if there's no file - echo name
    echo $row_Recordset7['Employee_Name'];
}
相关问题