user3258571 user3258571 - 24 days ago 12
MySQL Question

Echo employee name if file does not exist

I am using the following code to try to display records of employees without a profile picture. The profile pics are all stored in a pics/ folder labeled as id_number.jpg (e.g. 4567.jpg). If no pic is found, I want to echo the employee's name.

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

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


Currently, all employee names are being echoed. Any help would be appreciated.

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);


Complete Code:

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>

Answer

Since all informations you gave to me and RiggsFolly, you'll have to loop through the SQL result to get the data you want then apply the condition :

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'];
    }
}

Hope it helps.

PS : Remember that mysql_ is insecure and you'd really better learn PDO or mysqli_.

Comments