php - 无法在表中显示逗号分隔值

标签 php html mysql

我在数据库表中将id存储为逗号分隔值,并且我想在html表中显示每个id各自名称的列表。到目前为止,我还没有找到解决方案,因为当前发生的情况是表仅显示逗号分隔列表中拾取的第一个 id 的值。我可以对如何在 html 表中以逗号分隔的形式显示每个 id 的值有一些见解吗?

<table id="example"   class="table table-striped table-hover table-bordered  dt-responsive nowrap"  cellspacing="0" width="100%">
<thead>
<tr>

<th>S No.</th>
<th>Level :<br>(Attempt)</th>
<th>Candidate's<br>Designation->id</th>
<th>Reporting<br>Head->id</th>
<th>mail To :</th>
<th>mail CC :</th>
<th>Status</th>
</tr>
</thead>
<tbody>
    <?php

     $sqlQuerybatch     =   "SELECT * from tbl WHERE status = 1 ";

     $sq1           =   $db->query($sqlQuerybatch);
     $i             =   1 ;
    if($db->affected_rows > 0)
    {
        while($row=mysql_fetch_array($sq1))
        {
         extract($row);
         $des_cc=explode(',', $designation_cc_email);

       foreach($des_cc as $out) {
          $out = $rep; 
       }

     $sql_designation =  "SELECT designation from tbl_designation WHERE id = $designation_id ";
                  $sqdes            =   $db->query($sql_designation);
                 if($db->affected_rows > 0)
    {
        while($rowdes=mysql_fetch_array($sqdes))
        { 
        $desid =  $rowdes['designation'];
        } 

             $sql_designation1 =  "SELECT designation from tbl_designation WHERE id = $designation_email ";
                  $sqdes1           =   $db->query($sql_designation1);
                 if($db->affected_rows > 0)
    {
        while($rowdes1=mysql_fetch_array($sqdes1))
        { 
        $desid1 =  $rowdes1['designation'];
        } 

                 $sql_reporting =  "SELECT designation_id,reporting_head from tbl_reporting_head WHERE reporting_head_for = $designation_id and status=1";
                  $sqdes2           =   $db->query($sql_reporting);
                 if($db->affected_rows > 0)
    {
        while($rowdes2=mysql_fetch_array($sqdes2))
        { 
        $desid2 =  $rowdes2['reporting_head'];
        $reportingheadid = $rowdes2['designation_id'];
        } 

      $sql_reporting1 =  "SELECT designation from tbl_designation WHERE id in($rep) ";
        $sqdes3             =   $db->query($sql_reporting1);
      if($db->affected_rows > 0)
    {   
        while($rowdes3=mysql_fetch_array($sqdes3))
        { 
        $desid3 =  $rowdes3['designation'];
        }    
     ?>               
 <tr>
<td><?php echo $i ; ?></td>
<td><?php echo $attemp ; ?></td>
<td><?php echo $desid." -> ".$designation_id ; ?></td>
<td><?php echo $desid2." -> ".$reportingheadid ; ?></td>
<td><?php echo $desid1 ; ?></td> 
<td><?php echo $status ; ?></td>
</tr>
<?php $i++;}} }}}}?>                

</tbody>
</table>

$desid3 是我想显示 A,B 等值的地方。 我在这里得到的只是每个 html 表格行中的 A 。很抱歉代码困惑,但已经尝试了很多事情但没有得到确切的结果。

最佳答案

例如,你的所有循环都是错误的

foreach($des_cc as $out) {
   $out = $rep; 
}

这里的问题是您在每次迭代中覆盖变量而不使用 is。这样做的结果是变量中只有最后一个值。同样在这种情况下,你的任务也是倒退的。你应该有:

foreach($des_cc as $out) {
   $rep = $out; 
}

除非你试图做一些完全不同的事情,但无论如何这是错误的。例如,您可能尝试更新该值,但即使这样做也没有正确完成。

无论如何。

您要做的就是在循环内部,您需要放置要输出的 HTML,以便可以在循环的每次迭代中输出值。

因此,仅举个例子,假设 $des_cc 是一个像这样的数组:

$des_cc = [1,2,3,4,5,6];

现在我们开始循环

foreach($des_cc as $out) {
   $rep = $out; 
}

echo $rep;

这将输出

6

这是分配给 $rep 的最后一个值。现在我们在循环内输出该变量,如下所示:

foreach($des_cc as $out) {
   $rep = $out; 
   echo $rep;
}

它会输出这个(假设我们添加了一行返回):

1
2
3
4
5
6

如果您将该变量放入 HTML 中,同样如此。希望这是有道理的。

代码中此错误的其他一些示例:

while($rowdes=mysql_fetch_array($sqdes)){ 
    $desid =  $rowdes['designation'];
} 

并且

while($rowdes2=mysql_fetch_array($sqdes2))
{ 
    $desid2 =  $rowdes2['reporting_head'];
    $reportingheadid = $rowdes2['designation_id'];
} 

您可以做的另一件事是将数据存储到另一个数组中,如下所示

 $rep = []
 foreach($des_cc as $out) {
    $rep[] = $out; 
 }

此示例基本上一次将数组复制到 $rep 中一个元素。我认为这不是您想要的,我只是为了完整性而提及它。

关于php - 无法在表中显示逗号分隔值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49585558/

相关文章:

javascript - 无需滚动即可转到特定 div

html - 添加特定于具有多个类的组件的 css 规则

html - 当 parent 悬停时, child 会发生变化

sql - mysql 时间戳语法错误

php - DOCKER Config YII2 + MSSQL

php - 在 PHP 中使用静态方法的充分理由是什么?

php - 如何从数据库中获取不同的数据?

javascript - Highcharts JS 与数据库中的值

mysql - 优化 MySQL 查询以避免扫描大量行

mysql - 在 SQL 中,可以对行条件指定限制吗?