0

I am newbie in programming php, i have output query data from database like this

name    activity  payment
english  activ A   20
english  activ B   25
english  activ c   30
biology  activ d   50
biology  activ e   60

but i want to show output like this:

name    activity  payment
english  activ A   20
         activ B   25
         activ c   30
biology  activ d   50
         activ e   60

how logic program use PHP to show only one name in the same name?

can you solve this problem?

2 Answers2

1

database will show names same as

name    activity  payment
english  activ A   20
english  activ B   25
english  activ c   30
biology  activ d   50
biology  activ e   60

but if you want to show records as

name    activity  payment
english  activ A   20
         activ B   25
         activ c   30
biology  activ d   50
         activ e   60

you can do this in php

for example

$datas = $fetched_data; // $fetched_data store all data which is selected          from database
// now in php use foreach loop
$previous_colm = "";
foreach($datas as $data){
    if($previous_colm == $data['name']){
          // echo without printing column name again
          echo $data['activity']." ".$data['payment'];
    }else{
         // echo with new column name
         echo echo $data['name']." ".$data['activity']." ".$data['payment'];
    }
    $previous_colm = $data['name'];
}

now this will output same as you want...!!!

Zebi Rajpot
  • 186
  • 5
  • thanks its help me a lot. but i have question why in $previous_colm=" " is empty? – Pointshop Project Aug 19 '15 at 13:04
  • $previous_colm is empty because i added this condition if($previous_colm == $data['name']){ // then print data without repeating name so first time $previous_colm is empty that means condition is false so print the user data atleast one then second time loop setting $previous_colm = $data['name']; so condition will be true so name will no repeat anymore – Zebi Rajpot Aug 19 '15 at 13:14
0
$arr = array(
    array('name' => 'english', 'activity' => 'activ A', 'payment' => 20),
    array('name' => 'english', 'activity' => 'activ B', 'payment' => 25),
    array('name' => 'english', 'activity' => 'activ c', 'payment' => 30),
    array('name' => 'biology', 'activity' => 'activ d', 'payment' => 50),
    array('name' => 'biology', 'activity' => 'activ e', 'payment' => 60),
);

$different_names = get_different_names($arr);
echo "Name Activity Payment";
display($different_names, $arr);



function display($different_names, $arr) {
    foreach($different_names as $name) {
        foreach($arr as $row) {
            if($row['name'] == $name)
                echo "</br> {$row['name']}  {$row['activity']} {$row['payment']} ";
        }
    }
}

function get_different_names($arr) {
    $different_names = array();
    foreach($arr as $row) {
        if(!in_array($row['name'], $different_names))
            $different_names[] = $row['name'];
    }
    return $different_names;
}

p.s. Zebi Rajpot `s solution is good. But I dont think it will produce the desired output if the result is not sorted by name.

Petko Kostov
  • 367
  • 1
  • 9