簡體   English   中英

使td值在codeigniter中顯示為靜態

[英]get td values display as static in codeigniter

嗨,大家好,我正在嘗試將第一個td值顯示為靜態,因此我將這些值保留在一個數組中,並且嘗試增加這些值並嘗試顯示,但是當我得到它時,如果我有一條記錄,則顯示該值取決於foreach值foreach它顯示一個值,如果我有2條記錄,它顯示2個值。

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

這是我的代碼:

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

這是我要顯示的方式,這是我的參考圖片: 在此處輸入圖片說明

誰能先幫助我該怎么做,謝謝。

最終更新和工作版本

對我來說,這很棘手。 原來這是我覺得很尷尬的索引問題。

問題是您的數據不能很好地優化以適應您要執行的任務。 遍歷表格時,您必須進行奇怪的比較。 我能夠完成它。

實際上,我對看到更精致的方法非常感興趣。 但是在此之前,此代碼將動態生成一個表,該表與您在問題中發布的示例圖片的輸出匹配。

我已經用一些示例數據測試了此代碼,這些示例數據與您在注釋中發布的數組結構相匹配。

這是代碼:

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

嘗試這個。 我將課程狀態存儲在$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