PHP的一栏表格中有多个值

时间:2019-02-23 10:41:42

标签: javascript php html mysql

我在制作pdf时遇到问题,数据库书和作者中有2个表,问题是如果书是2个或更多作者,我无法在下面的仅一列中显示它是联接书和作者的图像和我制作的pdf。

我的数据有2位作者如何将它们排成一行或将它们合并成一列

My Data There`s a 2 author how to make them one row or join them in one column

我在下面制作的pdf文件是代码

The pdf that i make below is the code

$sqlpatient = mysqli_query($connect, "select * from book");  
   $rowpatient = mysqli_fetch_array($sqlpatient);

                do{
                    if(mysqli_num_rows($sqlpatient) != 0){  
                $bookid = $rowpatient['bookid'];
            $authorsql = mysqli_query($connect, "SELECT author FROM author WHERE bookid = '$bookid' ");
  $output .= '<tr>  
                      <td>'.$rowpatient["bookid"].'</td>  
                      <td>'.$rowpatient["callnumber"].'</td>  
                      <td>'.$rowpatient["isbn"].'</td>  
                      <td>'.$rowpatient["bookname"].'</td>  '; 
                      while ($authorrow = mysqli_fetch_array($authorsql)) {
                      $author =  $authorrow["author"].",";

                       $output .= '<td>'. $author.'</td>';
                   }
                     $output .= ' <td style="text-transform: capitalize;">'.$rowpatient["genre"].'</td>
                     <td>'.$rowpatient["publisher"].'</td>  
                      <td>'.$rowpatient["publishdate"].'</td>  
                 </tr>  
                      ';  

   }
                }while($rowpatient = mysqli_fetch_array($sqlpatient));  
  return $output;

3 个答案:

答案 0 :(得分:1)

您可以创建一个空字符串,然后在while循环内将其与作者的名字连接起来,当循环结束时,创建的变量将具有所有作者的名字,然后您可以创建一个表列并将该变量放入其中。 您可以执行此操作或创建一个数组并将其值推入循环内,然后将内爆函数与','一起使用,该函数返回数组元素的字符串,检查https://www.w3schools.com/php/func_string_implode.asp

答案 1 :(得分:0)

问题在于您正在为每个作者在循环中创建一个新的<td></td>对,如果他们的作者多于一个,这就是一团糟。

尝试将所有作者添加到字符串中,然后在循环后使用一对 <td></td>

// Define an empty string that we will append all authors to
$authors = '';

while ($authorrow = mysqli_fetch_array($authorsql)) {
    $authors .= $authorrow["author"].",";
}

// I've used trim() to trim away the trailing comma.
$output .= '<td>' . trim($authors, ',') .  '</td>';   

答案 2 :(得分:0)

据我对问题的理解,您需要使用implode()函数

<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>

输出:-世界您好!美丽的一天!