Jump to content

Group by value ( remarks ) below tr


shashib

Recommended Posts

Below is products table :

    id | mid | wgh | remark| remkok |
    1    3     1.5    r3ok      1
    2    2     1.5              0
    3    2     0.6    nice      0
    4    1     1.2    okh       0
    5    4     1.5    bye       0
    6    4     2.4    okby      0
    7    3     3.0    oknice    1

I want to display remark below tr of group by mid ....like below

   mid    wgh  
    3     1.5   
          3.0 
  remarks : r3ok, oknice
    4     1.5
          2.4
  remarks : bye, okby
    2     1.5
          0.6
  remarks :  , nice
    1     1.2
  remarks : okh

**What i have tried as below :**

 $pid= null;
    while($row = mysql_fetch_array($result))
    {
   
     $rowpkts =  $row['mid'];
     echo "<tr class=\"undercl\">";
       if($rowpkts != $pid){
             echo'<td align="center" valign="top">'.$row["mid"].'</td>';
      }else{
            echo'<td align="center" valign="top"></td>';
       }
    
       echo'<td align="center" valign="top">'.$row["wgh"].'</td>';
        
      echo "</tr>";

    // what i tried to build for remarks as below

    $remsql = "SELECT mid as onu , GROUP_CONCAT(`remark` ORDER BY `id` ASC  SEPARATOR ', ') AS plrmks
    FROM products  WHERE remkok= 1 GROUP BY  `mid`";
    $fetchremk = mysql_query($remsql);
    $rowresults =  mysql_fetch_array($fetchremk);

     if($rowresults['onu'] ==  $pid ){   

      echo"<tr style='border-style:underline;'>";
           echo'<td align="center" align="top">'.$rowresults["plrmks"].'</td>';               
               echo"</tr>";
            }
      }

      $pid = $rowpkts;
                            
    }

But remarks is not coming proper ...i means its not display below mid=3 or mid=1.....


Edited by shashib
Link to comment
Share on other sites

We are not here 24/7, from what I can see, there is no pattern of sorting to produce that layout order. If it was mid highest to lowest I could get close to what you want, but as it is now, one cancels out the other to produce the correct order.

Link to comment
Share on other sites

hey @dsonesuk , thanks for reply ... please help me out what you have in mind ...might it can help me ...basically that remarks row will be seen only when my Column remkok = 1 ... but to explain each point was making my question confusing ..hence i wrote it simple....

 

please help me with your answer

 

Thanks

Link to comment
Share on other sites


<?php

// put your code here

$query_producttest = "SELECT * FROM products ORDER BY remkok DESC, mid DESC";

$result = mysql_query($query_producttest, $productconn) or die(mysql_error());

$totalRows_producttest = mysql_num_rows($result);

 

echo $totalRows_producttest;

echo '<table border="1" cellpadding="5" cellspacing="0" style="width: 400px;">';

echo '<th>id : mid</th><th>wgh</th>'; //<th>remkok</th>';

$pid = null;

$join_array = [];

while ($row = mysql_fetch_array($result)) {

 

if ($row['mid'] !== $pid && $pid !== null) {

echo get_remarks(implode(', ', $join_array[$pid]));

}

 

echo "<tr class=\"undercl\">";

if ($row['mid'] !== $pid) {

echo'<td align="center" valign="top">' /* .$row["id"] . ' : ' */ . $row["mid"] . '</td>';

} else {

 

echo'<td align="center" valign="top"></td>';

}

$pid = $row['mid'];

echo'<td align="center" valign="top">' . $row["wgh"] . '</td>';

 

if ($row['mid'] === $pid) {

 

$join_array[$row["mid"]][] = '  ' . $row["remark"];

}

//echo'<td align="center" valign="top">' . $row["remkok"] . '</td>';

 

echo "</tr>";

 

$pid = $row['mid'];

}

echo get_remarks(implode(', ', $join_array[$pid]));

echo '</table >';

 

function get_remarks($arr_value) {

return '<tr><td colspan="3" align="center" valign="top"> Remarks : ' . $arr_value . '</td></tr>';

}

 

var_dump($join_array);

?>

Link to comment
Share on other sites

hey dude,

 

And what if i need as below, as i tried to build in your above query ...but i failed ..... :(

    id | mid | wgh | remark| remkok | name |
    1    3     1.5    r3ok      1     name1
    2    2     1.5              0     name3
    3    2     0.6    nice      0     name3
    4    1     1.2    okh       0     name4
    5    4     1.5    bye       0     name2
    6    4     2.4    okby      0     name2
    7    3     3.0    oknice    1     name1
mid  wgh 
3    1.5 
     3.0
remarks : NAME1 - r3ok, oknice
4    1.5
     2.4
remarks : NAME2 - bye, okby
2    1.5
     0.6
remarks : NAME3 - , nice
1    1.2
remarks : NAME4 - okh
Edited by shashib
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...