将td值在codeigniter中显示为静态

时间:2018-08-20 05:00:51

标签: php codeigniter

嗨,我正在尝试将第一个td值显示为静态,所以我将这些值保留在一个数组中,我尝试增加这些值并尝试显示,但是当我得到它时,如果有在foreach中有一条记录显示一个值,如果我有2条记录,则显示两个值。

但是如果我在foreach中也有值,我想显示所有td值。

这是我的代码:

 <tbody>
                                <?php
                              $arr = array(0=>'On Hold',1=>'Asset Incomplete',2=>'SME Discussion',3=>'a',4=>'b',5=>'c',6=>'d',7=>'e',8=>'f',9=>'g',10=>'h');
                              $i = 0;
                                foreach($getCourse as $report)
                                  $status= $report->status;
                                  {
                                    ?>
                                  <tr>
                                      <td><?php echo $arr[$i]; ?>
                                      <?php $i++;  ?></td>
                                    <td><?php
                                                 if($status==1)
                                                 {
                                                    echo "On Hold"; 
                                                 }
                                                 elseif($status==2)
                                                 {
                                                     echo "Asset Incomplete";
                                                 }
                                                 elseif($status==3)
                                                 {
                                                     echo "Yet to Start";
                                                 }
                                                 elseif($status==4)
                                                 {
                                                     echo "SME Discussion";
                                                 }
                                                 elseif($status==5)
                                                 {
                                                     echo "Development";
                                                 }
                                                 elseif($status==6)
                                                 {
                                                     echo "PB Review";
                                                 }
                                                 elseif($status==7)
                                                 {
                                                     echo "PB Fixes";
                                                 }
                                                 elseif($status==8)
                                                 {
                                                     echo "PB2 Review";
                                                 }
                                                 elseif($status==9)
                                                 {
                                                     echo "PB2 Fixes";
                                                 }
                                                 elseif($status==10)
                                                 {
                                                     echo "Alpha Development";
                                                 }
                                                 elseif($status==11)
                                                 {
                                                     echo "Alpha Review";
                                                 }
                                                 elseif($status==12)
                                                 {
                                                     echo "Alpha Fixes";
                                                 }

                                                 elseif($status==13)
                                                 {
                                                     echo "Beta Review";
                                                 }
                                                 elseif($status==14)
                                                 {
                                                     echo "Beta Fixes";
                                                 }
                                                 elseif($status==15)
                                                 {
                                                     echo "Gamma";
                                                 }

                                               ?></td>
                                    <td><?php echo $report->coursename; ?></td>
                                    <td><?php echo $report->statuscount;?></td>
                                    <td></td>
                                </tr>
                               <?php
                                }
                                ?>
                            </tbody>

这是我的控制人:

public function index()
{
        if ($this->session->userdata('is_logged')) {
        $data['getCourse']=$this->Report_model->getTopicReports();
    $this->load->view('template/header');
        $this->load->view('reports/report',$data);
        $this->load->view('template/footer');
}
    else {
        redirect("Login");
    }
}

这是我的模特:

public function getTopicReports()
{
     $this->db->select('count(t.status) as statuscount,t.topicName as topicname,c.coursename,t.status')
     ->from('topics t')
     ->join('course c', 't.courseId = c.id')
     ->where('t.courseid',1)
     ->where('t.status',5);
     $query = $this->db->get();
     return $query->result();
}

这是我要在此处显示的参考图像: enter image description here

任何人都可以提前帮助我该怎么做。

2 个答案:

答案 0 :(得分:2)

最终更新和工作版本

对我来说,这很棘手。原来这是我觉得很尴尬的索引问题。

问题是您的数据不能很好地优化以适应您要执行的任务。遍历表格时,您必须进行奇怪的比较。我能够完成它。

我实际上对看到一种更精致的方法非常感兴趣。但是在此之前,此代码将动态生成一个表,该表与您在问题中发布的示例图片的输出匹配。

我已经用一些示例数据测试了此代码,这些示例数据与您在注释中发布的数组结构相匹配。

代码如下:

//Create an array with your status values.
$rowName = array(

  'On Hold',
  'Asset Incomplete',
  'Yet to Start',
  'SME Discussion',
  'Development',
  'PB Review',
  'PB Fixes',
  'PB2 Review',
  'PB2 Fixes',
  'Alpha Development',
  'Alpha Review',
  'Alpha Fixes',
  'Beta Review',
  'Beta Fixes',
  'Gamma'

);

//Generate a list of class names and get rid of any duplicates.
foreach($getCourse as $report){

  $courseNames[] = $report->coursename;

}

$courseNames = array_unique($courseNames);


//Create the header.
echo
'<table>
  <thead>
    <tr>
      <th>#</th>';

      foreach($courseNames as $course){

        echo '<th>' . $course . '</td>';

      }

    echo
      '<th>Total</th>
    </tr>
  </thead>

<tbody>';

  //Iterate across the array(list) of status values.
  for($i = 0; $i < count($rowName); $i++){

    echo
    '<tr>';

        echo '<td>' . $rowName[$i] . '</td>';

        //Iterate through all combinations of class names and status values.
        for($j = 0; $j < count($courseNames); $j++){

          //Set flags and intial values.
          $found = FALSE;
          $total = 0;
          $value = NULL;

          for($k = 0; $k < count($getCourse); $k++){

            //***Note - The ""$getCourse[$k]->status - 1" is because the status values in your data do not appear
            //to start with an index  of 0.  Had to adjust for that.

            //Sum up all the values for matching status values.
            if(($getCourse[$k]->status - 1) == $i){

              $total += $getCourse[$k]->statuscount;

            }

            //Here we are checking that status row and the status value match and that the class names match.
            //If they do we set some values and generate a table cell value.
            if(($getCourse[$k]->status - 1) == $i && $courseNames[$j] == $getCourse[$k]->coursename){

              //Set flags and values for later.
              $found = TRUE;
              $value = $k;

            }

          }

          //Use flags and values to generate your data onto the table.
          if($found){

            echo '<td>' . $getCourse[$value]->statuscount . '</td>';

          }else{

            echo '<td>' . '0' . '</td>';

          }

        }

        echo '<td>' . $total . '</td>';

   echo
   '</tr>';

  }

echo '</tbody>
</table>';

答案 1 :(得分:0)

尝试一下。我将课程状态存储在$used_status中,然后显示未显示在foreach中的剩余状态。

$arr = array ( '1' =>'On Hold', '2' => 'Asset Incomplete', '3' => 'SME Discussion', '4' => 'a', '5' => 'b', '6' => 'c', '7' =>'d', '8' => 'e', '9' => 'f', '10' => 'g', '11' => 'h' );
    $used_status = array();
    foreach($getCourse as $report) { ?>
        <tr>
            <td>
                <?php $course_status = isset($arr[$report->status]) ? $arr[$report->status] : "-";
                echo $course_status;
                $used_status[] = $course_status; ?>
            </td>
            <td>
                <?php echo $report->coursename; ?>
            </td>
            <td>
                <?php echo $report->statuscount; ?>
            </td>
        </tr>
    <?php }
    foreach($arr as $status) {
        if(!in_array($status, $used_status)) { ?>
            <tr>
                <td>
                    <?php echo $status; ?>
                </td>
                <td>
                    <?php echo "-"; ?>
                </td>
                <td>
                    <?php echo "-"; ?>
                </td>
            </tr>
        <?php }
    } ?>