PHP不会在printf内部调用函数后打印

时间:2018-03-08 17:16:13

标签: php html sql

我已经在这2天了。我有一个联系人页面,显示html表中每个数据库行的所有信息。除了这些信息,我还在数据库中存储了用户上传文件的文件名。我希望将文件名打印为表格行中的列表项。我设法打印它们并使它们成为可下载的链接,但我无法为每行打印自己的文件(在db和文件夹中都存储了id)。这是代码:

 <?php 
            $loop = function($myarray,$id){         
                foreach ($myarray as $ray){
                        print("<li><a href=\"./uploads/$id/$ray\">$ray</a></li>");

                };
            };
            foreach ($dbh->query("SELECT id, firstname, lastname, fathersname, mothersname, mobile, fixed1, fixed2, email, work, street, tk, city, files FROM contacts") as $row) {
                $query1 = ("SELECT files FROM contacts WHERE id=$row->id");
                $res = $dbh->query($query1);
                $serialized_arr = $res->fetch(PDO::FETCH_ASSOC);
                $un_arr= unserialize($serialized_arr["files"]);

                printf("
                <tr>
                <td id=\"firstname$row->id\"> %s </td>
                <td id=\"lastname$row->id\"> %s </td>
                <td id=\"fathersname$row->id\"> %s </td> 
                <td id=\"mothersname$row->id\"> %s </td>
                <td id=\"mobile$row->id\"> %s </td>
                <td id=\"fixedone$row->id\"> %s </td>
                <td id=\"fixedtwo$row->id\"> %s </td>
                <td id=\"email$row->id\"> %s </td>
                <td id=\"work$row->id\"> %s </td>
                <td id=\"street$row->id\"> %s </td>
                <td id=\"tk$row->id\"> %s </td>
                <td id=\"city$row->id\"> %s </td>
                <td>
                     $loop($un_arr, $row->id)
                </td>
                <td> <button id=\"edit_button<?php echo $row->id;?>\" class=\"btn1\"  ><i class=\"fas fa-edit\"> </i> </button>        
                    <a href=\"delete.php?id=$row->id\" id=\"delete_button$row->id;?>\" class=\"btn1\"  onclick=\"return confirm('Είσαι Σίγουρος/η? Τα αρχεία που ανέβασες με την καταχώρηση δεν θα σβηστούν!');\" value=\"\"><i class=\"fas fa-trash\"></i></a>
                </td>
                </tr> " , $row->firstname, $row->lastname, $row->fathersname, $row->mothersname, $row->mobile, $row->fixed1, $row->fixed2, $row->email, $row->work, $row->street, $row->tk, $row->city

                );
            }
        ?>

此代码出现此错误:可捕获的致命错误:类Closure的对象无法转换为字符串。 如果我把它改成这个                      <td> ". $loop($un_arr, $row->id)." </td>     它打印正确的结果,但在网站的左上角,而不是在表格内。有解决方案吗提前谢谢!

1 个答案:

答案 0 :(得分:0)

PHP正在尝试打印名为$loop的变量。

由于分配给它的闭包(错误中提到的同一个),负责print ing,只需打破原来的printf调用!

printf("
    <tr>
         ...other cells here...
         <td id=\"city$row->id\"> %s </td>
         <td>
");

// Stop, at this point, and let your closure print the contents of the image cell!
$loop($un_arr, $row->id)

// Then, continue along with a new print statement!
printf(
    </td>
    <td>
        <button id=\"edit_button<?php echo $row->id;?>\" class=\"btn1\"  ><i class=\"fas fa-edit\"></i></button>        
        <a href=\"delete.php?id=$row->id\" id=\"delete_button$row->id;?>\" class=\"btn1\"  onclick=\"return confirm('Είσαι Σίγουρος/η? Τα αρχεία που ανέβασες με την καταχώρηση δεν θα σβηστούν!');\" value=\"\"><i class=\"fas fa-trash\"></i></a>
        </td>
        </tr>", $row->firstname, $row->lastname, $row->fathersname, $row->mothersname, $row->mobile, $row->fixed1, $row->fixed2, $row->email, $row->work, $row->street, $row->tk, $row->city
);