0

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>
JazZ
  • 4,469
  • 2
  • 20
  • 40
user3258571
  • 386
  • 3
  • 17
  • Check the contents of `$row_Recordset7['ID']` and then check that `'pics/' . $ID . '.jpg';` actually exist – RiggsFolly Nov 10 '16 at 20:44
  • 1
    Also try using an absolute path instead of a relative one. – bassxzero Nov 10 '16 at 20:44
  • $row_Recordset7['ID']; exists as I can see all employee names with their ID. – user3258571 Nov 10 '16 at 20:45
  • Same result with absolute path...all employees. – user3258571 Nov 10 '16 at 20:46
  • 1
    "stored in a pic\ folder" but you're looking into "pics/..." – JazZ Nov 10 '16 at 20:47
  • Not sure if it's the issue, but you use `pics` in your code, but say they're in `pic` – Joseph Young Nov 10 '16 at 20:47
  • It should be pics/. Thanks to Adrien I double-checked my path and needed to add ../pics. Now I am getting blank rows for with missing employee names echoing correctly. How can I just echo names of missing without the blank rows? – user3258571 Nov 10 '16 at 20:52
  • I corrected my incorrect narrative. Now I am getting blank rows for with missing employee names echoing correctly. How can I just echo names of missing without the blank rows? – user3258571 Nov 10 '16 at 20:53
  • Just echo names) I don't see the problem. – u_mulder Nov 10 '16 at 20:54
  • I am now still getting blank rows for the employees who have a jpg and echoing the names of the the missing. In a table of 200 employees with 195 pics I just need the 5 names echoing of the ones without a pic. – user3258571 Nov 10 '16 at 20:59
  • Seems that you didn't shared all the relevant code. There is nothing in the code you shared that must echo blank rows. – JazZ Nov 10 '16 at 21:01
  • I don't want the blank rows to echo blank. Just the 5 names missing their pic. – user3258571 Nov 10 '16 at 21:01
  • I understood. I'm saying that the code you shared does not echo blank rows. Did you shared all the relevant code ? – JazZ Nov 10 '16 at 21:03
  • Yes. What I shared is what is currently showing the 5 names mixed in with 195 blank rows. Need to figure out how to get rid of the blanks rows. – user3258571 Nov 10 '16 at 21:04
  • This code is part of a loop, isn't it ? Can you edit your question with the whole loop code ? – JazZ Nov 10 '16 at 21:05
  • 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); – user3258571 Nov 10 '16 at 21:10
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Nov 10 '16 at 21:11
  • I'm using Dreamweaver CS6. It is the default. – user3258571 Nov 10 '16 at 21:13
  • Halala ! you made @RiggsFolly mad ! = P – JazZ Nov 10 '16 at 21:15
  • @AdrienLeber No he didn't When I am mad **I use bold text** and **GROWL** :) – RiggsFolly Nov 10 '16 at 21:15
  • Hehe, I'll remember. – JazZ Nov 10 '16 at 21:16
  • I dont see any looping being done in the code you show us. Show us a complete pieces of code OR **we can just keep on guessing** – RiggsFolly Nov 10 '16 at 21:20
  • I did not loop. Any help would be appreciated. I was using the query to show all employees. My guess is that I used the wrong method. – user3258571 Nov 10 '16 at 21:27
  • . in .ID was a typo. I will fix above. – user3258571 Nov 10 '16 at 21:28
  • Can you help with the loop. – user3258571 Nov 10 '16 at 21:35
  • Hehe, there is a loop up there. – JazZ Nov 10 '16 at 21:42

2 Answers2

3

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_ with prepared statement.

JazZ
  • 4,469
  • 2
  • 20
  • 40
0

Don't fully understand what are you tring to do, but obviously it is something like:

$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'];
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • I just want to display the 5 employee names that do not have a profile pic. In a table of 200 employees with 195 pics I just need the 5 names echoing of the ones without a pic. Is there a way to use file_exists($image) in the MYSQL query? – user3258571 Nov 10 '16 at 21:06