Php 如何回显上次上载文件的名称
我被要求在输出文件中只输出最后一个上传文件的人的姓名和id。困惑是吧?就连我也感到困惑。让我举个例子 假设你有3名员工在你的公司注册。让我们给他们打电话Php 如何回显上次上载文件的名称,php,sql,html,echo,Php,Sql,Html,Echo,我被要求在输出文件中只输出最后一个上传文件的人的姓名和id。困惑是吧?就连我也感到困惑。让我举个例子 假设你有3名员工在你的公司注册。让我们给他们打电话 皮卡丘 雷楚和 比丘 Pikachu在公司上传了3个文件,Raichu上传了2个文件,Pichu也上传了2个文件。文件还必须根据employeename(ASC)和filename(DESC)排序。我的已经被订购了。这是我唯一的问题。在这种情况下,输出应如下所示: if ($_SESSION[$fgmembersite->GetLogin
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> ';
}
}
}?>
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
*注意,员工姓名和员工id在上次上传的文件中??有人知道怎么做吗?拜托,我求求你们帮我这个任务太难了,我只是个新手
我想在这里用两张桌子。表1给出了员工id和员工姓名,表2给出了文件名,等等。所以我猜这也会涉及到2 while子句。我有这个想法,但我不知道如何把它们都混在一起
我的代码在编辑之前就像vincent爵士一样如下所示:
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> ';
}
}
}?>
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
我希望我的输出是这样的:
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> ';
}
}
}?>
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上载了另一个文件,输出现在应该如下所示:
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> ';
}
}
}?>
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
输出
听起来没那么难 表
员工
:此表可能有两个字段(以最简单的方式):employee\u id、employee\u name 表
文件
:此表可能有三个字段:文件id、员工id(这是外键)、文件名 上传文件时,将其名称更改为以下格式:“文件”+文件id 为此,我们: 表
员工
:
employee_id | employee_name
-----------------------------
1 | pikachu
2 | raichu
3 | pichu
那真的很容易编造出来,你觉得呢?听起来没那么难 表
员工
:此表可能有两个字段(以最简单的方式):employee\u id、employee\u name 表
文件
:此表可能有三个字段:文件id、员工id(这是外键)、文件名 上传文件时,将其名称更改为以下格式:“文件”+文件id 为此,我们: 表
员工
:
employee_id | employee_name
-----------------------------
1 | pikachu
2 | raichu
3 | pichu
然后很容易将它们全部组合起来,您觉得如何?假设您使用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>';
$query='根据员工姓名ASC从'employee'订单中选择员工id、员工姓名';
$result=mysql\u query($query)或die(mysql\u error($db));
回声';
回显“员工名称员工名称文件名称”;
while($row=mysql\u fetch\u assoc($result)){
回声';
回显“.$row['employee_id']”;
回显'.$row['employee_name'].';
回声';
$query2='从'file`中选择文件名,其中employee_id='。$row['employee_id']。'ORDER BY file_id DESC';
$result2=mysql_query($query2)或die(mysql_error($db));
而($row2=mysql\u fetch\u assoc($result2)){
echo$row2['file_name'].
;
}
回声';
回声';
}
回声';
这段代码可以工作。假设您使用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>';
$query='根据员工姓名ASC从'employee'订单中选择员工id、员工姓名';
$result=mysql\u query($query)或die(mysql\u error($db));
回声';
回显“员工名称员工名称文件名称”;
while($row=mysql\u fetch\u assoc($result)){
回声';
回显“.$row['employee_id']”;
回显'.$row['employee_name'].';
回声';
$query2='从'file`中选择文件名,其中employee_id='。$row['employee_id']。'ORDER BY file_id DESC';
$result2=mysql_query($query2)或die(mysql_error($db));
而($row2=mysql\u fetch\u assoc($result2)){
echo$row2['file_name'].
;
}
回声';
回声';
}
回声';
这段代码可以工作。使用Vincent表结构可能会解决您的问题。为了得到想要的结果,可以对两个表使用join进行单个查询。所以不需要使用两个查询
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
更新
我假设你有两张桌子,结构和文森特是一样的。现在,此查询将获得两个表上的数据。这里的连接部分是,当员工表
上的员工id
在文件表
上具有相同的员工id
时,这将返回一行。有关更多信息,请参阅。然后,订单e.employee\u id
是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\u 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>';
$query=$参见上面的查询;
$result=mysql\u query($query)或die(mysql\u error($db));
$emp_id=“”//如果员工id已被回显,则此选项将用于删除该id。
$emp_name=“”//如果员工的姓名已被回显,则将使用此选项删除该姓名。
回声';
回显“员工名称员工名称文件名称”;
while($row=mysql\u fetch\u assoc($result)){
//检查$emp_id和$emp_name(如果与当前相同),如果相同,则不打印任何内容或空格,否则将其替换为新内容。
$emp\u id=$emp\u id==$row['employee\u id']?“”:$row['employee\u id']
$emp_name=$emp_name==$row['employee_name']?“”:$row['employee_name'];
回声';
回显“.$emp_id.”;
回音“.$emp_name.”;
回显'.$row['file_name'].';
回声';
}
回声';
使用Vincent表结构可能会解决您的问题。为了得到想要的结果,可以使用单个查询