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

时间:2018-03-31 07:51:16

标签: php html mysql

我将import xlwt style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on') wb = xlwt.Workbook(encoding="utf-8") ws = wb.add_sheet('test') with open("data.txt") as outfile: row = 0 for line in outfile: line = line.strip().split("\t") for col, val in enumerate(line): ws.write(row, col, val) row = row + 1 wb.save('data.xls') 存储为数据库表中的逗号分隔值,并且我希望在html表中显示每个id各自名称的列表。到目前为止,我还没有找到解决方法,因为目前发生的事情是表格仅显示在逗号分隔列表中拾取的第一个ID id。我是否可以对此如何在html表中以逗号分隔的方式显示每个id的值。

values

<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等值的地方。 我在这里得到的只是每个html表行中的A.很抱歉这些混乱的代码,但一直在尝试这么多东西,但没有得到确切的结果。

1 个答案:

答案 0 :(得分:0)

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

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个元素中。我认为这不是你想要的,我只是为了完整而提到它。