3

My Example Tables are below,

enter image description here

$hdn_roll[0] =0;
$i =0;
foreach($results as $row):
   $i++;
   $roll = $row->roll;
   $questions       = $row->questions;
   $hdn_roll[$i] = $roll;
   $count_ad = array_count_values($hdn_roll);
   $count_sp = $count_ad[$hdn_roll[$i]];

   $j = 0;
   if($hdn_roll[$i] != $hdn_roll[($i-1)]):

     if($count_sp ==1):
         create_row_after_balloting($roll,$questions);
     endif;
   else:
      $j++;

      $data['roll'.$j]          = $roll;

      $data['questions'.$j]     = $questions;
   endif;

endforeach;

Actually, I want to display Like Table:B from Table:A(MySQL Database).

Above Code, I can display 1001 to 1009 but Remain 1003,1006,1008,1006 are not display. On the other hand, Same Roll Can't stay another(1003) after one(1003)

How can i solve it in PHP code or MySQL Query.

2
  • 1
    Can you add your sql query? Commented Jan 18, 2014 at 10:37
  • Actully, Query is normal But Here i wanted to display using codeigniter. SELECT * FROM (studnet_info) WHERE status = 3. Table has 3 fields which are student_id(primary key) , roll,and questions. If it is possible using MySQL Query Or PHP code or Codeigniter. then Please help me @kruti Commented Jan 18, 2014 at 11:01

1 Answer 1

1

Try this may be its help you.

First create function in model file:

<?php
function getstudentinfo()
{
   //return $this->db->get_where('studnet_info', array('status' => 3));
   return $this->db->distinct('roll')->select('roll,questions')->where(array('status' => 3));
}

Then call in controller file:

function index()
{
   $data['student_detail'] = $this->yourmodeelname->getstudentinfo();
   $this->load->view('your view file name');
}

Then get in view file:

if(isset($student_detail)) { 
   foreach($student_detail as $student)
   {
      echo $student['roll'];
      echo '</br>';
      echo $student['questions'];
   }
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

But Output can't come like Table:B(in My given Questions table), it is showing Table:A(in My given Questions table). Actually I want to show like Table:B.
Please update query like $this->db->distinct('roll')->select('roll,questions')->where(array('status' => 3)); Please see my updated code.
If I use distinct then Roll will show unique. Actually i want to display duplicate roll at at last. Please see again the question , Data stored like Table:A in MySQL Database. But I want to display like Table:B from Table:A data. @kruti

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.