简体   繁体   English

使td值在codeigniter中显示为静态

[英]get td values display as static in codeigniter

Hi guys i am trying to display the first td values as static so i have keep those values in one array and i try to increase the values and try to display but when i get it is displaying depending on foreach values if i have one record in foreach it is displaying one value and if i have 2 records it is displaying 2 values. 嗨,大家好,我正在尝试将第一个td值显示为静态,因此我将这些值保留在一个数组中,并且尝试增加这些值并尝试显示,但是当我得到它时,如果我有一条记录,则显示该值取决于foreach值foreach它显示一个值,如果我有2条记录,它显示2个值。

But i want to display all td value if i have values in foreach or not also. 但是我想显示所有td值,如果我在foreach中也有值也没有。

Here is my code: 这是我的代码:

 <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>

Here is my controller: 这是我的控制器:

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");
    }
}

Here is my model: 这是我的模型:

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();
}

This is how i want to display here is my referrence image: 这是我要显示的方式,这是我的参考图片: 在此处输入图片说明

Can anyone help me how to do that thanks in advance. 谁能先帮助我该怎么做,谢谢。

FINAL UPDATED & WORKING VERSION 最终更新和工作版本

For me this was tricky. 对我来说,这很棘手。 This turned out to be what I felt to be a awkward indexing issue. 原来这是我觉得很尴尬的索引问题。

The problem is that your data is not optimized very well to accommodate the task you are asking for. 问题是您的数据不能很好地优化以适应您要执行的任务。 You have to make odd comparisons as you traverse the table. 遍历表格时,您必须进行奇怪的比较。 I was able to get it accomplished. 我能够完成它。

I would actually be very interested in seeing a more refined approach. 实际上,我对看到更精致的方法非常感兴趣。 But until that happens this code will dynamically generate a table that matches the output of the sample pictures that you posted in your question. 但是在此之前,此代码将动态生成一个表,该表与您在问题中发布的示例图片的输出匹配。

I have tested this code with some sample data that matches the array structure that you posted in your comments. 我已经用一些示例数据测试了此代码,这些示例数据与您在注释中发布的数组结构相匹配。

Here is the code: 这是代码:

//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>';

Try this. 尝试这个。 I am storing course status in $used_status then displaying the remaining status which are not displayed in foreach . 我将课程状态存储在$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 }
    } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM