无法在表格中显示逗号分隔的值 [英] Unable to show comma separated values in a table

查看:72
本文介绍了无法在表格中显示逗号分隔的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将id作为逗号分隔的值存储在数据库表中,并且我想在html表中显示每个id各自名称的列表.到现在为止,我还没有找到解决方案,因为当前正在发生的情况是,该表仅针对逗号分隔列表中选择的第一个ID显示values.我能否对此有一些见解,以了解如何将每个id的值显示为html表中的逗号分隔.

I have id's stored as comma separated values in my database table and I want to show listing of each id's respective name in a html table. Till now I have failed to find some solution for this because whats happening currently is the table is showing values only for the first id picked up in the comma separated listing. Can I have some insights on this to how to show up each id's value as comma separated in html table.

<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.很抱歉弄乱了代码,但是已经尝试了很多事情,但没有得到确切的结果.

$desid3 is where I want to show values like A,B and so on. What I am getting here is only A in each html table row. Sorry for the messed up code but have been trying so many things but not getting the exact result.

推荐答案

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

All your loops are wrong, For example

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

这里的问题是您在每次迭代时都覆盖了变量,而没有使用is.这样做的结果是您最后只有变量中的最后一个值.同样在这种情况下,您的分配也将倒退.您应该具有:

The problem here is you are overwriting the variable on each iteration without using is. What this does is you end up with only the last value being in the variable. Also in this case your assignment is backwards too. You should have:

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

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

Unless your trying to do something totally different, but regardless it's wrong. For example you could be trying to update the value, but even that it's not done right.

反正.

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

What you have to do is inside the loop, you need to put the HTML you are outputting so you can output the value on each iteration of the loop.

因此,仅出于示例的目的,可以说$des_cc是这样的数组:

So just for examples sake, lets say $des_cc is an array like this:

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

现在好好循环一下吧

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

echo $rep;

这将输出

6

哪个是分配给$rep的最后一个值.现在我们在循环内输出该变量了吗?

Which is the last value assigned to $rep. Now had we output that variable inside the loop like this:

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

它将输出此内容(假设我们添加了行返回):

It would output this (assuming we added a line return):

1
2
3
4
5
6

如果将该变量放入HTML,则同样适用.希望这是有道理的.

The same holds true if you put that variable into your HTML. Hope that makes sense.

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

A few other examples of this mistake in your code:

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

AND

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

您可以做的另一件事是将数据存储到另一个数组中,就像这样

The other thing you can do, is store that data into another array, like this

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

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

This example basically copies the array into $rep one element at a time. I don't think that is what you want, I only mention it for completeness.

这篇关于无法在表格中显示逗号分隔的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆