如何回显上次上传文件的名称

发布于 2024-11-07 15:39:33 字数 6362 浏览 5 评论 0原文

有人要求我输出文件应该只输出最后上传特定名称文件的人的名称和 ID。令人困惑吧?就连我也很困惑。让我举个例子

,假设您的公司有 3 名注册员工。我们称它们为

  1. 皮卡丘、
  2. 雷丘和
  3. 皮丘。

公司内皮卡丘上传了3个文件,雷丘上传了2个文件,皮丘也上传了2个文件。文件还必须根据员工姓名 (ASC) 和文件名 (DESC) 进行排序。我的已经在订购了。这是我唯一的问题。在这种情况下,输出应如下所示:

*注意到员工姓名和员工 ID 位于最后上传的文件中?有人知道该怎么做吗?请我求你们帮帮我吧,TT 这个任务太难了,我只是一个菜鸟,

我想在这里使用 2 张桌子。表 1 用于员工 ID 和员工姓名,表 2 用于文件名等,所以我猜这还涉及 2 个 while 子句。我有这个想法,但我不知道如何将它们混合在一起。

我的代码在我像文森特先生一样编辑之前看起来像这样:

if ($_SESSION[$fgmembersite->GetLoginSessionVar()] == 'sa' 
   OR $_SESSION[$fgmembersite->GetLoginSessionVar()] == 'admin')
{                                   

  $sql= "select * from gmdc_employee 
         where employee_name like '%$search%' 
         AND employee_name like '$listname%'";
} else {
  $sql = "select b.* from gmdc_user a, gmdc_employee b 
          where a.username = '".$_SESSION[$fgmembersite->GetLoginSessionVar()]."' 
          AND a.company_id = b.company_id 
          AND b.employee_name like '$listname%' 
          AND b.employee_name like '%$search%'"; 
}
$query = mysql_query("$sql ORDER BY employee_name,confirmation DESC 
                     ,file_id DESC,file_date DESC 
                      LIMIT $offset,$limit") or die ( mysql_error () );
$result = mysql_query($sql) or die (mysql_error());
$total = mysql_num_rows($result);

if(!$result || mysql_num_rows($result) <= 0)
{
  $fgmembersite->HandleError("No file found.");
  return false;
}
while ($row = mysql_fetch_assoc($query))
{
  $file_id = $row['file_id'];
  $file_desc = $row['file_description'];
  $file_date = $row['file_date'];
  $file_name = $row['file_name'];
  $file_accs = $row['folder_access'];
  $file_employee  = $row['employee_id'];
  $file_confir = $row['confirmation'];
  $file_ename = ucwords($row['employee_name']);

  $info = pathinfo($file_name);
  $file_ext = $info['extension'];

  echo '<tr><td>&nbsp;</td></tr>
        <tr class="subone"><td class="sub" width="100">'.$file_employee.'<br />
        &nbsp;</td>';
  if($_SESSION[$fgmembersite->GetLoginSessionVar()] == 'sa')
  {
    ?>
    <td class="sub" width="100">
    <a href="" onclick = javascript:newPopup('addfile.php?emp=
    <?php echo $file_employee ?>');><?php echo$file_ename?></a>
    <br />&nbsp;</td>
    <?php
  } else {
    echo '<td class="sub" width="182">'.$file_ename.'<br />&nbsp;</td>';
  }
  echo'<td  class="sub" width="218">
       <a href="'.$file_accs.$file_name.'" target="_blank" 
        style="text-decoration: underline;">'.$file_desc.'</a>
       <br />&nbsp;</td><td  class="sub" width="100">
      '.date('M d, Y',mktime(0,0,0,substr($file_date,5,2)
       ,substr($file_date,8,2),substr($file_date,0,4))).'
       <br />&nbsp;</td><td  class="sub" width="100">'.$file_confir.'
       <br />&nbsp;</td>';

  if($_SESSION[$fgmembersite->GetLoginSessionVar()] == 'sa')
  {
    if($file_confir == 'Pending' OR $file_confir == 'NotApproved')
    {
      if(isset($_GET['id']))
      {
        $fgmembersite->Delete_Db($_GET['id']);
      }
      echo '<td  class="sub" width="100">
            <a href="index.php?id='.$file_id.'">Delete</a>
            <br />&nbsp;</td>';
    }
  }
  else if($_SESSION[$fgmembersite->GetLoginSessionVar()] == 'admin')
  {
    if($file_confir == 'Pending')
      {
        if(isset($_GET['yes']))
        {
          $fgmembersite->UpdateYesDB($_GET['yes']);
          //echo "<script>location.reload();</script>";
        }
        else if(isset($_GET['no']))
        {
          $fgmembersite->UpdateNoDB($_GET['no']);
          //echo "<script>location.reload();</script>";
        }
        if (!isset($_GET['offset'])) {
          $prevoffset = 0;
        } else {
          $prevoffset = $_GET['offset'];
          echo'<td  class="sub" width="100">
          <a href="index.php?offset='.$prevoffset.'&searchfile='.$search.'
           &namelist='.$listname.'&yes='.$file_id.'">Approve</a>
           //there's a link here<br /><br />
           <a href="index.php?offset='.$prevoffset.'&searchfile='.$search.'
            &namelist='.$listname.'&no='.$file_id.'">NotApprove</a>
            //there's a link here
            &nbsp;</td> ';
      }
    }
  }?>

只需忽略每个 if($_SESSION[$fgmembersite->GetLoginSessionVar()] == 'admin') 等等,这是不同的输出,因为我的文件可以由 3 个不同的用户登录。 SA(程序员)、admin(上传文件的审批人)、user(公司)。那里也有TD和TR,不知道为什么这里不出现。

输出如下所示:

employee_id  +   employee_name  +  file_name
3            |   pichu          |  file6
3            |   pichu          |  file1
1            |   pikachu        |  file7
1            |   pikachu        |  file4
1            |   pikachi        |  file3
2            |   raichu         |  file8
2            |   raichu         |  file5
2            |   raichu         |  file2

我希望我的输出如下所示:

***OUTPUT***

**employee_id        employee_name              file_name**
    3                      pichu                        file6
                                                        file1
    1                     pikachu                       file7
                                                        file4
                                                        file3
    2                     raichu                        file5
                                                        file2

例如 raichu 上传了另一个文件,输出现在应如下所示:

OUTPUT

**employee_id        employee_name            file_name**
    3                      pichu                      file6
                                                      file1
    1                      pikachu                    file7
                                                      file4
                                                      file3
    2                      raichu                     file8
                                                      file5
                                                      file2

I was asked that the output file should only output the name and id of the last person who uploaded a file with a specific name. confusing right? even I am confused. so let me give an example

let us say that you have 3 employees registered in your company. lets call them

  1. Pikachu,
  2. Raichu and
  3. Pichu.

Pikachu has 3 files uploaded in the company, Raichu has 2 files uploaded and Pichu has also 2 files uploaded. The files must also be ordered according to employeename(ASC) and filename(DESC). Mine is already in ordered by. This is my only problem. In this case the output should look like this:

*noticed that the employee name and employee id is in the last file uploaded?? does anyone knows how to do that?please i beg you guys to help me out here T-T the task is way too hard and im just a rookie

Im thinking of using 2 tables here. table 1 for the employee id and employee name, table 2 for the file name, etc. so i guess this would also involve 2 while clause. i have the idea but i dont know how to mix them all up.

my code before I edited it like sir vincent look like this:

if ($_SESSION[$fgmembersite->GetLoginSessionVar()] == 'sa' 
   OR $_SESSION[$fgmembersite->GetLoginSessionVar()] == 'admin')
{                                   

  $sql= "select * from gmdc_employee 
         where employee_name like '%$search%' 
         AND employee_name like '$listname%'";
} else {
  $sql = "select b.* from gmdc_user a, gmdc_employee b 
          where a.username = '".$_SESSION[$fgmembersite->GetLoginSessionVar()]."' 
          AND a.company_id = b.company_id 
          AND b.employee_name like '$listname%' 
          AND b.employee_name like '%$search%'"; 
}
$query = mysql_query("$sql ORDER BY employee_name,confirmation DESC 
                     ,file_id DESC,file_date DESC 
                      LIMIT $offset,$limit") or die ( mysql_error () );
$result = mysql_query($sql) or die (mysql_error());
$total = mysql_num_rows($result);

if(!$result || mysql_num_rows($result) <= 0)
{
  $fgmembersite->HandleError("No file found.");
  return false;
}
while ($row = mysql_fetch_assoc($query))
{
  $file_id = $row['file_id'];
  $file_desc = $row['file_description'];
  $file_date = $row['file_date'];
  $file_name = $row['file_name'];
  $file_accs = $row['folder_access'];
  $file_employee  = $row['employee_id'];
  $file_confir = $row['confirmation'];
  $file_ename = ucwords($row['employee_name']);

  $info = pathinfo($file_name);
  $file_ext = $info['extension'];

  echo '<tr><td> </td></tr>
        <tr class="subone"><td class="sub" width="100">'.$file_employee.'<br />
         </td>';
  if($_SESSION[$fgmembersite->GetLoginSessionVar()] == 'sa')
  {
    ?>
    <td class="sub" width="100">
    <a href="" onclick = javascript:newPopup('addfile.php?emp=
    <?php echo $file_employee ?>');><?php echo$file_ename?></a>
    <br /> </td>
    <?php
  } else {
    echo '<td class="sub" width="182">'.$file_ename.'<br /> </td>';
  }
  echo'<td  class="sub" width="218">
       <a href="'.$file_accs.$file_name.'" target="_blank" 
        style="text-decoration: underline;">'.$file_desc.'</a>
       <br /> </td><td  class="sub" width="100">
      '.date('M d, Y',mktime(0,0,0,substr($file_date,5,2)
       ,substr($file_date,8,2),substr($file_date,0,4))).'
       <br /> </td><td  class="sub" width="100">'.$file_confir.'
       <br /> </td>';

  if($_SESSION[$fgmembersite->GetLoginSessionVar()] == 'sa')
  {
    if($file_confir == 'Pending' OR $file_confir == 'NotApproved')
    {
      if(isset($_GET['id']))
      {
        $fgmembersite->Delete_Db($_GET['id']);
      }
      echo '<td  class="sub" width="100">
            <a href="index.php?id='.$file_id.'">Delete</a>
            <br /> </td>';
    }
  }
  else if($_SESSION[$fgmembersite->GetLoginSessionVar()] == 'admin')
  {
    if($file_confir == 'Pending')
      {
        if(isset($_GET['yes']))
        {
          $fgmembersite->UpdateYesDB($_GET['yes']);
          //echo "<script>location.reload();</script>";
        }
        else if(isset($_GET['no']))
        {
          $fgmembersite->UpdateNoDB($_GET['no']);
          //echo "<script>location.reload();</script>";
        }
        if (!isset($_GET['offset'])) {
          $prevoffset = 0;
        } else {
          $prevoffset = $_GET['offset'];
          echo'<td  class="sub" width="100">
          <a href="index.php?offset='.$prevoffset.'&searchfile='.$search.'
           &namelist='.$listname.'&yes='.$file_id.'">Approve</a>
           //there's a link here<br /><br />
           <a href="index.php?offset='.$prevoffset.'&searchfile='.$search.'
            &namelist='.$listname.'&no='.$file_id.'">NotApprove</a>
            //there's a link here
             </td> ';
      }
    }
  }?>

Just ignore every if($_SESSION[$fgmembersite->GetLoginSessionVar()] == 'admin') and whatever, this is for different output since my file can be logged into by 3 different users. SA(programmers), admin(the approver of the file uploaded), and user(company). There are TD and TR there as well, I don't know why wont it appear here.

output looked like this:

employee_id  +   employee_name  +  file_name
3            |   pichu          |  file6
3            |   pichu          |  file1
1            |   pikachu        |  file7
1            |   pikachu        |  file4
1            |   pikachi        |  file3
2            |   raichu         |  file8
2            |   raichu         |  file5
2            |   raichu         |  file2

and I want my output to be like this:

***OUTPUT***

**employee_id        employee_name              file_name**
    3                      pichu                        file6
                                                        file1
    1                     pikachu                       file7
                                                        file4
                                                        file3
    2                     raichu                        file5
                                                        file2

for example that raichu uploaded another file, the output should now look like this:

OUTPUT

**employee_id        employee_name            file_name**
    3                      pichu                      file6
                                                      file1
    1                      pikachu                    file7
                                                      file4
                                                      file3
    2                      raichu                     file8
                                                      file5
                                                      file2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枫林﹌晚霞¤ 2024-11-14 15:39:33

使用 Vincent 表结构可能会解决您的问题。为了获得您想要的结果,您可以使用两个表的联接来使用单个查询。所以没有必要使用两个查询。

SELECT  e.employee_id as employee_id,            //Display column for employee_id
        e.employee_name as employee_name,        
        f.file_name as file_name
  FROM employee e                                // The e is an alias for table
 INNER JOIN file f                               // The inner join will return all the rows which have same employee_id on both table 
    ON e.employee_id = f.employee_id
 ORDER BY e.employee_name, f.file_name DESC;     // Order of the result default is ASC

更新

我假设您有两个表,并且结构与 Vincent 相同。现在这个查询将获取两个表上的数据。这里的连接部分是,当 employee 表 上的 employee_idfile 表 上具有相同的 employee_id 时,这将返回一行。请参阅 mysql join 了解更多信息。那么顺序e.employee_id就是employee表上的employee_id,由于没有指定顺序,它将使用默认的升序

那么上述查询的输出可能是下面的代码。

employee_id  +   employee_name  +  file_name
3            |   pichu          |  file6
3            |   pichu          |  file1
1            |   pikachu        |  file7
1            |   pikachu        |  file4
1            |   pikachi        |  file3
2            |   raichu         |  file8
2            |   raichu         |  file5
2            |   raichu         |  file2

您会注意到employee_id 和employee_name 是重复的。这是因为查询包含 SELECT column_name 中指定的行中的所有列值。您可以过滤 php 中的重复值,特别是如果您首先将其存储在对象中而不是直接打印它。

PHP

$query = $see_the_query_above;
$result = mysql_query($query) or die(mysql_error($db));
$emp_id = "";     //This will be use to remove employee_id if its already echoed.
$emp_name = "";   //This will be use to remove employee_name if its already echoed.

echo '<table>';
echo '<tr><th>employee_id</th><th>employee_name</th><th>file_name</th></tr>';

while ($row = mysql_fetch_assoc($result)) {
    //Check $emp_id and $emp_name if same with the current, if same just print nothing or space else replace it with new.
    $emp_id = $emp_id == $row['employee_id'] ? "" : $row['employee_id']
    $emp_name = $emp_name == $row['employee_name'] ? "" : $row['employee_name'];

    echo '<tr>';
    echo '<td>'.$emp_id.'<td>';
    echo '<td>'.$emp_name.'</td>';
    echo '<td>'.$row['file_name'].'</td>';
    echo '</tr>';
}
echo '</table>';

Using Vincent table structure will likely solve your problem. And to get the result you wanted, you can use single query using join for the two table. So there's no need to use two query.

SELECT  e.employee_id as employee_id,            //Display column for employee_id
        e.employee_name as employee_name,        
        f.file_name as file_name
  FROM employee e                                // The e is an alias for table
 INNER JOIN file f                               // The inner join will return all the rows which have same employee_id on both table 
    ON e.employee_id = f.employee_id
 ORDER BY e.employee_name, f.file_name DESC;     // Order of the result default is ASC

UPDATE

I assume you have two tables and the structure is same with Vincent. Now This query will get the data on two tables. The join part here is that, when an employee_id on employee table has a same employee_id on the file table, this will return a row. Please see mysql join for more information. Then the order e.employee_id is the employee_id on employee table, since there's no order specified it will use the default which is ascending.

Then output of the above query could be the below code.

employee_id  +   employee_name  +  file_name
3            |   pichu          |  file6
3            |   pichu          |  file1
1            |   pikachu        |  file7
1            |   pikachu        |  file4
1            |   pikachi        |  file3
2            |   raichu         |  file8
2            |   raichu         |  file5
2            |   raichu         |  file2

You will notice that employee_id and employee_name is repeated. This is because the query include all the columns value in a row that specified in SELECT column_name. You can filter the duplicate value in your php, specially if you will store it first in an object rather than directly printing it.

PHP

$query = $see_the_query_above;
$result = mysql_query($query) or die(mysql_error($db));
$emp_id = "";     //This will be use to remove employee_id if its already echoed.
$emp_name = "";   //This will be use to remove employee_name if its already echoed.

echo '<table>';
echo '<tr><th>employee_id</th><th>employee_name</th><th>file_name</th></tr>';

while ($row = mysql_fetch_assoc($result)) {
    //Check $emp_id and $emp_name if same with the current, if same just print nothing or space else replace it with new.
    $emp_id = $emp_id == $row['employee_id'] ? "" : $row['employee_id']
    $emp_name = $emp_name == $row['employee_name'] ? "" : $row['employee_name'];

    echo '<tr>';
    echo '<td>'.$emp_id.'<td>';
    echo '<td>'.$emp_name.'</td>';
    echo '<td>'.$row['file_name'].'</td>';
    echo '</tr>';
}
echo '</table>';
始终不够爱げ你 2024-11-14 15:39:33

听起来好像没那么难。

员工
该表可能有两个字段(以最简单的方式):employee_id、employee_name

TABLE file:
这个表可能有三个字段:file_id,employee_id(这是一个外键),file_name

当文件上传时,将其名称更改为这种格式:“file”+ file_id

因此,我们有:

TABLE employee:

employee_id  |  employee_name
-----------------------------
1            |  pikachu
2            |  raichu
3            |  pichu

TABLE file

file_id  |  employee_id  |  file_name
-------------------------------------
1        |  3            |  file1
2        |  2            |  file2
3        |  1            |  file3
4        |  1            |  file4
5        |  2            |  file5
6        |  3            |  file6
7        |  1            |  file7
8        |  2            |  file8

那么把它们全部编起来真的很容易,你觉得怎么样?

sounds like not that hard.

TABLE employee:
this table may have two fields(in the simplest way): employee_id, employee_name

TABLE file:
this table may have three fields: file_id, employee_id(this is a foreign key), file_name

when the file is uploaded, change its name to this format: "file" + file_id

therefor, we have:

TABLE employee:

employee_id  |  employee_name
-----------------------------
1            |  pikachu
2            |  raichu
3            |  pichu

TABLE file:

file_id  |  employee_id  |  file_name
-------------------------------------
1        |  3            |  file1
2        |  2            |  file2
3        |  1            |  file3
4        |  1            |  file4
5        |  2            |  file5
6        |  3            |  file6
7        |  1            |  file7
8        |  2            |  file8

then it is really easy to make them all up, what do you think?

ぇ气 2024-11-14 15:39:33

假设您使用 MySQL 并假设我们已经使用引用 $db 建立了数据库连接:

$query = 'SELECT employee_id, employee_name FROM `employee` ORDER BY employee_name ASC';
$result = mysql_query($query) or die(mysql_error($db));

echo '<table>';
echo '<tr><th>employee_id</th><th>employee_name</th><th>file_name</th></tr>';

while ($row = mysql_fetch_assoc($result)) {
    echo '<tr>';
    echo '<td>'.$row['employee_id'].'<td>';
    echo '<td>'.$row['employee_name'].'</td>';
    echo '<td>';

    $query2 = 'SELECT file_name FROM `file` WHERE employee_id='.$row['employee_id'].' ORDER BY file_id DESC';
    $result2 = mysql_query($query2) or die(mysql_error($db));

    while ($row2 = mysql_fetch_assoc($result2)) {
        echo $row2['file_name'].'<br />';
    }

    echo '</td>';
    echo '</tr>';
}

echo '</table>';

这段代码将起作用。

assuming you use MySQL and assuming we have establish a database connection using a reference $db:

$query = 'SELECT employee_id, employee_name FROM `employee` ORDER BY employee_name ASC';
$result = mysql_query($query) or die(mysql_error($db));

echo '<table>';
echo '<tr><th>employee_id</th><th>employee_name</th><th>file_name</th></tr>';

while ($row = mysql_fetch_assoc($result)) {
    echo '<tr>';
    echo '<td>'.$row['employee_id'].'<td>';
    echo '<td>'.$row['employee_name'].'</td>';
    echo '<td>';

    $query2 = 'SELECT file_name FROM `file` WHERE employee_id='.$row['employee_id'].' ORDER BY file_id DESC';
    $result2 = mysql_query($query2) or die(mysql_error($db));

    while ($row2 = mysql_fetch_assoc($result2)) {
        echo $row2['file_name'].'<br />';
    }

    echo '</td>';
    echo '</tr>';
}

echo '</table>';

this piece of code will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文