1

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.

But i want to display all td value if i have values in foreach or not also.

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: enter image description here

Can anyone help me how to do that thanks in advance.

user200
  • 291
  • 1
  • 4
  • 21
  • What do you mean by keep the first td value as static. Is it suppose to have the same value in every row? – Joseph_J Aug 20 '18 at 05:24
  • no in tbody the td column should contain array valyues which i have defined in array – user200 Aug 20 '18 at 05:29
  • what is the structure of array $getCourse? Can you give a sample of it? – Pawnesh Kumar Aug 20 '18 at 05:54
  • structure inthe sesne like what?what values i am getting or what? – user200 Aug 20 '18 at 05:57
  • yes what value you are getting in $getCourse – Pawnesh Kumar Aug 20 '18 at 06:00
  • for example as i have shown in image i have 15 stages i want to display the coursename for the particular stage and want to display the count of stage for the particular course? – user200 Aug 20 '18 at 06:16
  • I am not sure if I understand your problem. You want to fill a grid of table cells with values. But your code skips table cells when there is no value. Is this your problem? – Blue Box Aug 20 '18 at 06:28
  • exactly the same problem..means from foreach if i am getting one record it is showing first value from array orelse if i have 3 records i am getting first 3 values from array..my question is i want to display all array values in td whether i have records in foreach or not – user200 Aug 20 '18 at 06:29
  • the numbers in each td for the picture. What are they.. The status or the count? – Joseph_J Aug 20 '18 at 06:39
  • yes the status of the count and th columns are coursenames – user200 Aug 20 '18 at 06:40
  • the numbers for example physics, development is a 6. What is the 6. Is it the status or a count of something else. – Joseph_J Aug 20 '18 at 06:41
  • i have a column called status there i am storing the number for every stage of the course so if i have 6 onhold topics for course1 i ahve to show on onhold place for course 1 as 6 number – user200 Aug 20 '18 at 06:42
  • hope you people have understand my problem if not pls let me know i will explain you once again – user200 Aug 20 '18 at 06:48
  • so did javed's answer work or no? – Joseph_J Aug 20 '18 at 06:50
  • yes its almost working i am testing on that – user200 Aug 20 '18 at 06:52

2 Answers2

2

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>';
Joseph_J
  • 3,654
  • 2
  • 13
  • 22
0

Try this. I am storing course status in $used_status then displaying the remaining status which are not displayed in 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 }
    } ?>
Javed Sayyed
  • 149
  • 11